package org.gcube.common.storagehub.client.proxies; import java.util.List; 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.acls.AccessType; import org.gcube.common.storagehub.model.exceptions.BackendGenericError; import org.gcube.common.storagehub.model.exceptions.StorageHubException; public class DefaultGroupManager implements GroupManagerClient { private final ProxyDelegate delegate; public DefaultGroupManager(ProxyDelegate delegate) { this.delegate = delegate; } @Override public void addUserToGroup(String userId, String groupId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; MultivaluedMap formData = new MultivaluedHashMap(); formData.add("userId", userId); GXInboundResponse response = myManager.path(groupId).path("users").put(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); return; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public void removeUserFromGroup(String userId, String groupId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.path(groupId).path("users").path(userId).delete(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); return; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public List getGroups() throws StorageHubException { Call> call = new Call>() { @Override public List call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.get(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return response.getSource().readEntity(List.class); } }; try { List users = delegate.make(call); return users; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public void removeGroup(String groupId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.path(groupId).delete(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); return; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public void createGroup(String groupId, AccessType accessType) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; MultivaluedMap formData = new MultivaluedHashMap(); formData.add("group", groupId); formData.add("accessType", accessType); GXInboundResponse response = myManager.post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); return; }catch(Exception e) { throw new RuntimeException(e); } } @Override public List getUsersOfGroup(String groupId) throws StorageHubException { Call> call = new Call>() { @Override public List call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.path(groupId).path("users").get(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return response.getSource().readEntity(List.class); } }; try { List users = delegate.make(call); return users; }catch(Exception e) { throw new RuntimeException(e); } } @Override public void addAdmin(String groupId, String userId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; MultivaluedMap formData = new MultivaluedHashMap(); formData.add("userId", userId); GXInboundResponse response = myManager.path(groupId).path("admins").put(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); }catch(Exception e) { throw new RuntimeException(e); } } @Override public void removeAdmin(String groupId, String userId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.path(groupId).path("admins").path(userId).delete(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return null; } }; try { delegate.make(call); return; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public List getAdmins(String groupId) throws StorageHubException { Call> call = new Call>() { @Override public List call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; GXInboundResponse response = myManager.path(groupId).path("admins").get(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else throw new BackendGenericError(); } return response.getSource().readEntity(List.class); } }; try { List users = delegate.make(call); return users; }catch(Exception e) { throw new RuntimeException(e); } } }