Using RequestInfo

This commit is contained in:
Luca Frosini 2023-05-02 17:24:03 +02:00
parent 047e0d448d
commit c933cc3f85
2 changed files with 80 additions and 26 deletions

View File

@ -21,16 +21,13 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.Cont
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo;
import org.gcube.informationsystem.types.reference.Type;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface ResourceRegistryClient {
public boolean isHierarchicalMode();
public void setHierarchicalMode(boolean hierarchicalMode);
public interface ResourceRegistryClient extends RequestInfo {
/**
* Use {@link #includeContexts()} instead
@ -46,10 +43,6 @@ public interface ResourceRegistryClient {
@Deprecated
public void setIncludeContextsInHeader(boolean includeContexts);
public boolean includeContexts();
public void includeContexts(boolean includeContexts);
public void addHeader(String name, String value);

View File

@ -15,11 +15,13 @@ import org.gcube.common.gxhttp.reference.GXConnection;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.http.GXHTTPUtility;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.ERElement;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
@ -61,9 +63,27 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
protected Map<String, String> headers;
/**
* Track if the client must request the hierarchicalMode
*/
protected boolean hierarchicalMode;
/**
* Track if the client must request to include contexts
*/
protected boolean includeContexts;
/**
* Track if the client must request to include {@link Metadata}
*/
protected boolean includeMeta;
/**
* Track if the client must request to include {@link Metadata} in all
* {@link IdentifiableElement} or just in the root instance
*/
protected boolean allMeta;
protected ContextCache contextCache;
@Override
@ -85,7 +105,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Deprecated
@Override
public void setIncludeContextsInHeader(boolean includeContexts) {
includeContexts(includeContexts);
setIncludeContexts(includeContexts);
}
@Override
@ -94,37 +114,69 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
}
@Override
public void includeContexts(boolean includeContexts) {
public void setIncludeContexts(boolean includeContexts) {
this.includeContexts = includeContexts;
}
public boolean includeMeta() {
return includeMeta;
}
public void setIncludeMeta(boolean includeMeta) {
this.includeMeta = includeMeta;
}
public boolean allMeta() {
return allMeta;
}
public void setAllMeta(boolean allMeta) {
this.allMeta = allMeta;
}
private void addOptionalQueryParameters(Map<String,String> queryParams) throws UnsupportedEncodingException {
addHierarchicalMode(queryParams);
addIncludeContexts(queryParams);
addIncludeMeta(queryParams);
addIncludeAllMeta(queryParams);
}
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
return includeAdditionalQueryParameters(gxHTTPStringRequest, null);
Map<String,String> queryParams = new HashMap<>();
return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams);
}
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
gxHTTPStringRequest = checkHierarchicalMode(gxHTTPStringRequest, queryParams);
return checkIncludeContextsInInstance(gxHTTPStringRequest, queryParams);
if(queryParams==null) {
queryParams = new HashMap<>();
}
addOptionalQueryParameters(queryParams);
return gxHTTPStringRequest.queryParams(queryParams);
}
private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
private void addHierarchicalMode(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(hierarchicalMode) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode));
}
return gxHTTPStringRequest.queryParams(queryParams);
}
private GXHTTPStringRequest checkIncludeContextsInInstance(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
private void addIncludeContexts(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(includeContexts) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.INCLUDE_CONTEXTS_QUERY_PARAMETER, Boolean.toString(includeContexts));
}
return gxHTTPStringRequest.queryParams(queryParams);
}
private void addIncludeMeta(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(includeMeta) {
queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta));
}
}
private void addIncludeAllMeta(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(allMeta) {
queryParams.put(AccessPath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER, Boolean.toString(allMeta));
}
}
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@ -158,6 +210,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
this.address = address;
this.hierarchicalMode = false;
this.includeContexts = false;
this.includeMeta = false;
this.allMeta = false;
this.headers = new HashMap<>();
if(sharedContextCache) {
contextCache = ContextCache.getInstance();
@ -427,7 +481,9 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
Map<String,String> queryParams = new HashMap<>();
addHierarchicalMode(queryParams);
gxHTTPStringRequest.queryParams(queryParams);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
HTTPUtility.getResponse(String.class, httpURLConnection);
@ -507,7 +563,12 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
parameters.put(AccessPath.Q_QUERY_PARAMETER, query);
parameters.put(AccessPath.RAW_QUERY_PARAMETER, Boolean.toString(raw));
includeAdditionalQueryParameters(gxHTTPStringRequest, parameters);
if(raw) {
addHierarchicalMode(parameters);
gxHTTPStringRequest.queryParams(parameters);
} else {
includeAdditionalQueryParameters(gxHTTPStringRequest, parameters);
}
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();