diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/StorageHubClient.java b/src/main/java/org/gcube/common/storagehub/client/dsl/StorageHubClient.java index 9cee561..70fc563 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/StorageHubClient.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/StorageHubClient.java @@ -1,5 +1,7 @@ package org.gcube.common.storagehub.client.dsl; +import java.io.File; +import java.io.InputStream; import java.net.URI; import org.gcube.common.storagehub.client.plugins.AbstractPlugin; @@ -97,4 +99,16 @@ public class StorageHubClient { return userClient.exists(user); } + public String uploadFileOnVolatile(File file) throws StorageHubException { + return wsClient.uploadFileToVolatile(file); + } + + public String uploadFileOnVolatile(InputStream stream, String fileName) throws StorageHubException { + return wsClient.uploadFileToVolatile(stream, fileName); + } + + public String uploadFileOnVolatile(InputStream stream, String fileName, long size) throws StorageHubException { + return wsClient.uploadFileToVolatile(stream, fileName, size); + } + } diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultWorkspaceManager.java b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultWorkspaceManager.java index cc645f4..0dd9901 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultWorkspaceManager.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultWorkspaceManager.java @@ -1,5 +1,9 @@ package org.gcube.common.storagehub.client.proxies; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -23,6 +27,13 @@ import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.query.Query; import org.gcube.common.storagehub.model.service.ItemList; import org.gcube.common.storagehub.model.service.ItemWrapper; +import org.glassfish.jersey.media.multipart.ContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataMultiPart; +import org.glassfish.jersey.media.multipart.MultiPart; +import org.glassfish.jersey.media.multipart.MultiPartFeature; +import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; +import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart; import com.fasterxml.jackson.databind.ObjectMapper; @@ -107,7 +118,7 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(ItemList.class); } }; @@ -118,7 +129,7 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor throw new RuntimeException(e); } } - + @Override public List getVreFolders(int start, int limit, String ... excludeNodes) { Call call = new Call() { @@ -141,8 +152,8 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - - + + return response.getSource().readEntity(ItemList.class); } @@ -167,14 +178,14 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor params.put("exclude",excludeNodes); GXInboundResponse response = myManager.queryParams(params).get(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(ItemWrapper.class); } }; @@ -201,8 +212,8 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - - + + return response.getSource().readEntity(ItemList.class); } }; @@ -227,14 +238,14 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor params.put("exclude",excludeNodes); GXInboundResponse response = myManager.queryParams(params).get(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(ItemWrapper.class); } }; @@ -252,16 +263,16 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager.path("trash").path("empty"); - + GXInboundResponse response = myManager.queryParams(initQueryParameters()).delete(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return null; } }; @@ -281,22 +292,22 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor public String call(GXWebTargetAdapterRequest manager) throws Exception { Objects.nonNull(trashedItemid); GXWebTargetAdapterRequest myManager = manager.path("trash").path("restore"); - + MultivaluedMap formData = new MultivaluedHashMap(); formData.add("trashedItemId", trashedItemid); if (destinationFolderId !=null) formData.add("destinationId", destinationFolderId); - + GXInboundResponse response = myManager.queryParams(initQueryParameters()).put(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(String.class); } }; @@ -343,14 +354,14 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor params.put("e", new Object[] { URLEncoder.encode(serializedJson)}); GXInboundResponse response = myManager.queryParams(params).get(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(ItemList.class); } }; @@ -368,21 +379,21 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor @Override public String call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager.path("count"); - + GXInboundResponse response = myManager.queryParams(initQueryParameters()).get(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(String.class); } }; try { - + return Long.parseLong(delegate.make(call)); }catch(Exception e) { throw new RuntimeException(e); @@ -395,16 +406,16 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor @Override public String call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager.path("size"); - + GXInboundResponse response = myManager.queryParams(initQueryParameters()).get(); - + if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); } - + return response.getSource().readEntity(String.class); } }; @@ -415,6 +426,65 @@ public class DefaultWorkspaceManager extends DefaultManagerClient implements Wor } } + @Override + public String uploadFileToVolatile(InputStream stream, String fileName, long contentLength) throws StorageHubException{ + Call call = new Call() { + @Override + public String call(GXWebTargetAdapterRequest manager) throws Exception { + Objects.requireNonNull(stream, "stream cannot be null"); + Objects.requireNonNull(fileName, "parentId cannot be null"); + + GXWebTargetAdapterRequest myManager = manager.register(MultiPartFeature.class) + .path("volatile"); + + GXInboundResponse response =null; + FormDataMultiPart multipart = new FormDataMultiPart(); + + StreamDataBodyPart filePart = new StreamDataBodyPart("file", stream); + filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName(fileName).size(contentLength).build()); + + MultiPart multipartEntity = new FormDataMultiPart().bodyPart(filePart); + + response = myManager.queryParams(initQueryParameters()).post(Entity.entity(multipartEntity, multipartEntity.getMediaType())); + + if (response.isErrorResponse()) { + if (response.hasException()) { + throw response.getException(); + }else + throw new BackendGenericError("HTTP error code is "+response.getHTTPCode()); + } + + return response.getSource().readEntity(String.class); + } + }; + try { + return delegate.make(call); + }catch(StorageHubException e) { + throw e; + }catch(Exception e1) { + throw new RuntimeException(e1); + } + } + + @Override + public String uploadFileToVolatile(InputStream stream, String fileName) throws StorageHubException { + return this.uploadFileToVolatile(stream, fileName, -1); + } + + @Override + public String uploadFileToVolatile(File file) throws StorageHubException { + if (!file.exists()) throw new BackendGenericError("file "+file.getAbsolutePath()+" doesn't exist"); + String fileName = file.getName(); + + long size = file.length(); + try (InputStream is = new FileInputStream(file)){ + return uploadFileToVolatile(is, fileName, size); + }catch (IOException io) { + throw new BackendGenericError("error closing stream", io); + } + + } + } diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/WorkspaceManagerClient.java b/src/main/java/org/gcube/common/storagehub/client/proxies/WorkspaceManagerClient.java index 2eba7ea..f832a21 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/WorkspaceManagerClient.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/WorkspaceManagerClient.java @@ -1,5 +1,7 @@ package org.gcube.common.storagehub.client.proxies; +import java.io.File; +import java.io.InputStream; import java.util.List; import org.gcube.common.storagehub.model.exceptions.BackendGenericError; @@ -34,4 +36,10 @@ public interface WorkspaceManagerClient extends ManagerClient{ long getTotalVolume(); + String uploadFileToVolatile(InputStream stream, String filename, long contentLength) throws StorageHubException; + + String uploadFileToVolatile(InputStream stream, String filename) throws StorageHubException; + + String uploadFileToVolatile(File file) throws StorageHubException; + } diff --git a/src/test/java/org/gcube/data/access/fs/Items.java b/src/test/java/org/gcube/data/access/fs/Items.java index 156b6a9..f949850 100644 --- a/src/test/java/org/gcube/data/access/fs/Items.java +++ b/src/test/java/org/gcube/data/access/fs/Items.java @@ -15,6 +15,8 @@ import java.util.List; import java.util.Map.Entry; import java.util.Properties; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.client.dsl.ContainerType; import org.gcube.common.storagehub.client.dsl.FileContainer; @@ -53,8 +55,8 @@ public class Items { String token = splitValue[1]; //SecretManagerProvider.instance.set(new GCubeSecret(token)); - /*SecurityTokenProvider.instance.set(token); - ScopeProvider.instance.set(context);*/ + SecurityTokenProvider.instance.set(token); + ScopeProvider.instance.set(splitValue[0]); } catch (Exception e) { e.printStackTrace(); } @@ -91,6 +93,18 @@ public class Items { } + @Test + public void uploadTovolatile() throws StorageHubException { + StorageHubClient shc = new StorageHubClient(); + + + String vresFile = "/home/lucio/Downloads/gardaland_prenotazione.pdf"; + + File file = new File(vresFile); + + System.out.println(shc.uploadFileOnVolatile(file)); + } + @Test public void search() { @@ -236,7 +250,7 @@ public class Items { public void removeUser() throws Exception { StorageHubClient shc = new StorageHubClient(); - shc.deleteUserAccount("_sergencovid19"); + shc.deleteUserAccount(""); } diff --git a/src/test/java/org/gcube/data/access/fs/TestCall.java b/src/test/java/org/gcube/data/access/fs/TestCall.java index d10260a..5bf97ae 100644 --- a/src/test/java/org/gcube/data/access/fs/TestCall.java +++ b/src/test/java/org/gcube/data/access/fs/TestCall.java @@ -14,6 +14,8 @@ import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; //code-repo.d4science.org/gCubeSystem/storagehub-client-library.git import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.client.plugins.AbstractPlugin; @@ -44,7 +46,7 @@ public class TestCall { private static Logger log = LoggerFactory.getLogger(TestCall.class); - private static final String propFile = "/home/lucio/secret.properties"; + private static final String propFile = "/home/lucio/tokens.properties"; private static final String tokens = "dev-root"; @@ -57,8 +59,12 @@ public class TestCall { String value =(String)prop.get(tokens); String[] splitValue = value.split(","); String token = splitValue[1]; + String scope = splitValue[0]; //SecretManagerProvider.instance.set(new GCubeSecret(token)); + SecurityTokenProvider.instance.set(token); + ScopeProvider.instance.set(scope); + } catch (Exception e) { e.printStackTrace(); } @@ -69,7 +75,7 @@ public class TestCall { @Test public void sendMessagesWithAttachment() throws Exception { MessageManagerClient client = AbstractPlugin.messages().build(); - client.sendMessage(Arrays.asList("massimiliano.assante"), "subject encodato ? è ", "è econdato ??", Arrays.asList("4827dd33-6eea-4bba-b78b-b98e2fd2ddfa")); + client.sendMessage(Arrays.asList("massimiliano.assante"), "subject encodato ? è ", "è econdato ??", null); } @Test