Fabio Sinibaldi 2016-05-25 13:48:03 +00:00
parent cac35c3539
commit 3ef2f149ce
6 changed files with 132 additions and 18 deletions

View File

@ -67,7 +67,13 @@
<version>2.18</version>
</dependency>
<!-- Test log binding -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -1,8 +1,78 @@
package org.gcube.data.transfer.library;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.gcube.data.transfer.library.faults.FailedTransferException;
import org.gcube.data.transfer.library.faults.HostingNodeNotFoundException;
import org.gcube.data.transfer.library.faults.InitializationException;
import org.gcube.data.transfer.library.faults.InvalidSourceException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.library.faults.SourceNotSetException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.gcube.data.transfer.library.transferers.Transferer;
import org.gcube.data.transfer.library.transferers.TransfererBuilder;
@Slf4j
public class DataTransferClient {
private Transferer transferer=null;
private DataTransferClient(Transferer transferer) {
this.transferer=transferer;
}
public static DataTransferClient getInstanceByEndpoint(String endpoint) throws UnreachableNodeException, ServiceNotFoundException{
log.debug("Getting transferer for endpoint : "+endpoint);
return new DataTransferClient(TransfererBuilder.getTransfererByHost(endpoint));
}
public static DataTransferClient getInstanceByNodeId(String id) throws HostingNodeNotFoundException, UnreachableNodeException, ServiceNotFoundException{
log.debug("Getting transferer for nodeId : "+id);
return new DataTransferClient(TransfererBuilder.getTransfererByhostingNodeId(id));
}
@Synchronized("transferer")
public TransferResult localFile(String path) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");
log.debug("Sending local file {} to {}",path,transferer.getDestinationCapabilities().getHostName());
transferer.localFile(path);
return transferer.transfer();
}
@Synchronized("transferer")
public TransferResult localFile(File file) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");
log.debug("Sending local file {} to {}",file.getAbsolutePath(),transferer.getDestinationCapabilities().getHostName());
transferer.localFile(file);
return transferer.transfer();
}
@Synchronized("transferer")
public TransferResult httpSource(String url) throws MalformedURLException, InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");
log.debug("Passed url string : "+url);
return this.httpSource(new URL(url));
}
@Synchronized("transferer")
public TransferResult httpSource(URL url) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");
log.debug("Sending from url {} to {}",url,transferer.getDestinationCapabilities().getHostName());
transferer.fromURL(url);
return transferer.transfer();
}
@Synchronized("transferer")
public TransferResult storageId(String id) throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
if(transferer==null) throw new RuntimeException("Transferer not set, please set destination before trying to transfer");
log.debug("Sending from storage id {} to {}",id,transferer.getDestinationCapabilities().getHostName());
transferer.storageFileId(id);
return transferer.transfer();
}
}

View File

@ -27,6 +27,8 @@ public class HTTPTransferer extends Transferer {
private URL link;
private String toDeleteStorageId=null;
@Override
protected TransferRequest prepareRequest()throws InitializationException {
return new TransferRequest("", new HttpDownloadSettings(link, HttpDownloadOptions.DEFAULT));
@ -35,8 +37,9 @@ public class HTTPTransferer extends Transferer {
private URL getHttpLink() throws RemoteBackendException, FileNotFoundException, InvalidSourceException, MalformedURLException{
if(source instanceof LocalSource){
return new URL(StorageUtils.putOntoStorage(((LocalSource)source).getTheSource()));
}else if (source instanceof StorageSource){
toDeleteStorageId=StorageUtils.putOntoStorage(((LocalSource)source).getTheSource());
return new URL(StorageUtils.getUrlById(toDeleteStorageId));
}else if (source instanceof StorageSource){
return new URL(StorageUtils.getUrlById(((StorageSource)source).getTheSource()));
}else if (source instanceof URLSource){
return ((URLSource)source).getTheSource();
@ -59,4 +62,10 @@ public class HTTPTransferer extends Transferer {
throw new InitializationException(e);
}
}
@Override
protected void clean() {
// TODO Auto-generated method stub
super.clean();
}
}

View File

@ -108,4 +108,8 @@ public abstract class Transferer {
}
public TransferCapabilities getDestinationCapabilities(){
return targetCapabilities;
}
}

View File

@ -21,13 +21,11 @@ public class StorageUtils {
}
//return public link
//return Id
public static final String putOntoStorage(File source) throws RemoteBackendException, FileNotFoundException{
IClient client=getClient();
log.debug("Uploading local file "+source.getAbsolutePath());
String id=client.put(true).LFile(new FileInputStream(source)).RFile(Utils.getUniqueString());
log.debug("Id is "+id);
return client.getUrl().RFile(id);
return client.put(true).LFile(new FileInputStream(source)).RFile(Utils.getUniqueString());
}
public static final boolean checkStorageId(String id){
@ -39,4 +37,9 @@ public class StorageUtils {
log.debug("Id is "+id);
return client.getUrl().RFile(id);
}
public static final void removeById(String id){
IClient client=getClient();
client.remove().RFile(id);
}
}

View File

@ -1,25 +1,47 @@
package org.gcube.data.transfer.library;
import java.net.MalformedURLException;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.transfer.library.client.Client;
import org.gcube.data.transfer.library.transferers.TransfererBuilder;
import org.junit.Before;
import org.gcube.data.transfer.library.faults.FailedTransferException;
import org.gcube.data.transfer.library.faults.InitializationException;
import org.gcube.data.transfer.library.faults.InvalidSourceException;
import org.gcube.data.transfer.library.faults.ServiceNotFoundException;
import org.gcube.data.transfer.library.faults.SourceNotSetException;
import org.gcube.data.transfer.library.faults.UnreachableNodeException;
import org.junit.BeforeClass;
import org.junit.Test;
public class TransfererTest {
String hostname="http://pc-fabio.isti.cnr.it:8080";
String scope="/gcube/devNext";
static String hostname="http://pc-fabio.isti.cnr.it:8080";
static String scope="/gcube/devNext";
static DataTransferClient client;
@Before
public void init(){
ScopeProvider.instance.set(scope);
@BeforeClass
public static void init() throws UnreachableNodeException, ServiceNotFoundException{
ScopeProvider.instance.set(scope);
client=DataTransferClient.getInstanceByEndpoint(hostname);
}
@Test
public void single(){
TransfererBuilder.
public void localFile() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String localFile="/home/fabio/Downloads/Puntata 3.mp3";
System.out.println(client.localFile(localFile));
}
@Test
public void httpUrl() throws MalformedURLException, InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String link="https://www.dropbox.com/s/giqsbgmlkk9zklc/COMA%20-%20Last%20Aim.mp3?dl=0";
System.out.println(client.httpSource(link));
}
@Test
public void storage() throws InvalidSourceException, SourceNotSetException, FailedTransferException, InitializationException{
String toUpload="/home/fabio/Downloads/Incantesimi3_5.doc";
System.out.println(client.storageId(toUpload));
}
}