From 2fd18396ce400edd9437e435edfc67c8860f6edc Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Wed, 26 Sep 2018 13:24:21 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/Common/storagehub-client@171673 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/dsl/FolderContainer.java | 6 +- .../storagehub/client/dsl/ItemContainer.java | 2 +- .../storagehub/client/dsl/ListResolver.java | 5 +- .../client/dsl/ListResolverTyped.java | 16 ++ .../storagehub/client/dsl/ListRetriever.java | 2 +- .../client/proxies/DefaultItemManager.java | 153 +++++++++++------- .../client/proxies/ItemManagerClient.java | 8 + .../java/org/gcube/data/access/fs/Items.java | 31 +++- .../org/gcube/data/access/fs/TestCall.java | 6 +- 9 files changed, 157 insertions(+), 72 deletions(-) create mode 100644 src/main/java/org/gcube/common/storagehub/client/dsl/ListResolverTyped.java diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/FolderContainer.java b/src/main/java/org/gcube/common/storagehub/client/dsl/FolderContainer.java index c6b1f51..709eace 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/FolderContainer.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/FolderContainer.java @@ -25,8 +25,8 @@ public class FolderContainer extends ItemContainer{ return ContainerType.FOLDER; } - public ListResolver list() { - return new ListResolver(excludes -> itemclient.getChildren(itemId, excludes), itemclient); + public ListResolverTyped list() { + return new ListResolverTyped((onlyType, excludes) -> itemclient.getChildren(itemId, onlyType, excludes), itemclient) ; } public FileContainer uploadFile(InputStream stream, String filename, String description) { @@ -47,7 +47,7 @@ public class FolderContainer extends ItemContainer{ } public ListResolver findByName(String namePattern) { - return new ListResolver(excludes -> itemclient.findChildrenByNamePattern(itemId, namePattern , excludes), itemclient); + return new ListResolver((onlyType, excludes) -> itemclient.findChildrenByNamePattern(itemId, namePattern , excludes), itemclient); } public FolderContainer share(Set users, AccessType accessType) throws Exception { diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java b/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java index ec53f9a..3f617ce 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java @@ -42,7 +42,7 @@ public abstract class ItemContainer { } public ListResolver getAnchestors() { - return new ListResolver(excludes -> itemclient.getAnchestors(this.itemId,excludes) , itemclient); + return new ListResolver((onlyType, excludes) -> itemclient.getAnchestors(this.itemId,excludes) , itemclient); } public void delete() { diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolver.java b/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolver.java index b3aba50..e033bb6 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolver.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolver.java @@ -17,6 +17,8 @@ public class ListResolver { Set excludes = new HashSet<>(Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.CONTENT_NAME, NodeConstants.METADATA_NAME)); + Class onlyType = null; + protected ListResolver(ListRetriever retriever, ItemManagerClient itemClient) { this.retriever = retriever; @@ -38,8 +40,9 @@ public class ListResolver { return this; } + public List getItems(){ - return retriever.getList(excludes.toArray(new String[excludes.size()])); + return retriever.getList(onlyType, excludes.toArray(new String[excludes.size()])); } public List> getContainers(){ diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolverTyped.java b/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolverTyped.java new file mode 100644 index 0000000..ceb21ec --- /dev/null +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/ListResolverTyped.java @@ -0,0 +1,16 @@ +package org.gcube.common.storagehub.client.dsl; + +import org.gcube.common.storagehub.client.proxies.ItemManagerClient; +import org.gcube.common.storagehub.model.items.Item; + +public class ListResolverTyped extends ListResolver { + + protected ListResolverTyped(ListRetriever retriever, ItemManagerClient itemClient) { + super(retriever, itemClient); + } + + public ListResolver ofType(Class type){ + onlyType = type; + return this; + } +} diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/ListRetriever.java b/src/main/java/org/gcube/common/storagehub/client/dsl/ListRetriever.java index e8ff5f9..6c79887 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/ListRetriever.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/ListRetriever.java @@ -6,5 +6,5 @@ import org.gcube.common.storagehub.model.items.Item; public interface ListRetriever { - List getList(String ... excludes); + List getList(Class onlyType, String ... excludes); } diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java index 298d2cf..f27e317 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java @@ -22,6 +22,7 @@ import org.gcube.common.gxrest.response.inbound.GXInboundResponse; import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.model.acls.ACL; import org.gcube.common.storagehub.model.acls.AccessType; +import org.gcube.common.storagehub.model.annotations.RootNode; import org.gcube.common.storagehub.model.exceptions.BackendGenericError; import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.service.ItemList; @@ -41,7 +42,7 @@ public class DefaultItemManager implements ItemManagerClient { @Override - public List getChildren(String id, String ... excludeNodes) { + public List getChildren(String id, Class onlyOfType, String ... excludeNodes) { Call call = new Call() { @Override public ItemList call(GXWebTargetAdapterRequest manager) throws Exception { @@ -50,6 +51,9 @@ public class DefaultItemManager implements ItemManagerClient { if (excludeNodes !=null && excludeNodes.length>0) params.put("exclude",excludeNodes); + if (onlyOfType!=null) + params.put("onlyType", new Object[] {resolveNodeType(onlyOfType)}); + GXInboundResponse response = myManager.setAcceptedResponseType(MediaType.APPLICATION_JSON_TYPE).queryParams(params).get(); if (response.hasGXError()) @@ -67,8 +71,96 @@ public class DefaultItemManager implements ItemManagerClient { throw new RuntimeException(e); } } + + @Override + public List getChildren(String id, int start, int limit, Class onlyOfType, String... excludeNodes) { + Call call = new Call() { + @Override + public ItemList call(GXWebTargetAdapterRequest manager) throws Exception { + GXWebTargetAdapterRequest myManager = manager.path(id).path("children").path("paged"); + Map params = new HashMap<>(); + + + if (excludeNodes !=null && excludeNodes.length>0) + params.put("exclude",excludeNodes); + + if (onlyOfType!=null) + params.put("onlyType", new Object[] {resolveNodeType(onlyOfType)}); + + params.put("start", new Object[] {start}); + params.put("limit", new Object[] {limit}); + + + GXInboundResponse response = myManager.setAcceptedResponseType(MediaType.APPLICATION_JSON_TYPE).queryParams(params).get(); + + if (response.hasGXError()) + throw response.getException(); + + ItemList items = response.getSource().readEntity(ItemList.class); + + return items; + } + }; + try { + ItemList result = delegate.make(call); + return result.getItemlist(); + }catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public List getChildren(String id, int start, int limit, + String... excludeNodes) { + return getChildren(id, start, limit, null, excludeNodes); + } + + @Override + public List getChildren(String id, String ... excludeNodes) { + return getChildren(id, null, excludeNodes); + } + + + @Override + public Integer childrenCount(String id , Class onlyOfType) { + Call call = new Call() { + @Override + public Integer call(GXWebTargetAdapterRequest manager) throws Exception { + GXWebTargetAdapterRequest myManager = manager.path(id).path("children").path("count"); + Map params = new HashMap<>(); + if (onlyOfType!=null) + params.put("onlyType", new Object[] {resolveNodeType(onlyOfType)}); + + GXInboundResponse response = myManager.queryParams(params).setAcceptedResponseType(MediaType.TEXT_PLAIN_TYPE).get(); + + if (response.hasGXError()) + throw response.getException(); + + return response.getSource().readEntity(Integer.class); + } + }; + try { + Integer result = delegate.make(call); + return result; + }catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public Integer childrenCount(String id) { + return childrenCount(null); + } + + private String resolveNodeType(Class itemClass){ + if (!itemClass.isAnnotationPresent(RootNode.class)) return null; + String nodeType= itemClass.getAnnotation(RootNode.class).value(); + return nodeType; + } + + @Override public StreamDescriptor download(String id, String... excludeNodes) { Call call = new Call() { @@ -182,40 +274,6 @@ public class DefaultItemManager implements ItemManagerClient { } } - @Override - public List getChildren(String id, int start, int limit, String... excludeNodes) { - Call call = new Call() { - @Override - public ItemList call(GXWebTargetAdapterRequest manager) throws Exception { - GXWebTargetAdapterRequest myManager = manager.path(id).path("children").path("paged"); - Map params = new HashMap<>(); - - - if (excludeNodes !=null && excludeNodes.length>0) - params.put("exclude",excludeNodes); - - params.put("start", new Object[] {start}); - params.put("limit", new Object[] {limit}); - - - GXInboundResponse response = myManager.setAcceptedResponseType(MediaType.APPLICATION_JSON_TYPE).queryParams(params).get(); - - if (response.hasGXError()) - throw response.getException(); - - ItemList items = response.getSource().readEntity(ItemList.class); - - return items; - } - }; - try { - ItemList result = delegate.make(call); - return result.getItemlist(); - }catch(Exception e) { - throw new RuntimeException(e); - } - } - @Override public List findChildrenByNamePattern(String id, String name, String... excludeNodes) { Call call = new Call() { @@ -246,30 +304,6 @@ public class DefaultItemManager implements ItemManagerClient { } - @Override - public Integer childrenCount(String id) { - Call call = new Call() { - @Override - public Integer call(GXWebTargetAdapterRequest manager) throws Exception { - GXWebTargetAdapterRequest myManager = manager.path(id).path("children").path("count"); - - GXInboundResponse response = myManager.setAcceptedResponseType(MediaType.TEXT_PLAIN_TYPE).get(); - - if (response.hasGXError()) - throw response.getException(); - - return response.getSource().readEntity(Integer.class); - } - }; - try { - Integer result = delegate.make(call); - return result; - }catch(Exception e) { - throw new RuntimeException(e); - } - } - - @Override public String uploadFile(InputStream stream, String parentId, String fileName, String description) { Call call = new Call() { @@ -513,5 +547,4 @@ public class DefaultItemManager implements ItemManagerClient { } - } diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java b/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java index 20858fb..a5422c9 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java @@ -16,12 +16,18 @@ public interface ItemManagerClient { List getChildren(String id, String ... excludeNodes); + List getChildren(String id, Class onlyOfType, String ... excludeNodes); + + List getChildren(String id, int start, int limit, Class onlyOfType, String ... excludeNodes); + List getChildren(String id, int start, int limit, String ... excludeNodes); List getAnchestors(String id, String ... excludeNodes); Integer childrenCount(String id); + Integer childrenCount(String id, Class onlyOfType); + Item get(String id, String ... excludeNodes); StreamDescriptor download(String id, String... excludeNodes); @@ -47,5 +53,7 @@ public interface ItemManagerClient { String uploadArchive(InputStream stream, String parentId, String extractionFolderName); String unshareFolder(String id, Set users); + + } 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 b6112a7..ece5a48 100644 --- a/src/test/java/org/gcube/data/access/fs/Items.java +++ b/src/test/java/org/gcube/data/access/fs/Items.java @@ -1,5 +1,6 @@ package org.gcube.data.access.fs; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -26,8 +27,10 @@ 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; +import org.gcube.common.storagehub.client.dsl.FolderContainer; import org.gcube.common.storagehub.client.dsl.ItemContainer; import org.gcube.common.storagehub.client.dsl.StorageHubClient; +import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.media.multipart.FormDataMultiPart; @@ -42,7 +45,9 @@ public class Items { @BeforeClass public static void setUp(){ + //SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548"); SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548"); + ScopeProvider.instance.set("/gcube"); } @@ -65,6 +70,28 @@ public class Items { } } + + @Test + public void download() throws Exception { + StorageHubClient shc = new StorageHubClient(); + + FolderContainer openResolver = shc.open("5cf82ba3-53b9-4352-b37a-c4a94800eaec").asFolder(); + + StreamDescriptor streamDescr = openResolver.findByName("stat_algo.project").getContainers().get(0).download(); + + + File output = Files.createTempFile("down", streamDescr.getFileName()).toFile(); + try (BufferedInputStream bi = new BufferedInputStream(streamDescr.getStream()); FileOutputStream fo = new FileOutputStream(output)){ + byte[] buf = new byte[2048]; + int read = -1; + while ((read=bi.read(buf))!=-1) { + fo.write(buf, 0, read); + } + } + + System.out.println("file written "+output.getAbsolutePath()); + + } @Test public void emptyTrash() throws Exception { @@ -103,9 +130,9 @@ public class Items { @Test public void findByName() throws Exception{ StorageHubClient shc = new StorageHubClient(); - List containers = shc.getWSRoot().findByName("DataMiner").getItems(); + List containers = shc.getWSRoot().list().ofType(FolderItem.class).getItems(); for (Item container : containers) { - System.out.println("name is :"+container.getName()); + System.out.println("name is :"+container.getClass().getSimpleName()); } } 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 a098f13..dd80242 100644 --- a/src/test/java/org/gcube/data/access/fs/TestCall.java +++ b/src/test/java/org/gcube/data/access/fs/TestCall.java @@ -51,7 +51,7 @@ public class TestCall { //SecurityTokenProvider.instance.set("0e2c7963-8d3e-4ea6-a56d-ffda530dd0fa-98187548"); //token costantino 9ca79556-54b0-4bbf-ab0f-151ae326f4cf-98187548 - SecurityTokenProvider.instance.set("d9431600-9fef-41a7-946d-a5b402de30d6-98187548"); + SecurityTokenProvider.instance.set("ae1208f0-210d-47c9-9b24-d3f2dfcce05f-98187548"); ScopeProvider.instance.set("/gcube"); } @@ -182,7 +182,7 @@ public class TestCall { @Test public void download() throws Exception{ ItemManagerClient client = AbstractPlugin.item().build(); - StreamDescriptor streamDescr = client.download("07cd8d55-a35b-4445-9680-c98f158c55de"); + StreamDescriptor streamDescr = client.download("6875651d-6510-4b82-a0f3-cc3356c1a143"); File output = Files.createTempFile("down", streamDescr.getFileName()).toFile(); try (BufferedInputStream bi = new BufferedInputStream(streamDescr.getStream()); FileOutputStream fo = new FileOutputStream(output)){ byte[] buf = new byte[2048]; @@ -193,8 +193,6 @@ public class TestCall { } System.out.println("file written "+output.getAbsolutePath()); - - } @Test