Compare commits
23 Commits
Author | SHA1 | Date |
---|---|---|
Luca Frosini | c7bc13c359 | |
luca.frosini | ef5b530800 | |
luca.frosini | adc1827da1 | |
luca.frosini | 5a2d5cf4e8 | |
luca.frosini | 689b6fe2e6 | |
Luca Frosini | e75102d462 | |
Luca Frosini | 4a2c56c18d | |
Luca Frosini | a7fcf00921 | |
Luca Frosini | d8d2a1a2d6 | |
Luca Frosini | e3918f8f24 | |
Luca Frosini | cda204abcb | |
Luca Frosini | cab174141a | |
Luca Frosini | 0deeb0a05a | |
Luca Frosini | f445b1765e | |
Luca Frosini | 18c3d69d22 | |
Luca Frosini | a61912147c | |
Luca Frosini | 2084b6d913 | |
Luca Frosini | 277f9603ce | |
Luca Frosini | c529b03d97 | |
Luca Frosini | 82fa4ec559 | |
Luca Frosini | 035c7f66e9 | |
Luca Frosini | 1e20d87c13 | |
Luca Frosini | 64edec2349 |
|
@ -1,3 +1,5 @@
|
|||
target
|
||||
.classpath
|
||||
.project
|
||||
/.DS_Store
|
||||
/bin/
|
||||
|
|
18
CHANGELOG.md
18
CHANGELOG.md
|
@ -2,6 +2,24 @@ 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
|
||||
- Added usage of common-utility to overcome issues with different Smartgears version (i.e. 3 and 4)
|
||||
- Added the possibility for a client to add additional HTTP headers
|
||||
- Added the possibility to create a client instance by specifying context
|
||||
|
||||
|
||||
## [v4.0.1]
|
||||
|
||||
- Uniformed raised exception [#21993]
|
||||
|
|
19
pom.xml
19
pom.xml
|
@ -10,7 +10,7 @@
|
|||
|
||||
<groupId>org.gcube.information-system</groupId>
|
||||
<artifactId>resource-registry-context-client</artifactId>
|
||||
<version>4.0.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>
|
||||
|
||||
|
@ -30,7 +30,7 @@
|
|||
<dependency>
|
||||
<groupId>org.gcube.distribution</groupId>
|
||||
<artifactId>gcube-bom</artifactId>
|
||||
<version>2.1.0</version>
|
||||
<version>2.4.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -38,14 +38,6 @@
|
|||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.resources.discovery</groupId>
|
||||
<artifactId>ic-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.information-system</groupId>
|
||||
<artifactId>resource-registry-api</artifactId>
|
||||
|
@ -62,6 +54,7 @@
|
|||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -74,5 +67,11 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-utils</artifactId>
|
||||
<version>[2.2.0, 3.0.0-SNAPSHOT)</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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;
|
||||
|
@ -13,6 +14,10 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.Cont
|
|||
*/
|
||||
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;
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
||||
import org.gcube.informationsystem.resourceregistry.api.rest.ServiceInstance;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -14,20 +11,25 @@ import org.slf4j.LoggerFactory;
|
|||
public class ResourceRegistryContextClientFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
||||
|
||||
public static String getCurrentContextFullName() {
|
||||
String token = SecurityTokenProvider.instance.get();
|
||||
AuthorizationEntry authorizationEntry = null;
|
||||
try {
|
||||
authorizationEntry = org.gcube.common.authorization.client.Constants.authorizationService().get(token);
|
||||
} catch(Exception e) {
|
||||
return ScopeProvider.instance.get();
|
||||
}
|
||||
return authorizationEntry.getContext();
|
||||
|
||||
public static String getResourceRegistryURL() {
|
||||
String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
|
||||
return address;
|
||||
}
|
||||
|
||||
public static String getResourceRegistryURL(String context) {
|
||||
String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME);
|
||||
return address;
|
||||
}
|
||||
|
||||
public static ResourceRegistryContextClient create() {
|
||||
String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
|
||||
String address = getResourceRegistryURL();
|
||||
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||
return new ResourceRegistryContextClientImpl(address);
|
||||
}
|
||||
|
||||
public static ResourceRegistryContextClient create(String context) {
|
||||
String address = getResourceRegistryURL(context);
|
||||
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||
return new ResourceRegistryContextClientImpl(address);
|
||||
}
|
||||
|
|
|
@ -1,55 +1,141 @@
|
|||
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;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
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.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.utils.ElementMapper;
|
||||
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);
|
||||
|
||||
private static final String ACCEPT_HTTP_HEADER_KEY = "Accept";
|
||||
private static final String CONTENT_TYPE_HTTP_HEADER_KEY = "Content-Type";
|
||||
|
||||
protected final String address;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void addHeader(String name, String value) {
|
||||
headers.put(name, value);
|
||||
}
|
||||
|
||||
protected GXHTTPStringRequest getGXHTTPStringRequest() {
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPUtility.getGXHTTPStringRequest(address);
|
||||
gxHTTPStringRequest.from(this.getClass().getSimpleName());
|
||||
for(String name : headers.keySet()) {
|
||||
gxHTTPStringRequest.header(name, headers.get(name));
|
||||
}
|
||||
return gxHTTPStringRequest;
|
||||
}
|
||||
|
||||
public ResourceRegistryContextClientImpl(String address) {
|
||||
this(address, true);
|
||||
}
|
||||
|
||||
public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) {
|
||||
this.address = address;
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
this.headers = new HashMap<>();
|
||||
this.includeMeta = false;
|
||||
if(sharedContextCache) {
|
||||
contextCache = ContextCache.getInstance();
|
||||
}else {
|
||||
contextCache = new ContextCache();
|
||||
}
|
||||
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
||||
}
|
||||
|
||||
private void forceCacheRefresh() {
|
||||
try {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
contextCache.cleanCache();
|
||||
contextCache.refreshContextsIfNeeded();
|
||||
}catch (Exception e) {
|
||||
|
@ -57,14 +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);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
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);
|
||||
|
||||
|
@ -82,30 +184,35 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
|
||||
@Override
|
||||
public List<Context> all() throws ResourceRegistryException {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
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);
|
||||
|
||||
logger.trace("Going to create {}", contextString);
|
||||
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid.toString());
|
||||
|
||||
includeAdditionalQueryParameters(gxHTTPStringRequest);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
|
||||
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||
|
||||
|
@ -154,9 +261,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
public boolean existFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid);
|
||||
|
||||
|
@ -192,13 +298,12 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
|
||||
@Override
|
||||
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException {
|
||||
return read(context.getHeader().getUUID());
|
||||
return read(context.getID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
Context context = ContextCache.getInstance().getContextByUUID(uuid);;
|
||||
Context context = contextCache.getContextByUUID(uuid);;
|
||||
if(context == null) {
|
||||
String contextJson = readFromServer(uuid.toString());
|
||||
try {
|
||||
|
@ -207,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 {
|
||||
|
@ -219,29 +324,9 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
|
||||
@Override
|
||||
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
|
||||
String contextFullName = ResourceRegistryContextClientFactory.getCurrentContextFullName();
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
String contextFullName = ContextUtility.getCurrentContextFullName();
|
||||
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
|
||||
Context context = null;
|
||||
if(uuid == null) {
|
||||
String contextJson = readFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART);
|
||||
try {
|
||||
context = ElementMapper.unmarshal(Context.class, contextJson);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
contextCache.cleanCache();
|
||||
contextCache.refreshContextsIfNeeded();
|
||||
Context c = contextCache.getContextByUUID(context.getHeader().getUUID());
|
||||
if(c!=null){
|
||||
context = c;
|
||||
}else {
|
||||
logger.error("Current Context is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", contextFullName);
|
||||
}
|
||||
}else {
|
||||
context = contextCache.getContextByUUID(uuid);
|
||||
}
|
||||
return context;
|
||||
return read(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -260,12 +345,13 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
public String readFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid);
|
||||
|
||||
includeAdditionalQueryParameters(gxHTTPStringRequest);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
||||
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||
|
||||
|
@ -285,15 +371,16 @@ 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 = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid.toString());
|
||||
|
||||
includeAdditionalQueryParameters(gxHTTPStringRequest);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
|
||||
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||
|
||||
|
@ -341,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
|
||||
|
@ -353,9 +440,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
logger.trace("Going to delete {} with UUID {}", Context.NAME, uuid);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
|
||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid);
|
||||
|
||||
|
@ -364,8 +450,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
|
||||
boolean deleted = true;
|
||||
|
||||
|
||||
|
||||
logger.info("{} with UUID {} {}", Context.NAME, uuid,
|
||||
deleted ? " successfully deleted" : "was NOT deleted");
|
||||
return deleted;
|
||||
|
@ -377,9 +461,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
try {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
contextCache.cleanCache();
|
||||
contextCache.refreshContextsIfNeeded();
|
||||
forceCacheRefresh();
|
||||
}catch (Exception e) {
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,15 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.authorization.library.utils.Caller;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.common.authorization.utils.secret.JWTSecret;
|
||||
import org.gcube.common.authorization.utils.secret.Secret;
|
||||
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.Metadata;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -22,77 +23,107 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||
|
||||
protected static Properties properties;
|
||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||
protected static final String CONFIG_INI_FILENAME = "config.ini";
|
||||
|
||||
public static final String PARENT_DEFAULT_TEST_SCOPE;
|
||||
public static final String DEFAULT_TEST_SCOPE;
|
||||
public static final String ALTERNATIVE_TEST_SCOPE;
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
|
||||
public static final String GCUBE;
|
||||
public static final String DEVNEXT;
|
||||
public static final String NEXTNEXT;
|
||||
public static final String DEVSEC;
|
||||
public static final String DEVVRE;
|
||||
|
||||
protected static final Properties properties;
|
||||
|
||||
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id";
|
||||
protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret";
|
||||
|
||||
protected static final String clientID;
|
||||
protected static final String clientSecret;
|
||||
|
||||
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
|
||||
public static final String RESOURCE_REGISTRY_URL;
|
||||
|
||||
static {
|
||||
properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||
GCUBE = "/gcube";
|
||||
DEVNEXT = GCUBE + "/devNext";
|
||||
NEXTNEXT = DEVNEXT + "/NextNext";
|
||||
DEVSEC = GCUBE + "/devsec";
|
||||
DEVVRE = DEVSEC + "/devVRE";
|
||||
|
||||
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
|
||||
DEFAULT_TEST_SCOPE = DEVNEXT;
|
||||
ALTERNATIVE_TEST_SCOPE = NEXTNEXT;
|
||||
|
||||
properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
||||
try {
|
||||
// load the properties file
|
||||
properties.load(input);
|
||||
} catch(IOException e) {
|
||||
|
||||
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
||||
clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY);
|
||||
|
||||
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// PARENT_DEFAULT_TEST_SCOPE = "/pred4s"
|
||||
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "/preprod";
|
||||
// ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE";
|
||||
|
||||
|
||||
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
|
||||
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
|
||||
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
|
||||
|
||||
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
|
||||
|
||||
}
|
||||
|
||||
public static void set(Secret secret) throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
SecretManager secretManager = new SecretManager();
|
||||
secretManager.addSecret(secret);
|
||||
SecretManagerProvider.instance.set(secretManager);
|
||||
SecretManagerProvider.instance.get().set();
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
Secret secret = getSecretByContextName(fullContextName);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
|
||||
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||
ScopeProvider.instance.set(context);
|
||||
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
|
||||
return tr;
|
||||
}
|
||||
|
||||
public static Secret getSecretByContextName(String context) throws Exception {
|
||||
TokenResponse tr = getJWTAccessToken(context);
|
||||
Secret secret = new JWTSecret(tr.getAccessToken());
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static void setContext(String token) throws Exception {
|
||||
Secret secret = getSecret(token);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
private static Secret getSecret(String token) throws Exception {
|
||||
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static String getUser() {
|
||||
String user = Metadata.UNKNOWN_USER;
|
||||
try {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
|
||||
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
|
||||
|
||||
}
|
||||
|
||||
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
String context = authorizationEntry.getContext();
|
||||
logger.info("Context of token {} is {}", token, context);
|
||||
return context;
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
|
||||
String token = ContextTest.properties.getProperty(fullContextName);
|
||||
setContext(token);
|
||||
}
|
||||
|
||||
private static void setContext(String token) throws ObjectNotFound, Exception {
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
||||
String qualifier = authorizationEntry.getQualifier();
|
||||
Caller caller = new Caller(clientInfo, qualifier);
|
||||
AuthorizationProvider.instance.set(caller);
|
||||
ScopeProvider.instance.set(getCurrentScope(token));
|
||||
return user;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
|
@ -102,8 +133,7 @@ public class ContextTest {
|
|||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecurityTokenProvider.instance.reset();
|
||||
ScopeProvider.instance.reset();
|
||||
SecretManagerProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,21 +7,16 @@ import java.util.Map;
|
|||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.gcube.informationsystem.contexts.impl.entities.ContextImpl;
|
||||
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
||||
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
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.ContextException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.utils.ElementMapper;
|
||||
import org.gcube.informationsystem.serialization.ElementMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -45,54 +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 String getUser() {
|
||||
String user = Header.UNKNOWN_USER;
|
||||
try {
|
||||
String token = SecurityTokenProvider.instance.get();
|
||||
if(token != null) {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
if(authorizationEntry != null) {
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
String clientId = clientInfo.getId();
|
||||
if(clientId != null && clientId.compareTo("") != 0) {
|
||||
user = clientId;
|
||||
} else {
|
||||
throw new Exception("Username null or empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,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;
|
||||
}
|
||||
|
||||
|
@ -132,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 {
|
||||
|
@ -344,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);
|
||||
|
@ -468,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;
|
||||
|
@ -483,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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -493,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);
|
||||
|
|
Loading…
Reference in New Issue