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 002a024..d24d2fe 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 @@ -49,7 +49,7 @@ public class StorageHubClient { return new FolderContainer(itemclient, wsClient.getTrashFolder()); } - public void emptyTrash() { + public void emptyTrash() throws StorageHubException{ wsClient.emptyTrash(); } @@ -61,8 +61,12 @@ public class StorageHubClient { return wsClient.getTotalItemCount(); } - public GenericItemContainer restoreThrashItem(String itemId) { - return new GenericItemContainer(itemclient, wsClient.restoreFromTrash(itemId)); + public GenericItemContainer restoreThrashItem(String trashItemId) throws StorageHubException { + return new GenericItemContainer(itemclient, wsClient.restoreFromTrash(trashItemId, null)); + } + + public GenericItemContainer restoreThrashItem(String trashItemId, String destinationFolderId) throws StorageHubException { + return new GenericItemContainer(itemclient, wsClient.restoreFromTrash(trashItemId, destinationFolderId)); } public void createUserAccount(String userId) throws StorageHubException { 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 ee97e5a..bf06b6f 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 @@ -5,14 +5,19 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import org.gcube.common.clients.Call; import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest; import org.gcube.common.gxrest.response.inbound.GXInboundResponse; import org.gcube.common.storagehub.model.exceptions.BackendGenericError; +import org.gcube.common.storagehub.model.exceptions.StorageHubException; import org.gcube.common.storagehub.model.expressions.OrderField; import org.gcube.common.storagehub.model.expressions.SearchableItem; import org.gcube.common.storagehub.model.items.Item; @@ -246,7 +251,7 @@ public class DefaultWorkspaceManager implements WorkspaceManagerClient { } @Override - public void emptyTrash() { + public void emptyTrash() throws StorageHubException, BackendGenericError { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { @@ -266,19 +271,28 @@ public class DefaultWorkspaceManager implements WorkspaceManagerClient { }; try { delegate.make(call); - }catch(Exception e) { + }catch(StorageHubException e) { + throw e; + } catch (Throwable e) { throw new RuntimeException(e); } } @Override - public String restoreFromTrash(final String id) { + public String restoreFromTrash(String trashedItemid, String destinationFolderId) throws StorageHubException, BackendGenericError{ Call call = new Call() { @Override public String call(GXWebTargetAdapterRequest manager) throws Exception { + Objects.nonNull(trashedItemid); GXWebTargetAdapterRequest myManager = manager.path("trash").path("restore"); - GXInboundResponse response = myManager.put(Entity.text(id)); + MultivaluedMap formData = new MultivaluedHashMap(); + formData.add("trashedItemId", trashedItemid); + if (destinationFolderId !=null) + formData.add("destinationId", destinationFolderId); + + + GXInboundResponse response = myManager.put(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); if (response.isErrorResponse()) { if (response.hasException()) @@ -292,7 +306,9 @@ public class DefaultWorkspaceManager implements WorkspaceManagerClient { }; try { return delegate.make(call); - }catch(Exception e) { + }catch(StorageHubException e) { + throw e; + } catch (Throwable e) { throw new RuntimeException(e); } } 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 a841c3d..d53833e 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 @@ -2,6 +2,8 @@ package org.gcube.common.storagehub.client.proxies; import java.util.List; +import org.gcube.common.storagehub.model.exceptions.BackendGenericError; +import org.gcube.common.storagehub.model.exceptions.StorageHubException; import org.gcube.common.storagehub.model.expressions.SearchableItem; import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.query.Query; @@ -24,9 +26,9 @@ public interface WorkspaceManagerClient { List getRecentModifiedFilePerVre(); - String restoreFromTrash(String id); + String restoreFromTrash(String thrashedItemid, String destinationFolderId) throws StorageHubException, BackendGenericError; - void emptyTrash(); + void emptyTrash() throws StorageHubException, BackendGenericError; long getTotalItemCount(); 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 cfd771b..ddf8165 100644 --- a/src/test/java/org/gcube/data/access/fs/TestCall.java +++ b/src/test/java/org/gcube/data/access/fs/TestCall.java @@ -101,10 +101,11 @@ public class TestCall { } @Test - public void restore() { - + public void restore() throws Exception{ + ScopeProvider.instance.set("/gcube"); + SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548"); WorkspaceManagerClient client = AbstractPlugin.workspace().build(); - client.restoreFromTrash("82af9e1c-6cc7-4e16-bba5-9bec6545015a"); + System.out.println(client.restoreFromTrash("cd04ff61-e83f-482f-b59e-438c7be7013d", "f3d336cc-cd00-48ba-8339-2bffcbef825e")); } @Test