Fabio Sinibaldi 2016-05-25 16:48:24 +00:00
parent 3ef2f149ce
commit edaf385807
4 changed files with 68 additions and 25 deletions

View File

@ -26,26 +26,29 @@ public class Client {
config.register(JAXBElement.class); config.register(JAXBElement.class);
} }
private String hostname; // private String hostname;
private WebTarget rootTarget; private WebTarget rootTarget;
public Client(String hostname){ public Client(String hostname){
log.debug("Creating client for "+hostname); log.debug("Creating client for "+hostname);
this.hostname=hostname; this.hostname=hostname+"";
rootTarget= ClientBuilder.newClient(config).target(hostname).path("/data-transfer-service/").path(ServiceConstants.APPLICATION_PATH); rootTarget= ClientBuilder.newClient(config).target(hostname).path("/data-transfer-service/").path(ServiceConstants.APPLICATION_PATH);
} }
public TransferCapabilities getCapabilties(){ public TransferCapabilities getCapabilties(){
log.debug("Getting capabilities to {} ",hostname);
return rootTarget.path(ServiceConstants.CAPABILTIES_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).get(TransferCapabilities.class); return rootTarget.path(ServiceConstants.CAPABILTIES_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).get(TransferCapabilities.class);
} }
public TransferTicket submit(TransferRequest request){ public TransferTicket submit(TransferRequest request){
log.debug("Sending request {} to {}",request,hostname);
return rootTarget.path(ServiceConstants.REQUESTS_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).post(Entity.entity(request,MediaType.APPLICATION_XML),TransferTicket.class); return rootTarget.path(ServiceConstants.REQUESTS_SERVLET_NAME).request(MediaType.APPLICATION_XML_TYPE).post(Entity.entity(request,MediaType.APPLICATION_XML),TransferTicket.class);
} }
public TransferTicket getTransferStatus(String transferId){ public TransferTicket getTransferStatus(String transferId){
log.debug("Requesting transfer status [id = {}, hostname={}]",transferId,hostname);
return rootTarget.path(ServiceConstants.STATUS_SERVLET_NAME).path(transferId).request(MediaType.APPLICATION_XML).get(TransferTicket.class); return rootTarget.path(ServiceConstants.STATUS_SERVLET_NAME).path(transferId).request(MediaType.APPLICATION_XML).get(TransferTicket.class);
} }
} }

View File

@ -1,36 +1,55 @@
package org.gcube.data.transfer.library.transferers; package org.gcube.data.transfer.library.transferers;
import java.net.MalformedURLException;
import java.net.URL;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.gcube.data.transfer.library.caches.CapabilitiesCache; import org.gcube.data.transfer.library.caches.CapabilitiesCache;
import org.gcube.data.transfer.library.faults.HostingNodeNotFoundException; import org.gcube.data.transfer.library.faults.HostingNodeNotFoundException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException; import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException; import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.gcube.data.transfer.library.utils.Utils;
import org.gcube.data.transfer.model.ServiceConstants;
import org.gcube.data.transfer.model.TransferCapabilities; import org.gcube.data.transfer.model.TransferCapabilities;
@Slf4j @Slf4j
public class TransfererBuilder { public class TransfererBuilder {
private static final int timeout=10*1000; private static final int timeout=10*1000;
//e.g. http://pc-fabio.isti.cnr.it:8080/data-transfer-service/gcube/service
public static Transferer getTransfererByHost(String host) throws UnreachableNodeException, ServiceNotFoundException{
log.debug("Get transferer by Host "+host); public static Transferer getTransfererByHost(String endpoint) throws UnreachableNodeException, ServiceNotFoundException{
if(!Utils.pingURL(host, timeout)) throw new UnreachableNodeException("No response from host in "+timeout); log.debug("Get transferer by Host "+endpoint);
if(!Utils.pingURL(host+"REST-API/"+ServiceConstants.APPLICATION_PATH, timeout)) throw new ServiceNotFoundException("No DT Service found @ "+host+"REST-API/"+ServiceConstants.APPLICATION_PATH); try{
log.debug("Host is ok, getting targetCapabilities"); URL url=new URL(endpoint);
TransferCapabilities cap=CapabilitiesCache.getInstance().getObject(host);
return new HTTPTransferer(cap); String hostname=url.getProtocol()+"://"+url.getHost()+":"+url.getPort();
//TODO Implement checks
// if(!Utils.pingURL(host, timeout)) throw new UnreachableNodeException("No response from host in "+timeout);
// String finalHost=host;
// if(!finalHost.endsWith(ServiceConstants.APPLICATION_PATH)){
// // adjust host
// finalHost=finalHost+(host.endsWith("/")?"":"/")+"data-transfer-service"+ServiceConstants.APPLICATION_PATH;
// }
//
//
// if(!Utils.pingURL(finalHost, timeout)) throw new ServiceNotFoundException("No DT Service found @ "+finalHost);
// log.debug("Host is ok, getting targetCapabilities");
TransferCapabilities cap=CapabilitiesCache.getInstance().getObject(hostname);
return new HTTPTransferer(cap);
}catch(MalformedURLException e){
throw new ServiceNotFoundException(e);
}
} }
public static Transferer getTransfererByhostingNodeId(String hostId) throws HostingNodeNotFoundException, UnreachableNodeException, ServiceNotFoundException{ public static Transferer getTransfererByhostingNodeId(String hostId) throws HostingNodeNotFoundException, UnreachableNodeException, ServiceNotFoundException{
String hostname=retrieveHostnameByNodeId(hostId); String hostname=retrieveHostnameByNodeId(hostId);
return getTransfererByHost(hostname); return getTransfererByHost(hostname);
} }
private static String retrieveHostnameByNodeId(String nodeId)throws HostingNodeNotFoundException{ private static String retrieveHostnameByNodeId(String nodeId)throws HostingNodeNotFoundException{
//TODO implement endpoint retrieval
return null; return null;
} }
} }

