getByPath method added

This commit is contained in:
lucio 2020-03-05 15:19:45 +01:00
parent 44e4ebd403
commit 6b013ec305
7 changed files with 78 additions and 8 deletions

View File

@ -18,7 +18,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId>
<version>1.0.8</version>
<version>1.1.0-SNAPSHOT</version>
<name>storagehub-client-library</name>
<dependencyManagement>

View File

@ -177,6 +177,19 @@ public class FolderContainer extends ItemContainer<FolderItem>{
return new ListResolver((onlyType, includeHidden, excludes) -> itemclient.findChildrenByNamePattern(itemId, namePattern , excludes), itemclient);
}
/**
*
* returns the children of this {@FolderContainer} that matches the name pattern
*
* @return {@ListResolver}
* @throws {@InvalidItemException}
* @throws {@UserNotAuthorizedException} if user is not authorized to read this folder
*/
public OpenResolver openByRelativePath(String relativePath) throws StorageHubException {
final String id = this.itemId;
return new OpenResolver(itemclient.getByRelativePath(id, relativePath), itemclient);
}
/**
*
* returns the items that matches the name pattern searching recursively on all subfolders

View File

@ -37,4 +37,15 @@ public class OpenResolver {
return new URLContainer(itemclient, (ExternalLink)item);
else throw new RuntimeException("this item is not a File");
}
public ItemContainer<?> resolve(){
if (item instanceof FolderItem)
return this.asFolder();
if (item instanceof AbstractFileItem)
return this.asFile();
if (item instanceof ExternalLink)
return this.asURL();
else return this.asItem();
}
}

View File

@ -28,7 +28,7 @@ public class StorageHubClient {
Item item = itemclient.get(id);
return new OpenResolver(item, itemclient);
}
public FolderContainer openVREFolder() {
return new FolderContainer(itemclient, wsClient.getVreFolder());
}

View File

@ -435,7 +435,48 @@ public class DefaultItemManager implements ItemManagerClient {
throw new RuntimeException(e1);
}
}
@Override
public Item getByRelativePath(String id, String path, String... excludeNodes) throws StorageHubException {
Call<GXWebTargetAdapterRequest, ItemWrapper<Item>> call = new Call<GXWebTargetAdapterRequest, ItemWrapper<Item>>() {
@Override
public ItemWrapper<Item> call(GXWebTargetAdapterRequest manager) throws Exception {
Objects.requireNonNull(id, "id cannot be null");
Objects.requireNonNull(path, "path cannot be null");
GXWebTargetAdapterRequest myManager = manager.path(id).path("path");
Map<String, Object[]> params = new HashMap<>();
params.put("path",new String[] {path});
if (excludeNodes !=null && excludeNodes.length>0)
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());
}
ItemWrapper<Item> item = response.getSource().readEntity(ItemWrapper.class);
return item;
}
};
try {
ItemWrapper<Item> result = delegate.make(call);
return result.getItem();
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
@Override
public Item getRootSharedFolder(String id) throws StorageHubException {
Call<GXWebTargetAdapterRequest, ItemWrapper<Item>> call = new Call<GXWebTargetAdapterRequest, ItemWrapper<Item>>() {

View File

@ -36,6 +36,8 @@ public interface ItemManagerClient {
Item get(String id, String ... excludeNodes) throws StorageHubException;
Item getByRelativePath(String id, String path, String ... excludeNodes) throws StorageHubException;
StreamDescriptor download(String id, String... excludeNodes) throws StorageHubException;
String uploadFile(InputStream stream, String parentId, String fileName, String description) throws StorageHubException;

View File

@ -31,8 +31,8 @@ public class Items {
@BeforeClass
public static void setUp(){
SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548");
ScopeProvider.instance.set("/gcube");
SecurityTokenProvider.instance.set("52e1492d-4bd1-4058-946f-d2fe5059ddc4-843339462");
ScopeProvider.instance.set("/d4science.research-infrastructures.eu");
}
@ -127,19 +127,22 @@ public class Items {
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open("a5bcf8b8-d149-42c2-92be-7f671223eba4").asFile().downloadSpecificVersion("1.2");
StreamDescriptor streamDescr = shc.open("1104d6fc-b2ab-4915-b480-cb60257320a4").asFile().download();
long start = System.currentTimeMillis();
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];
byte[] buf = new byte[2046];
int read = -1;
while ((read=bi.read(buf))!=-1) {
fo.write(buf, 0, read);
}
}
System.out.println("file written "+output.getAbsolutePath());
System.out.println("file written "+output.getAbsolutePath()+" in "+(System.currentTimeMillis()-start));
}