Using RequestInfo

This commit is contained in:
Luca Frosini 2023-05-02 17:24:11 +02:00
parent 22a6538683
commit 6b8b2e780a
2 changed files with 80 additions and 34 deletions

View File

@ -28,16 +28,13 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isr
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface ResourceRegistryPublisher {
public interface ResourceRegistryPublisher extends RequestInfo {
public boolean isHierarchicalMode();
public void setHierarchicalMode(boolean hierarchicalMode);
/**
* Use {@link #includeContexts()} instead
* @return
@ -52,10 +49,6 @@ public interface ResourceRegistryPublisher {
@Deprecated
public void setIncludeContextsInHeader(boolean includeContexts);
public boolean includeContexts();
public void includeContexts(boolean includeContexts);
public void addHeader(String name, String value);

View File

@ -10,10 +10,12 @@ import java.util.UUID;
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.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.ERElement;
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.resourceregistry.api.contexts.ContextCache;
@ -60,9 +62,27 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
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
@ -84,7 +104,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
@Deprecated
@Override
public void setIncludeContextsInHeader(boolean includeContexts) {
includeContexts(includeContexts);
setIncludeContexts(includeContexts);
}
@Override
@ -93,42 +113,69 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
}
@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 GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest)
throws UnsupportedEncodingException {
return includeAdditionalQueryParameters(gxHTTPStringRequest, null);
private void addOptionalQueryParameters(Map<String,String> queryParams) throws UnsupportedEncodingException {
addHierarchicalMode(queryParams);
addIncludeContexts(queryParams);
addIncludeMeta(queryParams);
addIncludeAllMeta(queryParams);
}
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest,
Map<String, String> queryParams) throws UnsupportedEncodingException {
gxHTTPStringRequest = checkHierarchicalMode(gxHTTPStringRequest, queryParams);
return checkIncludeContextsInInstance(gxHTTPStringRequest, queryParams);
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
Map<String,String> queryParams = new HashMap<>();
return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams);
}
private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest,
Map<String, String> queryParams) throws UnsupportedEncodingException {
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
if(queryParams==null) {
queryParams = new HashMap<>();
}
addOptionalQueryParameters(queryParams);
return gxHTTPStringRequest.queryParams(queryParams);
}
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() {
@ -310,6 +357,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
@ -403,7 +452,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
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);
@ -543,6 +594,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);