Compare commits
40 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 | |
Luca Frosini | 0cb5a31376 | |
Luca Frosini | 38376902fa | |
Luca Frosini | 5b6503eece | |
Luca Frosini | 6cd9b2fd1f | |
Luca Frosini | d6bb89079b | |
Luca Frosini | a37236ba85 | |
Luca Frosini | 2b3ffcaf50 | |
Luca Frosini | 15140318ea | |
Luca Frosini | a98db71cb5 | |
Luca Frosini | eb42cc76db | |
Luca Frosini | af76f9ff03 | |
Luca Frosini | 825c0ee20d | |
Luca Frosini | 3d02dff158 | |
Luca Frosini | 323fe367e6 | |
Luca Frosini | 4e091a25de | |
Luca Frosini | 25b72dcc2d | |
Luca Frosini | d3763d854e |
|
@ -1,3 +1,5 @@
|
|||
target
|
||||
.classpath
|
||||
.project
|
||||
/.DS_Store
|
||||
/bin/
|
||||
|
|
23
CHANGELOG.md
23
CHANGELOG.md
|
@ -2,6 +2,29 @@ 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]
|
||||
- Client gets service URL using resource-registry-api lib utility [#23658]
|
||||
|
||||
|
||||
## [v4.0.0]
|
||||
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# Acknowledgments
|
||||
|
||||
The projects leading to this software have received funding from a series of European Union programmes including:
|
||||
|
||||
- the Sixth Framework Programme for Research and Technological Development
|
||||
- [DILIGENT](https://cordis.europa.eu/project/id/004260) (grant no. 004260).
|
||||
- the Seventh Framework Programme for research, technological development and demonstration
|
||||
- [D4Science](https://cordis.europa.eu/project/id/212488) (grant no. 212488);
|
||||
- [D4Science-II](https://cordis.europa.eu/project/id/239019) (grant no.239019);
|
||||
- [ENVRI](https://cordis.europa.eu/project/id/283465) (grant no. 283465);
|
||||
- [iMarine](https://cordis.europa.eu/project/id/283644) (grant no. 283644);
|
||||
- [EUBrazilOpenBio](https://cordis.europa.eu/project/id/288754) (grant no. 288754).
|
||||
- the H2020 research and innovation programme
|
||||
- [SoBigData](https://cordis.europa.eu/project/id/654024) (grant no. 654024);
|
||||
- [PARTHENOS](https://cordis.europa.eu/project/id/654119) (grant no. 654119);
|
||||
- [EGI-Engage](https://cordis.europa.eu/project/id/654142) (grant no. 654142);
|
||||
- [ENVRI PLUS](https://cordis.europa.eu/project/id/654182) (grant no. 654182);
|
||||
- [BlueBRIDGE](https://cordis.europa.eu/project/id/675680) (grant no. 675680);
|
||||
- [PerformFISH](https://cordis.europa.eu/project/id/727610) (grant no. 727610);
|
||||
- [AGINFRA PLUS](https://cordis.europa.eu/project/id/731001) (grant no. 731001);
|
||||
- [DESIRA](https://cordis.europa.eu/project/id/818194) (grant no. 818194);
|
||||
- [ARIADNEplus](https://cordis.europa.eu/project/id/823914) (grant no. 823914);
|
||||
- [RISIS 2](https://cordis.europa.eu/project/id/824091) (grant no. 824091);
|
||||
- [EOSC-Pillar](https://cordis.europa.eu/project/id/857650) (grant no. 857650);
|
||||
- [Blue Cloud](https://cordis.europa.eu/project/id/862409) (grant no. 862409);
|
||||
- [SoBigData-PlusPlus](https://cordis.europa.eu/project/id/871042) (grant no. 871042);
|
24
README.md
24
README.md
|
@ -45,26 +45,4 @@ open-source software toolkit used for building and operating Hybrid Data
|
|||
Infrastructures enabling the dynamic deployment of Virtual Research Environments
|
||||
by favouring the realisation of reuse oriented policies.
|
||||
|
||||
The projects leading to this software have received funding from a series of European Union programmes including:
|
||||
|
||||
- the Sixth Framework Programme for Research and Technological Development
|
||||
- DILIGENT (grant no. 004260).
|
||||
- the Seventh Framework Programme for research, technological development and demonstration
|
||||
- D4Science (grant no. 212488);
|
||||
- D4Science-II (grant no.239019);
|
||||
- ENVRI (grant no. 283465);
|
||||
- iMarine(grant no. 283644);
|
||||
- EUBrazilOpenBio (grant no. 288754).
|
||||
- the H2020 research and innovation programme
|
||||
- SoBigData (grant no. 654024);
|
||||
- PARTHENOS (grant no. 654119);
|
||||
- EGIEngage (grant no. 654142);
|
||||
- ENVRIplus (grant no. 654182);
|
||||
- BlueBRIDGE (grant no. 675680);
|
||||
- PerformFish (grant no. 727610);
|
||||
- AGINFRAplus (grant no. 731001);
|
||||
- DESIRA (grant no. 818194);
|
||||
- ARIADNEplus (grant no. 823914);
|
||||
- RISIS2 (grant no. 824091);
|
||||
|
||||
|
||||
The projects leading to this software have received funding from a series of European Union programmes see [FUNDING.md](FUNDING.md)
|
||||
|
|
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.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.0.1</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>
|
|
@ -1,121 +0,0 @@
|
|||
package org.gcube.informationsystem.resourceregistry.context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ResourceRegistryContextClientFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
||||
|
||||
protected static List<String> addresses;
|
||||
|
||||
static {
|
||||
addresses = new ArrayList<>();
|
||||
}
|
||||
|
||||
private static String FORCED_URL = null;
|
||||
|
||||
protected static void forceToURL(String url){
|
||||
FORCED_URL = url;
|
||||
}
|
||||
|
||||
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
private static String containsFormat = "$entry/@EntryName eq '%1s'";
|
||||
|
||||
|
||||
private static String serviceEndpointCategoryFormat = "$resource/Profile/Category/text() eq '%1s'";
|
||||
private static String serviceEndpointNameFormat = "$resource/Profile/Name/text() eq '%1s'";
|
||||
private static String serviceEndpointstatusFormat = "$resource/Profile/RunTime/Status/text() eq 'READY'";
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private static SimpleQuery queryForService(){
|
||||
return ICFactory.queryFor(GCoreEndpoint.class)
|
||||
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
|
||||
.addCondition(String.format(nameFormat, Constants.SERVICE_NAME))
|
||||
.addCondition(String.format(statusFormat))
|
||||
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
|
||||
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
|
||||
.setResult("$entry/text()");
|
||||
}
|
||||
|
||||
private static SimpleQuery queryForProxy(){
|
||||
return ICFactory.queryFor(ServiceEndpoint.class)
|
||||
.addCondition(String.format(serviceEndpointCategoryFormat, Constants.SERVICE_CLASS))
|
||||
.addCondition(String.format(serviceEndpointNameFormat, Constants.SERVICE_NAME))
|
||||
.addCondition(String.format(serviceEndpointstatusFormat))
|
||||
.addVariable("$entry","$resource/Profile/AccessPoint/Interface/Endpoint")
|
||||
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
|
||||
.setResult("$entry/text()");
|
||||
}
|
||||
|
||||
|
||||
protected static List<String> getAddresses(){
|
||||
List<String> addresses = new ArrayList<>();
|
||||
|
||||
try {
|
||||
SimpleQuery proxyQuery = queryForProxy();
|
||||
addresses = ICFactory.client().submit(proxyQuery);
|
||||
if(addresses==null || addresses.isEmpty()){
|
||||
throw new Exception("No ResourceRegistry Proxy Found");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.debug("{}. Looking for RunningInstance.", e.getMessage());
|
||||
SimpleQuery serviceQuery = queryForService();
|
||||
addresses = ICFactory.client().submit(serviceQuery);
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public static ResourceRegistryContextClient create() {
|
||||
String address = null;
|
||||
|
||||
if(FORCED_URL!=null){
|
||||
address = FORCED_URL;
|
||||
}else {
|
||||
|
||||
if(addresses==null || addresses.isEmpty()) {
|
||||
addresses = getAddresses();
|
||||
}
|
||||
|
||||
if(addresses==null || addresses.isEmpty()){
|
||||
String error = String.format("No %s:%s found in the current context %s", Constants.SERVICE_CLASS, Constants.SERVICE_NAME, getCurrentContextFullName());
|
||||
throw new RuntimeException(error);
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
int index = random.nextInt(addresses.size());
|
||||
address = addresses.get(index);
|
||||
}
|
||||
|
||||
return new ResourceRegistryContextClientImpl(address);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,23 +1,34 @@
|
|||
package org.gcube.informationsystem.resourceregistry.context;
|
||||
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.informationsystem.context.reference.entities.Context;
|
||||
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.context.ContextAlreadyPresentException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
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;
|
||||
|
||||
public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException;
|
||||
|
||||
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
public boolean exist(UUID uuid) throws ResourceRegistryException;
|
||||
|
||||
public boolean exist(String uuid) throws ResourceRegistryException;
|
||||
|
||||
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
|
@ -25,9 +36,9 @@ public interface ResourceRegistryContextClient {
|
|||
|
||||
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
public Context update(Context context) throws ResourceRegistryException;
|
||||
public Context update(Context context) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
public String update(String context) throws ResourceRegistryException;
|
||||
public String update(String context) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
public boolean delete(Context context) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
|
@ -35,6 +46,4 @@ public interface ResourceRegistryContextClient {
|
|||
|
||||
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException;
|
||||
|
||||
public List<Context> all() throws ResourceRegistryException;
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
||||
import org.gcube.informationsystem.resourceregistry.api.rest.ServiceInstance;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ResourceRegistryContextClientFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
||||
|
||||
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 = 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.context;
|
||||
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.informationsystem.context.reference.entities.Context;
|
||||
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.gcube.common.http.GXHTTPUtility;
|
||||
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
||||
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.context.ContextAlreadyPresentException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
||||
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,26 +143,76 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.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);
|
||||
|
||||
logger.debug("Got contexts are {}", Context.NAME, all);
|
||||
return ElementMapper.unmarshalList(Context.class, all);
|
||||
|
||||
} catch(ResourceRegistryException e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Context> all() throws ResourceRegistryException {
|
||||
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);
|
||||
|
||||
|
@ -122,15 +258,52 @@ 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 = getGXHTTPStringRequest();
|
||||
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
gxHTTPStringRequest.path(uuid);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
|
||||
HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||
|
||||
return true;
|
||||
} catch (NotFoundException e) {
|
||||
return false;
|
||||
} catch(ResourceRegistryException e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exist(String uuid) throws ResourceRegistryException {
|
||||
return exist(UUID.fromString(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exist(UUID uuid) throws ResourceRegistryException {
|
||||
try {
|
||||
read(uuid);
|
||||
return true;
|
||||
}catch (ContextNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
|
@ -139,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 {
|
||||
|
@ -151,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(AccessPath.CURRENT_CONTEXT);
|
||||
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
|
||||
|
@ -192,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);
|
||||
|
||||
|
@ -212,20 +366,21 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
}
|
||||
}
|
||||
|
||||
public String internalUpdate(Context context) throws ResourceRegistryException {
|
||||
public String internalUpdate(Context context) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
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);
|
||||
|
||||
|
@ -244,7 +399,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
}
|
||||
|
||||
@Override
|
||||
public Context update(Context context) throws ResourceRegistryException {
|
||||
public Context update(Context context) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
String res = internalUpdate(context);
|
||||
return ElementMapper.unmarshal(Context.class, res);
|
||||
|
@ -258,7 +413,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
}
|
||||
|
||||
@Override
|
||||
public String update(String context) throws ResourceRegistryException {
|
||||
public String update(String context) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
Context c = ElementMapper.unmarshal(Context.class, context);
|
||||
return internalUpdate(c);
|
||||
|
@ -273,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
|
||||
|
@ -285,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);
|
||||
|
||||
|
@ -296,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;
|
||||
|
@ -309,42 +461,11 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
|||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
try {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
contextCache.cleanCache();
|
||||
contextCache.refreshContextsIfNeeded();
|
||||
forceCacheRefresh();
|
||||
}catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected List<Context> getAllContextFromServer() 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);
|
||||
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
||||
String all = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||
|
||||
logger.debug("Got contexts are {}", Context.NAME, all);
|
||||
return ElementMapper.unmarshalList(Context.class, all);
|
||||
|
||||
} catch(ResourceRegistryException e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
// logger.trace("Error Creating {}", facet, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Context> all() throws ResourceRegistryException {
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
return contextCache.getContexts();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.resourceregistry.context;
|
||||
|
||||
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.scope.api.ScopeProvider;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||
|
||||
protected static Properties properties;
|
||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||
|
||||
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;
|
||||
|
||||
protected static final String REGISTRY_PROPERTIES_FILENAME = "registry.properties";
|
||||
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);
|
||||
|
||||
try {
|
||||
// load the properties file
|
||||
properties.load(input);
|
||||
} 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;
|
||||
|
||||
try {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
Properties registryProperties = new Properties();
|
||||
input = ContextTest.class.getClassLoader().getResourceAsStream(REGISTRY_PROPERTIES_FILENAME);
|
||||
try {
|
||||
// load the properties file
|
||||
registryProperties.load(input);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
RESOURCE_REGISTRY_URL = registryProperties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
|
||||
|
||||
if(RESOURCE_REGISTRY_URL!=null){
|
||||
ResourceRegistryContextClientFactory.forceToURL(RESOURCE_REGISTRY_URL);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecurityTokenProvider.instance.reset();
|
||||
ScopeProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
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;
|
||||
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 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 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 {
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 {
|
||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.gcube.informationsystem.resourceregistry.context;
|
||||
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
@ -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.context.impl.entities.ContextImpl;
|
||||
import org.gcube.informationsystem.context.reference.entities.Context;
|
||||
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
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.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.utils.ElementMapper;
|
||||
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.serialization.ElementMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -34,61 +29,43 @@ public class ResourceRegistryContextClientTest extends ContextTest {
|
|||
protected ResourceRegistryContextClient resourceRegistryContextClient;
|
||||
|
||||
public ResourceRegistryContextClientTest() {
|
||||
resourceRegistryContextClient = ResourceRegistryContextClientFactory.create();
|
||||
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
|
||||
resourceRegistryContextClient = new ResourceRegistryContextClientImpl(ContextTest.RESOURCE_REGISTRY_URL);
|
||||
}else {
|
||||
resourceRegistryContextClient = ResourceRegistryContextClientFactory.create();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String CTX_NAME_A = "A";
|
||||
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().getModifiedBy().compareTo(user) == 0);
|
||||
Assert.assertTrue(er.getMetadata().getLastUpdateBy().compareTo(user) == 0);
|
||||
|
||||
if(create) {
|
||||
Assert.assertTrue(er.getHeader().getCreator().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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,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;
|
||||
}
|
||||
|
||||
|
@ -128,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 {
|
||||
|
@ -340,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);
|
||||
|
@ -464,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;
|
||||
|
@ -479,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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -489,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);
|
|
@ -6,3 +6,4 @@
|
|||
/registry.properties.empty
|
||||
/registry.properties.redirect-test
|
||||
/registry.properties.test
|
||||
/config.ini
|
||||
|
|
Loading…
Reference in New Issue