Compare commits

...

12 Commits

7 changed files with 137 additions and 45 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
target
.classpath
.project
/.DS_Store
/bin/

View File

@ -2,6 +2,16 @@ 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]
## [v4.1.0]
- Enhanced gcube-bom version

10
pom.xml
View File

@ -10,7 +10,7 @@
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-context-client</artifactId>
<version>4.1.0</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>
@ -30,7 +30,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>2.2.0</version>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -50,10 +50,6 @@
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-utility-sg3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@ -74,7 +70,7 @@
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.1.0, 3.0.0-SNAPSHOT)</version>
<version>[2.2.0, 3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
</dependencies>

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

@ -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;
@ -13,24 +14,24 @@ 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.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.properties.Header;
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.ContextPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.UUIDManager;
import org.slf4j.Logger;
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);
@ -42,12 +43,63 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
protected Map<String, String> headers;
protected ContextCache contextCache;
private void addOptionalQueryParameters(Map<String,String> queryParams) throws UnsupportedEncodingException {
addIncludeMeta(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{
addIncludeMeta(queryParams, includeMeta);
}
private void addIncludeMeta(Map<String,String> queryParams, boolean includeMeta) throws UnsupportedEncodingException{
if(includeMeta) {
queryParams.put(ContextPath.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(ContextPath.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(ContextPath.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);
}
};
@ -73,6 +125,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) {
this.address = address;
this.headers = new HashMap<>();
this.includeMeta = false;
if(sharedContextCache) {
contextCache = ContextCache.getInstance();
}else {
@ -90,13 +143,30 @@ 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);
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);
@ -117,14 +187,19 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
return contextCache.getContexts();
}
@Override
public ContextCache getContextCache() {
return contextCache;
}
protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Header header = context.getHeader();
if(header == null) {
header = new HeaderImpl(UUID.randomUUID());
context.setHeader(header);
UUID uuid = context.getID();
if(uuid == null) {
uuid = UUIDManager.getInstance().generateValidUUID();
context.setID(uuid);
}
UUID uuid = context.getHeader().getUUID();
String contextString = ElementMapper.marshal(context);
@ -136,6 +211,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);
@ -221,7 +298,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException {
return read(context.getHeader().getUUID());
return read(context.getID());
}
@Override
@ -235,7 +312,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
throw new RuntimeException(e);
}
forceCacheRefresh();
Context c = contextCache.getContextByUUID(context.getHeader().getUUID());
Context c = contextCache.getContextByUUID(context.getID());
if(c!=null){
context = c;
}else {
@ -273,6 +350,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);
@ -292,7 +371,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
String contextString = ElementMapper.marshal(context);
logger.trace("Going to update {}", contextString);
UUID uuid = context.getHeader().getUUID();
UUID uuid = context.getID();
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
@ -300,6 +379,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);
@ -347,7 +428,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public boolean delete(Context context) throws ContextNotFoundException, ResourceRegistryException {
return delete(context.getHeader().getUUID());
return delete(context.getID());
}
@Override

View File

