Added support for paginated results

This commit is contained in:
luca.frosini 2023-11-09 19:04:36 +01:00
parent 5a2d5cf4e8
commit adc1827da1
4 changed files with 63 additions and 20 deletions

View File

@ -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]

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-context-client</artifactId>
<version>4.1.1</version>
<version>4.2.0-SNAPSHOT</version>
<name>Resource Registry Context Client</name>
<description>Resource Registry Context Client is a library designed to interact with Resource Registry Context APIs</description>

View File

@ -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<Context> all() throws ResourceRegistryException;
public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException;

View File

@ -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<String, String> 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<String,String> queryParams) throws UnsupportedEncodingException {
addIncludeMeta(queryParams);
}
@ -76,16 +63,44 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
private void addIncludeMeta(Map<String,String> queryParams) throws UnsupportedEncodingException{
addIncludeMeta(queryParams, includeMeta);
}
private void addIncludeMeta(Map<String,String> queryParams, boolean includeMeta) throws UnsupportedEncodingException{
if(includeMeta) {
queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta));
}
}
/*
private void addOffset(Map<String,String> queryParams) throws UnsupportedEncodingException{
addOffset(queryParams, offset);
}
*/
private void addOffset(Map<String,String> queryParams, Integer offset) throws UnsupportedEncodingException{
if(offset!=null) {
queryParams.put(AccessPath.OFFSET_QUERY_PARAMETER, offset.toString());
}
}
/*
private void addLimit(Map<String,String> queryParams) throws UnsupportedEncodingException{
addLimit(queryParams, limit);
}
*/
private void addLimit(Map<String,String> queryParams, Integer limit) throws UnsupportedEncodingException{
if(limit!=null) {
queryParams.put(AccessPath.LIMIT_QUERY_PARAMETER, limit.toString());
}
}
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@Override
public List<Context> renew() throws ResourceRegistryException {
return getAllContextFromServer();
return getAllContextFromServer(true, 0, BaseRequestInfo.UNBOUNDED_LIMIT);
}
};
@ -129,14 +144,29 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
protected List<Context> 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<Context> getAllContextFromServer() throws ResourceRegistryException {
return getAllContextFromServer(includeMeta, offset, limit);
}
protected List<Context> 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<String,String> 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();