diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/requests/ServerRequestInfo.java b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/ServerRequestInfo.java index e9d1c71..cede9bd 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/requests/ServerRequestInfo.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/requests/ServerRequestInfo.java @@ -50,6 +50,14 @@ public class ServerRequestInfo extends BaseRequestInfo implements RequestInfo { case InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER: // TODO check is the user has the role to request such parameter return true; + + case InstancePath.LIMIT_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + return true; + + case InstancePath.OFFSET_QUERY_PARAMETER: + // TODO check is the user has the role to request such parameter + return true; default: break; @@ -62,27 +70,34 @@ public class ServerRequestInfo extends BaseRequestInfo implements RequestInfo { * Set the parameter if the user is allowed otherwise the default is maintained * @param queryParameterKey requested query parameter * @param bool the value to set - * @return the value of variable corresponding the request parameter independetly if - * the value has been set. + * @return the value of variable corresponding the request parameter + * independently if the value has been set. */ public boolean setIfAllowed(String queryParameterKey, boolean bool) { - boolean toBeSet = bool && isAllowed(queryParameterKey); - + switch (queryParameterKey) { case InstancePath.INCLUDE_META_QUERY_PARAMETER: - includeMeta = toBeSet; + if(isAllowed(queryParameterKey)) { + includeMeta = bool; + } return includeMeta; case InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER: - allMeta = toBeSet; + if(isAllowed(queryParameterKey)) { + allMeta = bool; + } return allMeta; case InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER: - includeContexts = toBeSet; + if(isAllowed(queryParameterKey)) { + includeContexts = bool; + } return includeContexts; case InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER: - hierarchicalMode = toBeSet; + if(isAllowed(queryParameterKey)) { + hierarchicalMode = bool; + } return hierarchicalMode; default: @@ -92,7 +107,36 @@ public class ServerRequestInfo extends BaseRequestInfo implements RequestInfo { return false; } - public void checkQueryParameter(String queryParameterKey) { + /** + * Set the parameter if the user is allowed otherwise the default is maintained + * @param queryParameterKey requested query parameter + * @param integer the int value to set + * @return the value of variable corresponding the request parameter independently if + * the value has been set. + */ + public int setIfAllowed(String queryParameterKey, int integer) { + + switch (queryParameterKey) { + case InstancePath.LIMIT_QUERY_PARAMETER: + if(isAllowed(queryParameterKey)) { + limit = integer; + } + return limit; + + case InstancePath.OFFSET_QUERY_PARAMETER: + if(isAllowed(queryParameterKey)) { + offset = integer; + } + return offset; + + default: + break; + } + + return -1; + } + + public void checkBooleanQueryParameter(String queryParameterKey) { try { List queryParameterList = uriInfo.getQueryParameters().get(queryParameterKey); if(queryParameterList!=null && queryParameterList.size()>0) { @@ -105,27 +149,42 @@ public class ServerRequestInfo extends BaseRequestInfo implements RequestInfo { } } + public void checkIntegerQueryParameter(String queryParameterKey) { + try { + List queryParameterList = uriInfo.getQueryParameters().get(queryParameterKey); + if(queryParameterList!=null && queryParameterList.size()>0) { + String intString = queryParameterList.get(0); + int integer = Integer.valueOf(intString); + setIfAllowed(queryParameterKey, integer); + } + }catch (Throwable t) { + logger.warn("Unable to properly set the Hierarchical Mode is set", t.getMessage()); + } + } + + public void setUriInfo(UriInfo uriInfo) { this.uriInfo = uriInfo; } public void checkAllQueryParameters() { checkIncludeQueryParameters(); - checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); } public void checkIncludeQueryParameters() { checkIncludeAllMetaQueryParameters(); - checkQueryParameter(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER); + checkBooleanQueryParameter(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER); } public void checkIncludeAllMetaQueryParameters() { - checkQueryParameter(InstancePath.INCLUDE_META_QUERY_PARAMETER); - checkQueryParameter(InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER); + checkBooleanQueryParameter(InstancePath.INCLUDE_META_QUERY_PARAMETER); + checkBooleanQueryParameter(InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER); } - - - + public void checkLimitOffset() { + checkIntegerQueryParameter(InstancePath.LIMIT_QUERY_PARAMETER); + checkIntegerQueryParameter(InstancePath.OFFSET_QUERY_PARAMETER); + } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java index d2e1e1d..f87315e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java @@ -81,7 +81,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); ContextManagement contextManagement = new ContextManagement(); return contextManagement.all(false); @@ -104,7 +104,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); ContextManagement contextManagement = new ContextManagement(); contextManagement.setUUID(UUID.fromString(uuid)); @@ -127,7 +127,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); TypeManagement typeManagement = new TypeManagement(); typeManagement.setTypeName(type); @@ -152,7 +152,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); return queryTemplateManagement.all(false); @@ -172,7 +172,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); queryTemplateManagement.setName(queryTemplateName); @@ -239,7 +239,7 @@ public class Access extends BaseRest { setAccountingMethod(Method.EXIST, InstancesManager.INSTANCE); ServerRequestInfo serverRequestInfo = initRequestInfo(); - serverRequestInfo.checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); ElementManagement erManagement = ElementManagementUtility.getERManagement(type); @@ -338,7 +338,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); if(raw) { // TODO Check if the role allow to request raw data - serverRequestInfo.checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); }else { serverRequestInfo.checkAllQueryParameters(); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java index 939c8d8..a26f381 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java @@ -48,7 +48,7 @@ public class ContextManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); ContextManagement contextManagement = new ContextManagement(); return contextManagement.all(false); @@ -73,7 +73,7 @@ public class ContextManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); ContextManagement contextManagement = new ContextManagement(); contextManagement.setUUID(UUID.fromString(uuid)); @@ -98,7 +98,7 @@ public class ContextManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); ContextManagement contextManagement = new ContextManagement(); contextManagement.setUUID(UUID.fromString(uuid)); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java index 31b5015..1e67a2e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -80,7 +80,7 @@ public class InstancesManager extends BaseRest { setAccountingMethod(Method.EXIST, InstancesManager.INSTANCE); ServerRequestInfo serverRequestInfo = initRequestInfo(); - serverRequestInfo.checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); @SuppressWarnings("rawtypes") ElementManagement erManagement = ElementManagementUtility.getERManagement(type); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/QueryTemplateManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/QueryTemplateManager.java index 892d91f..3af2b70 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/QueryTemplateManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/QueryTemplateManager.java @@ -46,7 +46,7 @@ public class QueryTemplateManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); return queryTemplateManagement.all(false); @@ -93,7 +93,7 @@ public class QueryTemplateManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); queryTemplateManagement.setName(queryTemplateName); @@ -115,7 +115,7 @@ public class QueryTemplateManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement(); queryTemplateManagement.setName(queryTemplateName); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java index a9ed281..6566a57 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/TypeManager.java @@ -54,7 +54,7 @@ public class TypeManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); TypeManagement schemaManagement = new TypeManagement(); schemaManagement.setTypeName(typeName); @@ -80,7 +80,7 @@ public class TypeManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.setAllMeta(true); - serverRequestInfo.checkQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); + serverRequestInfo.checkBooleanQueryParameter(TypePath.INCLUDE_META_QUERY_PARAMETER); TypeManagement schemaManagement = new TypeManagement(); schemaManagement.setTypeName(type);