From adc1827da1aef87673baff0f0ce4c36dd7a61255 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Thu, 9 Nov 2023 19:04:36 +0100 Subject: [PATCH] Added support for paginated results --- CHANGELOG.md | 5 ++ pom.xml | 2 +- .../ResourceRegistryContextClient.java | 3 + .../ResourceRegistryContextClientImpl.java | 73 ++++++++++++++----- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d0cc32..155aabf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for Resource Registry Context Client +## [v4.2.0-SNAPSHOT] + +- Added support for paginated results [#24648] + + ## [v4.1.1] - Migrated code to reorganized E/R format [#24992] diff --git a/pom.xml b/pom.xml index 22c37cc..59c44fd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.information-system resource-registry-context-client - 4.1.1 + 4.2.0-SNAPSHOT Resource Registry Context Client Resource Registry Context Client is a library designed to interact with Resource Registry Context APIs diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java index de80890..b2bad7f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.UUID; import org.gcube.informationsystem.contexts.reference.entities.Context; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; 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; @@ -15,6 +16,8 @@ public interface ResourceRegistryContextClient { public void addHeader(String name, String value); + public ContextCache getContextCache(); + public List all() throws ResourceRegistryException; public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java index b4a3440..510871e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java @@ -14,13 +14,13 @@ import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.http.GXHTTPUtility; 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.request.BaseRequestInfo; 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; @@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -public class ResourceRegistryContextClientImpl implements ResourceRegistryContextClient { +public class ResourceRegistryContextClientImpl extends BaseRequestInfo implements ResourceRegistryContextClient { private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientImpl.class); @@ -43,21 +43,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex protected Map headers; - /** - * Track if the client must request to include {@link Metadata} - */ - protected boolean includeMeta; - protected ContextCache contextCache; - public boolean includeMeta() { - return includeMeta; - } - - public void setIncludeMeta(boolean includeMeta) { - this.includeMeta = includeMeta; - } - private void addOptionalQueryParameters(Map queryParams) throws UnsupportedEncodingException { addIncludeMeta(queryParams); } @@ -76,16 +63,44 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } private void addIncludeMeta(Map queryParams) throws UnsupportedEncodingException{ + addIncludeMeta(queryParams, includeMeta); + } + + private void addIncludeMeta(Map queryParams, boolean includeMeta) throws UnsupportedEncodingException{ if(includeMeta) { queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta)); } } + /* + private void addOffset(Map queryParams) throws UnsupportedEncodingException{ + addOffset(queryParams, offset); + } + */ + + private void addOffset(Map queryParams, Integer offset) throws UnsupportedEncodingException{ + if(offset!=null) { + queryParams.put(AccessPath.OFFSET_QUERY_PARAMETER, offset.toString()); + } + } + + /* + private void addLimit(Map queryParams) throws UnsupportedEncodingException{ + addLimit(queryParams, limit); + } + */ + + private void addLimit(Map queryParams, Integer limit) throws UnsupportedEncodingException{ + if(limit!=null) { + queryParams.put(AccessPath.LIMIT_QUERY_PARAMETER, limit.toString()); + } + } + protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { @Override public List renew() throws ResourceRegistryException { - return getAllContextFromServer(); + return getAllContextFromServer(true, 0, BaseRequestInfo.UNBOUNDED_LIMIT); } }; @@ -129,14 +144,29 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } } - protected List getAllContextFromServer() throws ResourceRegistryException { + + /** + * It reads all the contexts from server. + * The cache used for contexts is bypassed and not updated. + * @return All Contexts read from server + * @throws ResourceRegistryException + */ + public List getAllContextFromServer() throws ResourceRegistryException { + return getAllContextFromServer(includeMeta, offset, limit); + } + + protected List getAllContextFromServer(boolean includeMeta, Integer offset, Integer limit) throws ResourceRegistryException { try { - logger.trace("Going to read {} with UUID {}", Context.NAME); + logger.info("Going to read all {}s", Context.NAME); GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest(); gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART); - includeAdditionalQueryParameters(gxHTTPStringRequest); + Map parameters = new HashMap<>(); + addIncludeMeta(parameters, includeMeta); + addOffset(parameters, offset); + addLimit(parameters, limit); + gxHTTPStringRequest.queryParams(parameters); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String all = HTTPUtility.getResponse(String.class, httpURLConnection); @@ -158,6 +188,11 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex return contextCache.getContexts(); } + @Override + public ContextCache getContextCache() { + return contextCache; + } + protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException { try { UUID uuid = context.getID();