From 6b8b2e780a0372b33ad9310bf6ea94c29c0fd86c Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 2 May 2023 17:24:11 +0200 Subject: [PATCH] Using RequestInfo --- .../publisher/ResourceRegistryPublisher.java | 11 +- .../ResourceRegistryPublisherImpl.java | 103 +++++++++++++----- 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisher.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisher.java index 2479b6e..2c775f6 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisher.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisher.java @@ -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); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherImpl.java index 04a3066..bffe928 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherImpl.java @@ -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 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 queryParams) throws UnsupportedEncodingException { + addHierarchicalMode(queryParams); + addIncludeContexts(queryParams); + addIncludeMeta(queryParams); + addIncludeAllMeta(queryParams); } - - private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, - Map queryParams) throws UnsupportedEncodingException { - gxHTTPStringRequest = checkHierarchicalMode(gxHTTPStringRequest, queryParams); - return checkIncludeContextsInInstance(gxHTTPStringRequest, queryParams); + + private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{ + Map queryParams = new HashMap<>(); + return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams); } - - private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, - Map queryParams) throws UnsupportedEncodingException { + + private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map queryParams) throws UnsupportedEncodingException{ + if(queryParams==null) { + queryParams = new HashMap<>(); + } + addOptionalQueryParameters(queryParams); + return gxHTTPStringRequest.queryParams(queryParams); + } + + private void addHierarchicalMode(Map 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 queryParams) throws UnsupportedEncodingException { + + private void addIncludeContexts(Map 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 queryParams) throws UnsupportedEncodingException{ + if(includeMeta) { + queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta)); + } + } + + private void addIncludeAllMeta(Map 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 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);