Added missing query parameters to cache

This commit is contained in:
Luca Frosini 2023-05-05 12:14:19 +02:00
parent a7fcf00921
commit 4a2c56c18d
1 changed files with 71 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.informationsystem.resourceregistry.contexts;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
@ -12,13 +13,16 @@ import org.gcube.common.context.ContextUtility;
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.properties.Metadata;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
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.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.serialization.ElementMapper;
@ -40,7 +44,64 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
protected Map<String, String> 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;
protected ContextCache contextCache;
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 {
addIncludeMeta(queryParams);
addIncludeAllMeta(queryParams);
}
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
Map<String,String> queryParams = new HashMap<>();
return includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams);
}
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 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() {
@ -72,6 +133,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) {
this.address = address;
this.headers = new HashMap<>();
this.includeMeta = false;
this.allMeta = false;
if(sharedContextCache) {
contextCache = ContextCache.getInstance();
}else {
@ -96,6 +159,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String all = HTTPUtility.getResponse(String.class, httpURLConnection);
@ -135,6 +200,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
@ -272,6 +339,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid);
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
@ -299,6 +368,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid.toString());
includeAdditionalQueryParameters(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);