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.exceptions.BackendGenericError; import org.gcube.common.storagehub.model.exceptions.IdNotFoundException; import org.gcube.common.storagehub.model.exceptions.StorageHubException; import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException; public class DefaultUserManager extends DefaultManagerClient implements UserManagerClient { public DefaultUserManager(ProxyDelegate delegate) { super(delegate); } @Override public void createUser(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("user", userId); formData.add("password", userId+"pwd"); GXInboundResponse response = myManager.post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED)); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else if (response.getHTTPCode()==403) throw new UserNotAuthorizedException("the call to this method is not allowed for the user"); 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 removeUser(String userId) throws StorageHubException { Call call = new Call() { @Override public Void call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager; try { GXInboundResponse response = myManager.path(userId).delete(); if (response.isErrorResponse()) { if (response.hasException()) throw response.getException(); else if (response.getHTTPCode()==403) throw new UserNotAuthorizedException("the call to this method is not allowed for the user"); else throw new BackendGenericError(); } }catch (Exception e) { e.printStackTrace(); throw e; } return null; } }; try { delegate.make(call); return; }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } @Override public List getUsers() 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 if (response.getHTTPCode()==403) throw new UserNotAuthorizedException("the call to this method is not allowed for the user"); 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 boolean exists(String user) throws StorageHubException { Call call = new Call() { @Override public Boolean call(GXWebTargetAdapterRequest manager) throws Exception { GXWebTargetAdapterRequest myManager = manager.path(user); GXInboundResponse response = myManager.get(); if (response.isErrorResponse()) { if (response.hasException()) if (response.getException().getClass().equals(IdNotFoundException.class)) return false; else throw response.getException(); else if (response.getHTTPCode()==403) throw new UserNotAuthorizedException("the call to this method is not allowed for the user"); else throw new BackendGenericError(); } return true; } }; try { return delegate.make(call); }catch(StorageHubException e) { throw e; }catch(Exception e1) { throw new RuntimeException(e1); } } }