From c4786572ab18c960e7a09ca7de3819c12d7825d5 Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Tue, 26 Mar 2024 17:39:59 +0100 Subject: [PATCH] paginated bean result --- .../service/idm/rest/UsersSocialAPI.java | 25 +++--- .../org/gcube/service/rest/ResponseBean.java | 6 +- .../service/rest/ResponseBeanPaginated.java | 76 +++++++++++++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/gcube/service/rest/ResponseBeanPaginated.java diff --git a/src/main/java/org/gcube/service/idm/rest/UsersSocialAPI.java b/src/main/java/org/gcube/service/idm/rest/UsersSocialAPI.java index 147f8d4..7b9e2b8 100644 --- a/src/main/java/org/gcube/service/idm/rest/UsersSocialAPI.java +++ b/src/main/java/org/gcube/service/idm/rest/UsersSocialAPI.java @@ -1,7 +1,6 @@ package org.gcube.service.idm.rest; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,6 +22,7 @@ import org.gcube.service.idm.IdMManager; import org.gcube.service.idm.controller.KKUserClient; import org.gcube.service.idm.serializers.ContextSerializator; import org.gcube.service.rest.ResponseBean; +import org.gcube.service.rest.ResponseBeanPaginated; import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.vomanagement.usermanagement.UserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; @@ -49,9 +49,10 @@ import jakarta.ws.rs.core.MediaType; // }) public class UsersSocialAPI { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class); - private static final String NOT_USER_TOKEN_CONTEXT_USED = "User's information can only be retrieved through a user token (not qualified)"; - private static final List GLOBAL_ROLES_ALLOWED_BY_LOCAL_CALL_METHOD = Arrays.asList("DataMiner-Manager", - "Quota-Manager"); + + // private static final List GLOBAL_ROLES_ALLOWED_BY_LOCAL_CALL_METHOD = + // Arrays.asList("DataMiner-Manager", + // "Quota-Manager"); @GET @Path("/get-usernames-by-role") @@ -59,9 +60,14 @@ public class UsersSocialAPI { public Response getUsernamesByRole( @QueryParam("role-name") String roleName, @QueryParam("firstResult") @DefaultValue("0") @Min(value = 0, message = "from cannot be negative") int firstResult, - @QueryParam("maxResults") @DefaultValue("100000") @Min(value = 0, message = "quantity cannot be negative") int maxResults) { + @QueryParam("maxResults") @DefaultValue("10000") @Min(value = 0, message = "quantity cannot be negative") int maxResults) { Status status = Status.OK; - ResponseBean responseBean = new ResponseBean(); + ResponseBean responseBean; + if (firstResult > 0 || maxResults > 0) { + responseBean = new ResponseBeanPaginated(firstResult, maxResults); + } else { + responseBean = new ResponseBean(); + } List usernames = new ArrayList(); try { List users = KKUserClient.searchUsersByRole(roleName, firstResult, maxResults); @@ -83,10 +89,10 @@ public class UsersSocialAPI { return Response.serverError().build(); } catch (Exception e) { logger.error("Unable to retrieve users with the requested role", e); - // responseBean.setMessage(e.getMessage()); + responseBean.setMessage(e.getMessage()); status = Status.INTERNAL_SERVER_ERROR; + return Response.status(status).entity(responseBean).build(); } - return Response.status(status).entity(responseBean).build(); } @GET @@ -156,7 +162,7 @@ public class UsersSocialAPI { // return Response.status(status).entity(responseBean).build(); // } - UserManager lr_userManager = null; //UserManagerWSBuilder.getInstance().getUserManager(); + UserManager lr_userManager = null; // UserManagerWSBuilder.getInstance().getUserManager(); try { GCubeUser user = lr_userManager.getUserByUsername(userId); @@ -223,6 +229,7 @@ public class UsersSocialAPI { responseBean.setResult(userprofiile); responseBean.setSuccess(true); } catch (Exception e) { + logger.error("Unable to retrieve user's profile", e); responseBean.setMessage(e.getMessage()); status = Status.INTERNAL_SERVER_ERROR; diff --git a/src/main/java/org/gcube/service/rest/ResponseBean.java b/src/main/java/org/gcube/service/rest/ResponseBean.java index b908a8d..8565c53 100644 --- a/src/main/java/org/gcube/service/rest/ResponseBean.java +++ b/src/main/java/org/gcube/service/rest/ResponseBean.java @@ -12,16 +12,16 @@ public class ResponseBean implements Serializable { /** * The result of the request: true if it succeeded, false otherwise */ - private boolean success; + protected boolean success; /** * An error message if something wrong happened, null/empty otherwise */ - private String message; + protected String message; /** * The result object of the request */ - private Object result; + protected Object result; public ResponseBean() { super(); diff --git a/src/main/java/org/gcube/service/rest/ResponseBeanPaginated.java b/src/main/java/org/gcube/service/rest/ResponseBeanPaginated.java new file mode 100644 index 0000000..bf17e08 --- /dev/null +++ b/src/main/java/org/gcube/service/rest/ResponseBeanPaginated.java @@ -0,0 +1,76 @@ +package org.gcube.service.rest; + +import java.io.Serializable; + +/** + * Response bean + * + */ +public class ResponseBeanPaginated extends ResponseBean { + + private static final long serialVersionUID = -2725238162673879658L; + + protected Integer firstResult; + + protected Integer maxResults; + + public Integer getFirstResult() { + return firstResult; + } + + public void setFirstResult(Integer firstResult) { + this.firstResult = firstResult; + } + + public Integer getMaxResults() { + return maxResults; + } + + public void setMaxResults(Integer maxResults) { + this.maxResults = maxResults; + } + + public ResponseBeanPaginated() { + super(); + } + + // /** + // * @param success + // * @param message + // * @param result + // */ + // public ResponseBeanPaginated(boolean success, String message, Object result) + // { + // super(success, message, result); + // } + + /** + * @param firstResult + * @param maxResults + */ + public ResponseBeanPaginated(Integer firstResult, Integer maxResults) { + this.firstResult = firstResult; + this.maxResults = maxResults; + } + + /** + * @param success + * @param message + * @param result + * @param firstResult + * @param maxResults + */ + public ResponseBeanPaginated(boolean success, String message, Object result, Integer firstResult, + Integer maxResults) { + super(success, message, result); + this.firstResult = firstResult; + this.maxResults = maxResults; + } + + @Override + public String toString() { + return "ResponseBean [success=" + success + + ", message=" + message + ", result=" + result + ", firstResult=" + firstResult + ", maxResults=" + + maxResults + "]"; + } +}