View File

@ -15,19 +15,19 @@ import org.gcube.data.transfer.model.TransferTicket.Status;
import org.gcube.data.transfer.model.options.HttpDownloadOptions; import org.gcube.data.transfer.model.options.HttpDownloadOptions;
import org.gcube.data.transfer.model.settings.HttpDownloadSettings; import org.gcube.data.transfer.model.settings.HttpDownloadSettings;
import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientConfig;
import org.junit.Before; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
public class TestClientCalls { public class TestClientCalls {
String hostname="http://pc-fabio.isti.cnr.it:8080"; static String hostname="http://pc-fabio.isti.cnr.it:8080";
String scope="/gcube/devNext"; static String scope="/gcube/devNext";
Client client; static Client client;
@Before @BeforeClass
public void init(){ public static void init(){
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
client=new Client(hostname); client=new Client(hostname);
} }

View File

@ -1,20 +1,24 @@
package org.gcube.data.transfer.library; package org.gcube.data.transfer.library;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.data.transfer.library.faults.FailedTransferException; import org.gcube.data.transfer.library.faults.FailedTransferException;
import org.gcube.data.transfer.library.faults.InitializationException; import org.gcube.data.transfer.library.faults.InitializationException;
import org.gcube.data.transfer.library.faults.InvalidSourceException; import org.gcube.data.transfer.library.faults.InvalidSourceException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException; import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.library.faults.SourceNotSetException; import org.gcube.data.transfer.library.faults.SourceNotSetException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException; import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.gcube.data.transfer.library.utils.StorageUtils;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
public class TransfererTest { public class TransfererTest {
static String hostname="http://pc-fabio.isti.cnr.it:8080"; static String hostname="http://pc-fabio.isti.cnr.it:8080/data-transfer-service/gcube/service";
static String scope="/gcube/devNext"; static String scope="/gcube/devNext";
static DataTransferClient client; static DataTransferClient client;
@ -34,14 +38,31 @@ public class TransfererTest {
@Test @Test
public void httpUrl() throws MalformedURLException, InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{ public void httpUrl() throws MalformedURLException, InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String link="https://www.dropbox.com/s/giqsbgmlkk9zklc/COMA%20-%20Last%20Aim.mp3?dl=0"; String link="http://goo.gl/oLP7zG";
System.out.println(client.httpSource(link)); System.out.println(client.httpSource(link));
} }
@Test @Test
public void storage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{ public void storage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException, RemoteBackendException, FileNotFoundException{
String toUpload="/home/fabio/Downloads/Incantesimi3_5.doc"; String toUpload="/home/fabio/Downloads/Incantesimi3_5.doc";
System.out.println(client.storageId(toUpload)); System.out.println(client.storageId(StorageUtils.putOntoStorage(new File(toUpload))));
}
@Test
public void wrongStorage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
System.out.println(client.storageId("13245780t"));
}
@Test
public void wrongLocal() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String localFile="/home/fabio/Downloads/123045689.mp3";
System.out.println(client.localFile(localFile));
}
@Test
public void wrongUrl() throws MalformedURLException, InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String link="https://www.dropbox.com/s/789023450";
System.out.println(client.httpSource(link));
} }
} }