@ -15,7 +15,7 @@ import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
@ -117,7 +117,7 @@ public class ContextTest {
}
public static String getUser() {
String user = Header.UNKNOWN_USER;
String user = Metadata.UNKNOWN_USER;
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {

View File

@ -40,32 +40,32 @@ public class ResourceRegistryContextClientTest extends ContextTest {
public static final String CTX_NAME_B = "B";
public static final String CTX_NAME_C = "C";
public static void checkHeader(IdentifiableElement er, UUID uuid, boolean create) {
Assert.assertTrue(er.getHeader() != null);
Assert.assertTrue(er.getHeader().getUUID() != null);
public static void checkUUIDAndMetadata(IdentifiableElement er, UUID uuid, boolean create) {
Assert.assertTrue(er.getMetadata() != null);
Assert.assertTrue(er.getID() != null);
if(uuid != null) {
Assert.assertTrue(er.getHeader().getUUID().compareTo(uuid) == 0);
Assert.assertTrue(er.getID().compareTo(uuid) == 0);
}
String user = getUser();
Assert.assertTrue(er.getHeader().getLastUpdateBy().compareTo(user) == 0);
Assert.assertTrue(er.getMetadata().getLastUpdateBy().compareTo(user) == 0);
if(create) {
Assert.assertTrue(er.getHeader().getCreatedBy().compareTo(user) == 0);
Assert.assertTrue(er.getHeader().getCreationTime().compareTo(er.getHeader().getLastUpdateTime()) == 0);
Assert.assertTrue(er.getMetadata().getCreatedBy().compareTo(user) == 0);
Assert.assertTrue(er.getMetadata().getCreationTime().compareTo(er.getMetadata().getLastUpdateTime()) == 0);
} else {
Assert.assertTrue(er.getHeader().getCreationTime().before(er.getHeader().getLastUpdateTime()));
Assert.assertTrue(er.getMetadata().getCreationTime().before(er.getMetadata().getLastUpdateTime()));
}
}
protected void assertions(Context pre, Context post, boolean checkParent, boolean create) {
if(checkParent) {
if(pre.getHeader() != null) {
checkHeader(post, pre.getHeader().getUUID(), create);
if(pre.getMetadata() != null) {
checkUUIDAndMetadata(post, pre.getID(), create);
} else {
checkHeader(post, null, create);
checkUUIDAndMetadata(post, null, create);
}
}
@ -79,9 +79,9 @@ public class ResourceRegistryContextClientTest extends ContextTest {
protected Context read(UUID uuid) throws ResourceRegistryException, IOException {
Context c = resourceRegistryContextClient.read(uuid);
Assert.assertTrue(c.getHeader() != null);
Assert.assertTrue(c.getHeader().getUUID() != null);
Assert.assertTrue(c.getHeader().getUUID().compareTo(uuid) == 0);
Assert.assertTrue(c.getMetadata() != null);
Assert.assertTrue(c.getID() != null);
Assert.assertTrue(c.getID().compareTo(uuid) == 0);
return c;
}
@ -105,7 +105,7 @@ public class ResourceRegistryContextClientTest extends ContextTest {
}
protected boolean delete(Context context) throws ResourceRegistryException {
return delete(context.getHeader().getUUID());
return delete(context.getID());
}
protected void invalidCreate(Context context) throws ResourceRegistryException, IOException {
@ -317,7 +317,7 @@ public class ResourceRegistryContextClientTest extends ContextTest {
delete(contextA5);
} catch(ContextNotFoundException e) {
logger.debug("The context with uuid {} was not found. (Was already deleted)",
contextA5.getHeader().getUUID());
contextA5.getID());
}
delete(contextB3);
@ -441,11 +441,11 @@ public class ResourceRegistryContextClientTest extends ContextTest {
for(Context c : contexts) {
UUID uuid = c.getHeader().getUUID();
UUID uuid = c.getID();
if(c.getParent()!=null) {
IsParentOf isParentOf = c.getParent();
Context parentContext = isParentOf.getSource();
UUID parentUUID = parentContext.getHeader().getUUID();
UUID parentUUID = parentContext.getID();
Assert.assertEquals(parentContext, contextCache.getContextByUUID(parentUUID));
List<IsParentOf> children = parentContext.getChildren();
boolean found = false;
@ -456,9 +456,9 @@ public class ResourceRegistryContextClientTest extends ContextTest {
}
}
Assert.assertTrue(found);
logger.debug("{} : {} (parent {} : {})", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid), parentUUID, contextCache.getContextFullNameByUUID(parentUUID));
logger.debug("{} : {} (parent {} : {})", c.getID(), contextCache.getContextFullNameByUUID(uuid), parentUUID, contextCache.getContextFullNameByUUID(parentUUID));
}else {
logger.debug("{} : {}", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid));
logger.debug("{} : {}", c.getID(), contextCache.getContextFullNameByUUID(uuid));
}
}
@ -466,7 +466,7 @@ public class ResourceRegistryContextClientTest extends ContextTest {
logger.debug("Current context : {}", currentContext);
for(Context c : contexts) {
UUID uuid = c.getHeader().getUUID();
UUID uuid = c.getID();
Context context = resourceRegistryContextClient.read(uuid);
String fullName = ContextCache.getInstance().getContextFullNameByUUID(uuid);
logger.debug("{} - {} : {}", uuid, fullName, context);