diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java index eb36b62..91b0479 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java @@ -1,5 +1,6 @@ package org.gcube.informationsystem.resourceregistry.schema; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.util.HashMap; import java.util.List; @@ -9,12 +10,16 @@ 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.Element; +import org.gcube.informationsystem.base.reference.IdentifiableElement; +import org.gcube.informationsystem.model.reference.properties.Metadata; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.types.TypeMapper; @@ -37,6 +42,63 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC protected Map headers; + /** + * 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; + + 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 queryParams) throws UnsupportedEncodingException { + addIncludeMeta(queryParams); + addIncludeAllMeta(queryParams); + } + + private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{ + Map queryParams = new HashMap<>(); + return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams); + } + + private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map queryParams) throws UnsupportedEncodingException{ + if(queryParams==null) { + queryParams = new HashMap<>(); + } + addOptionalQueryParameters(queryParams); + 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)); + } + } + @Override public void addHeader(String name, String value) { headers.put(name, value); @@ -54,6 +116,8 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC public ResourceRegistrySchemaClientImpl(String address) { this.address = address; this.headers = new HashMap<>(); + this.includeMeta = false; + this.allMeta = false; } @Override @@ -83,6 +147,8 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART); gxHTTPStringRequest.path(typeDefinitionObj.getName()); + includeAdditionalQueryParameters(gxHTTPStringRequest); + HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition); String c = HTTPUtility.getResponse(String.class, httpURLConnection); @@ -162,7 +228,7 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC if(polymorphic != null) { parameters.put(TypePath.POLYMORPHIC_QUERY_PARAMETER, polymorphic.toString()); } - gxHTTPStringRequest.queryParams(parameters); + includeAdditionalQueryParameters(gxHTTPStringRequest, parameters); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String json = HTTPUtility.getResponse(String.class, httpURLConnection);