Extracted RequestInfo interface and renamed class to ServerRequestInfo

This commit is contained in:
Luca Frosini 2023-05-02 17:24:42 +02:00
parent 119ae14dfa
commit a3c0f725c6
6 changed files with 66 additions and 30 deletions

View File

@ -46,7 +46,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.base.properties.PropertyElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.requests.RequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.gcube.informationsystem.resourceregistry.types.CachedType;
import org.gcube.informationsystem.resourceregistry.types.TypesCache;
@ -403,7 +403,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
/* Add first these key to provide an order in Json */
RequestInfo requestInfo = RequestUtility.getRequestInfo().get();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
List<String> keysToAddFirst = new ArrayList<>();
keysToAddFirst.add(Element.TYPE_PROPERTY);

View File

@ -5,16 +5,16 @@ package org.gcube.informationsystem.resourceregistry.requests;
*/
public class RequestUtility {
private static final InheritableThreadLocal<RequestInfo> requestInfo = new InheritableThreadLocal<RequestInfo>() {
private static final InheritableThreadLocal<ServerRequestInfo> requestInfo = new InheritableThreadLocal<ServerRequestInfo>() {
@Override
protected RequestInfo initialValue() {
return new RequestInfo();
protected ServerRequestInfo initialValue() {
return new ServerRequestInfo();
}
};
public static InheritableThreadLocal<RequestInfo> getRequestInfo() {
public static InheritableThreadLocal<ServerRequestInfo> getRequestInfo() {
return requestInfo;
}

View File

@ -6,6 +6,7 @@ import javax.ws.rs.core.UriInfo;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo;
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -13,9 +14,9 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class RequestInfo {
public class ServerRequestInfo implements RequestInfo {
protected static Logger logger = LoggerFactory.getLogger(RequestInfo.class);
protected static Logger logger = LoggerFactory.getLogger(ServerRequestInfo.class);
protected UriInfo uriInfo;
@ -41,13 +42,43 @@ public class RequestInfo {
*/
protected boolean includeContexts;
public RequestInfo() {
public ServerRequestInfo() {
this.includeMeta = false;
this.allMeta = false;
this.hierarchicalMode = false;
this.includeContexts = false;
}
/**
* -check if the user is allowed to set such a request
* @param queryParameterKey requested query parameter
* @return true is the user is allowed to set such a request
*/
public boolean isAllowed(String queryParameterKey) {
switch (queryParameterKey) {
case InstancePath.INCLUDE_META_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
return true;
case InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
return true;
case InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
return true;
case InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
return true;
default:
break;
}
return true;
}
/**
* Set the parameter if the user is allowed otherwise the default is maintained
* @param queryParameterKey requested query parameter
@ -55,31 +86,30 @@ public class RequestInfo {
* @return the value of variable corresponding the request parameter independetly if
* the value has been set.
*/
protected boolean setIfAllowed(String queryParameterKey, boolean bool) {
public boolean setIfAllowed(String queryParameterKey, boolean bool) {
boolean toBeSet = bool && isAllowed(queryParameterKey);
switch (queryParameterKey) {
case InstancePath.INCLUDE_META_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
includeMeta = bool;
includeMeta = toBeSet;
return includeMeta;
case InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
allMeta = bool;
allMeta = toBeSet;
return allMeta;
case InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
includeContexts = bool;
includeContexts = toBeSet;
return includeContexts;
case InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER:
// TODO check is the user has the role to request such parameter
hierarchicalMode = bool;
hierarchicalMode = toBeSet;
return hierarchicalMode;
default:
break;
}
return false;
}
@ -111,38 +141,44 @@ public class RequestInfo {
checkQueryParameter(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER);
}
@Override
public boolean includeMeta() {
return includeMeta;
}
@Override
public void setIncludeMeta(boolean includeMeta) {
this.includeMeta = includeMeta;
}
@Override
public boolean allMeta() {
return allMeta;
}
@Override
public void setAllMeta(boolean allMeta) {
this.allMeta = allMeta;
}
@Override
public boolean isHierarchicalMode() {
return hierarchicalMode;
}
@Override
public void setHierarchicalMode(boolean hierarchicalMode) {
this.hierarchicalMode = hierarchicalMode;
}
@Override
public boolean includeContexts() {
return includeContexts;
}
@Override
public void setIncludeContexts(boolean includeContexts) {
this.includeContexts = includeContexts;
}
}

View File

@ -40,7 +40,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Res
import org.gcube.informationsystem.resourceregistry.queries.Query;
import org.gcube.informationsystem.resourceregistry.queries.QueryImpl;
import org.gcube.informationsystem.resourceregistry.queries.json.JsonQuery;
import org.gcube.informationsystem.resourceregistry.requests.RequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.gcube.informationsystem.resourceregistry.types.TypeManagement;
import org.gcube.informationsystem.types.TypeMapper;
@ -244,7 +244,7 @@ public class Access extends BaseRest {
logger.info("Requested query (Raw {}):\n{}", raw, query);
CalledMethodProvider.instance.set("graphQuery");
RequestInfo requestInfo = RequestUtility.getRequestInfo().get();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
if(raw) {
// TODO Check if the role allow to request raw data
requestInfo.checkQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER);

View File

@ -3,7 +3,7 @@ package org.gcube.informationsystem.resourceregistry.rest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import org.gcube.informationsystem.resourceregistry.requests.RequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -19,7 +19,7 @@ public class BaseRest {
protected UriInfo uriInfo;
public BaseRest() {
RequestInfo requestInfo = new RequestInfo();
ServerRequestInfo requestInfo = new ServerRequestInfo();
requestInfo.setUriInfo(uriInfo);
RequestUtility.getRequestInfo().set(requestInfo);

View File

@ -34,7 +34,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Fac
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.requests.RequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.serialization.ElementMapper;
@ -112,7 +112,7 @@ public class ERManagementTest extends ContextTest {
@Before
public void before() throws Exception {
RequestInfo requestInfo = RequestUtility.getRequestInfo().get();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
requestInfo.setIncludeMeta(true);
requestInfo.setAllMeta(true);
}
@ -471,7 +471,7 @@ public class ERManagementTest extends ContextTest {
EService eService = null;
try {
eService = createEService();
RequestInfo requestInfo = RequestUtility.getRequestInfo().get();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
requestInfo.setIncludeMeta(true);
requestInfo.setAllMeta(true);