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.IsRelatedToAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToNotFoundException; 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.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo;
/** /**
* @author Luca Frosini (ISTI - CNR) * @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 * Use {@link #includeContexts()} instead
* @return * @return
@ -52,10 +49,6 @@ public interface ResourceRegistryPublisher {
@Deprecated @Deprecated
public void setIncludeContextsInHeader(boolean includeContexts); public void setIncludeContextsInHeader(boolean includeContexts);
public boolean includeContexts();
public void includeContexts(boolean includeContexts);
public void addHeader(String name, String value); 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.reference.GXConnection;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.http.GXHTTPUtility; 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.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.model.reference.ERElement;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; 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.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
@ -60,9 +62,27 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
protected Map<String, String> headers; protected Map<String, String> headers;
/**
* Track if the client must request the hierarchicalMode
*/
protected boolean hierarchicalMode; protected boolean hierarchicalMode;
/**
* Track if the client must request to include contexts
*/
protected boolean includeContexts; 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; protected ContextCache contextCache;
@Override @Override
@ -84,7 +104,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
@Deprecated @Deprecated
@Override @Override
public void setIncludeContextsInHeader(boolean includeContexts) { public void setIncludeContextsInHeader(boolean includeContexts) {
includeContexts(includeContexts); setIncludeContexts(includeContexts);
} }
@Override @Override
@ -93,42 +113,69 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
@Override @Override
public void includeContexts(boolean includeContexts) { public void setIncludeContexts(boolean includeContexts) {
this.includeContexts = 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 { private void addOptionalQueryParameters(Map<String,String> queryParams) throws UnsupportedEncodingException {
return includeAdditionalQueryParameters(gxHTTPStringRequest, null); addHierarchicalMode(queryParams);
addIncludeContexts(queryParams);
addIncludeMeta(queryParams);
addIncludeAllMeta(queryParams);
} }
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
Map<String, String> queryParams) throws UnsupportedEncodingException { Map<String,String> queryParams = new HashMap<>();
gxHTTPStringRequest = checkHierarchicalMode(gxHTTPStringRequest, queryParams); return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams);
return checkIncludeContextsInInstance(gxHTTPStringRequest, queryParams);
} }
private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
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(hierarchicalMode) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode)); queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode));
} }
return gxHTTPStringRequest.queryParams(queryParams);
} }
private GXHTTPStringRequest checkIncludeContextsInInstance(GXHTTPStringRequest gxHTTPStringRequest, private void addIncludeContexts(Map<String,String> queryParams) throws UnsupportedEncodingException{
Map<String, String> queryParams) throws UnsupportedEncodingException {
if(includeContexts) { if(includeContexts) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.INCLUDE_CONTEXTS_QUERY_PARAMETER, Boolean.toString(includeContexts)); 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() { protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@ -310,6 +357,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString()); gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection); String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
@ -403,7 +452,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString()); gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest); Map<String,String> queryParams = new HashMap<>();
addHierarchicalMode(queryParams);
gxHTTPStringRequest.queryParams(queryParams);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
HTTPUtility.getResponse(String.class, httpURLConnection); HTTPUtility.getResponse(String.class, httpURLConnection);
@ -543,6 +594,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString()); gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection); String ret = HTTPUtility.getResponse(String.class, httpURLConnection);