Compare commits
41 Commits
Author | SHA1 | Date |
---|---|---|
Luca Frosini | 14626cc5b9 | |
Luca Frosini | 3a7649a73c | |
Luca Frosini | c1128dd10b | |
luca.frosini | 46a4ecc5a9 | |
luca.frosini | fea4d597c1 | |
luca.frosini | fa08c93fd8 | |
Luca Frosini | fb19b25664 | |
Luca Frosini | ef7d845903 | |
Luca Frosini | 9933344e63 | |
Luca Frosini | 6510a1dac9 | |
Luca Frosini | 988630d5c0 | |
Luca Frosini | effa972513 | |
Luca Frosini | 99e0725344 | |
Luca Frosini | eb60093daf | |
Luca Frosini | 0bd6be8c3f | |
Luca Frosini | 1a55e93ebe | |
Luca Frosini | 76873d9787 | |
Luca Frosini | 9be6bfa8ec | |
Luca Frosini | 5f3bcda570 | |
Luca Frosini | c84c4e4c78 | |
Luca Frosini | b1541f1671 | |
Luca Frosini | 926812df1d | |
Luca Frosini | 00c275d22f | |
Luca Frosini | 9ea7640867 | |
Luca Frosini | 26e2eb5e6d | |
Luca Frosini | 66506cb5e5 | |
Luca Frosini | 31c19f3c49 | |
Luca Frosini | 7a0eb867fe | |
Luca Frosini | f56b983b2e | |
Luca Frosini | 73560578ea | |
Luca Frosini | adbda6559e | |
Luca Frosini | 73de846d82 | |
Luca Frosini | 58e80626a7 | |
Luca Frosini | dde4d011d2 | |
Luca Frosini | 9577ba0b13 | |
Luca Frosini | 76d1a81b9f | |
Luca Frosini | 847dc36fab | |
Luca Frosini | 9fa32fa7c3 | |
Luca Frosini | edc51232a5 | |
Luca Frosini | 4b58c94bc5 | |
Luca Frosini | 47ba494ec3 |
|
@ -1,3 +1,5 @@
|
||||||
target
|
target
|
||||||
.classpath
|
.classpath
|
||||||
.project
|
.project
|
||||||
|
/.DS_Store
|
||||||
|
/bin/
|
||||||
|
|
25
CHANGELOG.md
25
CHANGELOG.md
|
@ -2,8 +2,31 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||||
|
|
||||||
# Changelog for Resource Registry Schema Client
|
# Changelog for Resource Registry Schema Client
|
||||||
|
|
||||||
|
## [v4.3.0-SNAPSHOT]
|
||||||
|
|
||||||
## [v4.0.0] [r5.0.0] -
|
- Added support for model knowledge [#25922]
|
||||||
|
|
||||||
|
|
||||||
|
## [v4.2.1]
|
||||||
|
|
||||||
|
- Migrated code to reorganized E/R format [#24992]
|
||||||
|
|
||||||
|
|
||||||
|
## [v4.2.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.1.0]
|
||||||
|
|
||||||
|
- Restrict the interface to accept ERElement classes and not all the Element classes [#21973]
|
||||||
|
- Client gets service URL using resource-registry-api lib utility [#23658]
|
||||||
|
|
||||||
|
|
||||||
|
## [v4.0.0] [r5.0.0]
|
||||||
|
|
||||||
- Switched JSON management to gcube-jackson [#19116]
|
- Switched JSON management to gcube-jackson [#19116]
|
||||||
|
|
||||||
|
|
|
@ -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);
|
23
README.md
23
README.md
|
@ -45,26 +45,5 @@ open-source software toolkit used for building and operating Hybrid Data
|
||||||
Infrastructures enabling the dynamic deployment of Virtual Research Environments
|
Infrastructures enabling the dynamic deployment of Virtual Research Environments
|
||||||
by favouring the realisation of reuse oriented policies.
|
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 projects leading to this software have received funding from a series of European Union programmes see [FUNDING.md](FUNDING.md)
|
||||||
|
|
||||||
- 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);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
18
pom.xml
18
pom.xml
|
@ -8,7 +8,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.gcube.information-system</groupId>
|
<groupId>org.gcube.information-system</groupId>
|
||||||
<artifactId>resource-registry-schema-client</artifactId>
|
<artifactId>resource-registry-schema-client</artifactId>
|
||||||
<version>4.0.0</version>
|
<version>4.3.0-SNAPSHOT</version>
|
||||||
<name>Resource Registry Schema Client</name>
|
<name>Resource Registry Schema Client</name>
|
||||||
<description>Contains Non Idempotent API to manage Schemas in Resource Registry</description>
|
<description>Contains Non Idempotent API to manage Schemas in Resource Registry</description>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.distribution</groupId>
|
<groupId>org.gcube.distribution</groupId>
|
||||||
<artifactId>gcube-bom</artifactId>
|
<artifactId>gcube-bom</artifactId>
|
||||||
<version>2.0.1</version>
|
<version>2.4.0</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -37,14 +37,6 @@
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
<dependencies>
|
<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>
|
<dependency>
|
||||||
<groupId>org.gcube.information-system</groupId>
|
<groupId>org.gcube.information-system</groupId>
|
||||||
<artifactId>resource-registry-api</artifactId>
|
<artifactId>resource-registry-api</artifactId>
|
||||||
|
@ -78,5 +70,11 @@
|
||||||
<artifactId>gcube-model</artifactId>
|
<artifactId>gcube-model</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,30 @@
|
||||||
|
package org.gcube.informationsystem.resourceregistry.schema;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.base.reference.AccessType;
|
||||||
|
import org.gcube.informationsystem.model.knowledge.TypesDiscoverer;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class RRCCTypesDiscoverer implements TypesDiscoverer<Type> {
|
||||||
|
|
||||||
|
protected ResourceRegistrySchemaClientImpl rrsc;
|
||||||
|
|
||||||
|
public RRCCTypesDiscoverer(ResourceRegistrySchemaClientImpl rrsc) {
|
||||||
|
this.rrsc = rrsc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Type> discover(AccessType accessType) {
|
||||||
|
try {
|
||||||
|
return rrsc.getTypeFromServer(accessType.getTypeClass(), true);
|
||||||
|
} catch (ResourceRegistryException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,10 +2,13 @@ package org.gcube.informationsystem.resourceregistry.schema;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
|
||||||
|
import org.gcube.informationsystem.model.reference.ModelElement;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
|
||||||
|
import org.gcube.informationsystem.tree.Node;
|
||||||
|
import org.gcube.informationsystem.types.knowledge.TypeInformation;
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,16 +16,35 @@ import org.gcube.informationsystem.types.reference.Type;
|
||||||
*/
|
*/
|
||||||
public interface ResourceRegistrySchemaClient {
|
public interface ResourceRegistrySchemaClient {
|
||||||
|
|
||||||
public <E extends Element> Type create(Class<E> clz)
|
public ModelKnowledge<Type, TypeInformation> getModelKnowledge();
|
||||||
throws SchemaException, ResourceRegistryException;
|
|
||||||
|
|
||||||
public String create(String typeDefinitition)
|
public void renewModelKnowledge();
|
||||||
throws SchemaException, ResourceRegistryException;
|
|
||||||
|
|
||||||
public <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
|
public void addHeader(String name, String value);
|
||||||
|
|
||||||
|
public String create(String typeDefinitition) throws SchemaException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public <ME extends ModelElement> Type create(Class<ME> clz) throws SchemaException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public boolean exist(String typeName) throws ResourceRegistryException;
|
||||||
|
|
||||||
|
public <ME extends ModelElement> boolean exist(Class<ME> clz) throws ResourceRegistryException;
|
||||||
|
|
||||||
|
public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public <ME extends ModelElement> List<Type> read(Class<ME> clz, Boolean polymorphic)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public String read(String typeName, int level)
|
||||||
throws SchemaNotFoundException, ResourceRegistryException;
|
throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
public String read(String typeName, Boolean polymorphic)
|
public <ME extends ModelElement> List<Type> read(Class<ME> clz, int level)
|
||||||
throws SchemaNotFoundException, ResourceRegistryException;
|
throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public Node<Type> getTypeTreeNode(String typeName)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public <ME extends ModelElement> Node<Type> getTypeTreeNode(Class<ME> clz)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,7 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.schema;
|
package org.gcube.informationsystem.resourceregistry.schema;
|
||||||
|
|
||||||
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.informationsystem.resourceregistry.api.Constants;
|
||||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
import org.gcube.informationsystem.resourceregistry.api.rest.ServiceInstance;
|
||||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -21,100 +11,26 @@ import org.slf4j.LoggerFactory;
|
||||||
public class ResourceRegistrySchemaClientFactory {
|
public class ResourceRegistrySchemaClientFactory {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientFactory.class);
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientFactory.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() {
|
public static String getResourceRegistryURL() {
|
||||||
String token = SecurityTokenProvider.instance.get();
|
String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
|
||||||
AuthorizationEntry authorizationEntry = null;
|
return address;
|
||||||
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(){
|
public static String getResourceRegistryURL(String context) {
|
||||||
return ICFactory.queryFor(GCoreEndpoint.class)
|
String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME);
|
||||||
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
|
return address;
|
||||||
.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 ResourceRegistrySchemaClient create() {
|
public static ResourceRegistrySchemaClient create() {
|
||||||
String address = null;
|
String address = getResourceRegistryURL();
|
||||||
|
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||||
if(FORCED_URL!=null){
|
return new ResourceRegistrySchemaClientImpl(address);
|
||||||
address = FORCED_URL;
|
}
|
||||||
}else {
|
|
||||||
|
public static ResourceRegistrySchemaClient create(String context) {
|
||||||
if(addresses==null || addresses.isEmpty()) {
|
String address = getResourceRegistryURL(context);
|
||||||
addresses = getAddresses();
|
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||||
}
|
|
||||||
|
|
||||||
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 ResourceRegistrySchemaClientImpl(address);
|
return new ResourceRegistrySchemaClientImpl(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,34 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.schema;
|
package org.gcube.informationsystem.resourceregistry.schema;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.gcube.common.gxhttp.reference.GXConnection;
|
import org.gcube.common.gxhttp.reference.GXConnection;
|
||||||
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.common.http.GXHTTPUtility;
|
||||||
|
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
|
||||||
|
import org.gcube.informationsystem.model.reference.ModelElement;
|
||||||
|
import org.gcube.informationsystem.model.reference.properties.Metadata;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
|
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
import org.gcube.informationsystem.tree.Node;
|
||||||
import org.gcube.informationsystem.types.TypeMapper;
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
|
import org.gcube.informationsystem.types.knowledge.TypeInformation;
|
||||||
|
import org.gcube.informationsystem.types.knowledge.TypesKnowledge;
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
|
import org.gcube.informationsystem.utils.TypeUtility;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -28,15 +39,95 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientImpl.class);
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientImpl.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 final String address;
|
||||||
|
|
||||||
public ResourceRegistrySchemaClientImpl(String address) {
|
protected Map<String, String> headers;
|
||||||
this.address = address;
|
|
||||||
|
protected TypesKnowledge typesKnowledge;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Track if the client must request to include {@link Metadata}
|
||||||
|
*/
|
||||||
|
protected boolean includeMeta;
|
||||||
|
|
||||||
|
public boolean includeMeta() {
|
||||||
|
return includeMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIncludeMeta(boolean includeMeta) {
|
||||||
|
this.includeMeta = includeMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
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{
|
||||||
|
if(includeMeta) {
|
||||||
|
queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends Element> Type create(Class<E> clz)
|
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 ResourceRegistrySchemaClientImpl(String address) {
|
||||||
|
this(address, true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResourceRegistrySchemaClientImpl(String address, boolean sharedModelKnowledge) {
|
||||||
|
this.address = address;
|
||||||
|
this.headers = new HashMap<>();
|
||||||
|
this.includeMeta = false;
|
||||||
|
|
||||||
|
if(sharedModelKnowledge) {
|
||||||
|
this.typesKnowledge = TypesKnowledge.getInstance();
|
||||||
|
}else {
|
||||||
|
this.typesKnowledge = new TypesKnowledge();
|
||||||
|
}
|
||||||
|
typesKnowledge.setTypesDiscoverer(new RRCCTypesDiscoverer(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
|
||||||
|
return typesKnowledge.getModelKnowledge();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void renewModelKnowledge() {
|
||||||
|
typesKnowledge.renew();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ME extends ModelElement> Type create(Class<ME> clz)
|
||||||
throws SchemaException, ResourceRegistryException {
|
throws SchemaException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
String typeDefinition = TypeMapper.serializeType(clz);
|
String typeDefinition = TypeMapper.serializeType(clz);
|
||||||
|
@ -56,16 +147,20 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
|
||||||
logger.trace("Going to create: {}", typeDefinitition);
|
logger.trace("Going to create: {}", typeDefinitition);
|
||||||
Type typeDefinitionObj = TypeMapper.deserializeTypeDefinition(typeDefinitition);
|
Type typeDefinitionObj = TypeMapper.deserializeTypeDefinition(typeDefinitition);
|
||||||
|
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistrySchemaClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
gxHTTPStringRequest.header(CONTENT_TYPE_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
||||||
gxHTTPStringRequest.path(typeDefinitionObj.getName());
|
gxHTTPStringRequest.path(typeDefinitionObj.getName());
|
||||||
|
|
||||||
|
includeAdditionalQueryParameters(gxHTTPStringRequest);
|
||||||
|
|
||||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition);
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition);
|
||||||
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||||
|
|
||||||
|
Type t = TypeMapper.deserializeTypeDefinition(c);
|
||||||
|
typesKnowledge.getModelKnowledge().addType(t);
|
||||||
|
|
||||||
logger.trace("{} successfully created", c);
|
logger.trace("{} successfully created", c);
|
||||||
return c;
|
return c;
|
||||||
|
|
||||||
|
@ -76,14 +171,154 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ME extends ModelElement> boolean exist(Class<ME> clazz) throws ResourceRegistryException {
|
||||||
|
return exist(TypeUtility.getTypeName(clazz));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <ISM extends Element> List<Type> read(Class<ISM> clz, Boolean polymorphic)
|
public boolean exist(String typeName) throws ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
return typesKnowledge.getModelKnowledge().getTypeByName(typeName) != null;
|
||||||
|
}catch (RuntimeException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
return getTypeFromTypesKnowledge(typeName, polymorphic, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Type> getTypeFromTypesKnowledge(String typeName, int level)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
return getTypeFromTypesKnowledge(typeName, true, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<Type> addChildren(Node<Type> node, List<Type> types, int currentLevel, int maxLevel) {
|
||||||
|
if(maxLevel>=0 && maxLevel <= currentLevel) {
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Node<Type>> children = node.getChildrenNodes();
|
||||||
|
if(children!=null && children.size()>0) {
|
||||||
|
for(Node<Type> child : children) {
|
||||||
|
types.add(child.getNodeElement());
|
||||||
|
types = addChildren(child, types, ++currentLevel, maxLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic, int level)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
|
||||||
|
Node<Type> node = getTypeTreeNode(typeName);
|
||||||
|
|
||||||
|
List<Type> types = new ArrayList<>();
|
||||||
|
types.add(node.getNodeElement());
|
||||||
|
|
||||||
|
if (polymorphic) {
|
||||||
|
addChildren(node, types, 0, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
List<Type> types = getTypeFromTypesKnowledge(typeName, polymorphic);
|
||||||
|
return TypeMapper.serializeTypeDefinitions(types);
|
||||||
|
} catch(ResourceRegistryException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ME extends ModelElement> List<Type> read(Class<ME> clazz, Boolean polymorphic)
|
||||||
throws SchemaNotFoundException, ResourceRegistryException {
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
String typeName = Utility.getTypeName(clz);
|
String typeName = TypeUtility.getTypeName(clazz);
|
||||||
String res = read(typeName, polymorphic);
|
return getTypeFromTypesKnowledge(typeName, polymorphic);
|
||||||
return TypeMapper.deserializeTypeDefinitions(res);
|
} catch (Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String read(String typeName, int level) throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
List<Type> types = getTypeFromTypesKnowledge(typeName, level);
|
||||||
|
return TypeMapper.serializeTypeDefinitions(types);
|
||||||
|
} catch(ResourceRegistryException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ME extends ModelElement> List<Type> read(Class<ME> clazz, int level)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
String typeName = TypeUtility.getTypeName(clazz);
|
||||||
|
return getTypeFromTypesKnowledge(typeName, level);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node<Type> getTypeTreeNode(String typeName) throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
Node<Type> node = null;
|
||||||
|
try {
|
||||||
|
node = typesKnowledge.getModelKnowledge().getNodeByName(typeName);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
throw new SchemaNotFoundException(e);
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
} catch(ResourceRegistryException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <ME extends ModelElement> Node<Type> getTypeTreeNode(Class<ME> clazz)
|
||||||
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
String typeName = TypeUtility.getTypeName(clazz);
|
||||||
|
return getTypeTreeNode(typeName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existTypeFromServer(String typeName) throws ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
logger.info("Going to get {} schema", typeName);
|
||||||
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
||||||
|
gxHTTPStringRequest.path(typeName);
|
||||||
|
|
||||||
|
Map<String,String> parameters = new HashMap<>();
|
||||||
|
parameters.put(TypePath.POLYMORPHIC_QUERY_PARAMETER, Boolean.FALSE.toString());
|
||||||
|
gxHTTPStringRequest.queryParams(parameters);
|
||||||
|
|
||||||
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
|
||||||
|
HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
return false;
|
||||||
} catch(ResourceRegistryException e) {
|
} catch(ResourceRegistryException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
|
@ -91,21 +326,32 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public <ME extends ModelElement> List<Type> getTypeFromServer(Class<ME> clz, Boolean polymorphic)
|
||||||
public String read(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
|
throws SchemaNotFoundException, ResourceRegistryException {
|
||||||
|
try {
|
||||||
|
String typeName = TypeUtility.getTypeName(clz);
|
||||||
|
String json = getTypeFromServer(typeName, polymorphic);
|
||||||
|
return TypeMapper.deserializeTypeDefinitions(json);
|
||||||
|
} catch(ResourceRegistryException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTypeFromServer(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
|
||||||
try {
|
try {
|
||||||
logger.info("Going to get {} schema", typeName);
|
logger.info("Going to get {} schema", typeName);
|
||||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
|
||||||
gxHTTPStringRequest.from(ResourceRegistrySchemaClient.class.getSimpleName());
|
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
||||||
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
|
||||||
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART);
|
||||||
gxHTTPStringRequest.path(typeName);
|
gxHTTPStringRequest.path(typeName);
|
||||||
|
|
||||||
Map<String,String> parameters = new HashMap<>();
|
Map<String,String> parameters = new HashMap<>();
|
||||||
if(polymorphic != null) {
|
if(polymorphic != null) {
|
||||||
parameters.put(TypePath.POLYMORPHIC_PARAM, polymorphic.toString());
|
parameters.put(TypePath.POLYMORPHIC_QUERY_PARAMETER, polymorphic.toString());
|
||||||
}
|
}
|
||||||
gxHTTPStringRequest.queryParams(parameters);
|
includeAdditionalQueryParameters(gxHTTPStringRequest, parameters);
|
||||||
|
|
||||||
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
||||||
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
|
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
|
||||||
|
|
|
@ -7,14 +7,15 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.gcube.common.authorization.client.Constants;
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
import org.gcube.common.authorization.utils.secret.JWTSecret;
|
||||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
import org.gcube.common.authorization.utils.secret.Secret;
|
||||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||||
import org.gcube.common.authorization.library.utils.Caller;
|
import org.gcube.common.keycloak.KeycloakClientHelper;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.keycloak.model.TokenResponse;
|
||||||
|
import org.gcube.informationsystem.model.reference.properties.Metadata;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -22,91 +23,147 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||||
|
|
||||||
protected static Properties properties;
|
protected static final String CONFIG_INI_FILENAME = "config.ini";
|
||||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
|
||||||
|
|
||||||
public static final String PARENT_DEFAULT_TEST_SCOPE;
|
public static final String PARENT_DEFAULT_TEST_SCOPE;
|
||||||
public static final String DEFAULT_TEST_SCOPE;
|
public static final String DEFAULT_TEST_SCOPE;
|
||||||
public static final String ALTERNATIVE_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;
|
||||||
|
|
||||||
|
public static final String TYPE_PROPERTY_KEY = "type";
|
||||||
|
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||||
|
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||||
|
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
|
||||||
|
|
||||||
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_PROPERTY = "RESOURCE_REGISTRY_URL";
|
||||||
public static final String RESOURCE_REGISTRY_URL;
|
public static final String RESOURCE_REGISTRY_URL;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
properties = new Properties();
|
GCUBE = "/gcube";
|
||||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
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 {
|
try {
|
||||||
// load the properties file
|
// load the properties file
|
||||||
properties.load(input);
|
properties.load(input);
|
||||||
} catch(IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
RESOURCE_REGISTRY_URL = registryProperties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
|
}
|
||||||
|
|
||||||
|
private enum Type{
|
||||||
|
USER, CLIENT_ID
|
||||||
|
};
|
||||||
|
|
||||||
|
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 {
|
||||||
|
logger.debug("Going to set credentials for context {}", fullContextName);
|
||||||
|
Secret secret = getSecretByContextName(fullContextName);
|
||||||
|
set(secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||||
|
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
|
||||||
|
|
||||||
if(RESOURCE_REGISTRY_URL!=null){
|
TokenResponse tr = null;
|
||||||
ResourceRegistrySchemaClientFactory.forceToURL(RESOURCE_REGISTRY_URL);
|
|
||||||
|
int index = context.indexOf('/', 1);
|
||||||
|
String root = context.substring(0, index == -1 ? context.length() : index);
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case CLIENT_ID:
|
||||||
|
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
||||||
|
String clientSecret = properties.getProperty(root);
|
||||||
|
|
||||||
|
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USER:
|
||||||
|
default:
|
||||||
|
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
|
||||||
|
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
|
||||||
|
|
||||||
|
switch (root) {
|
||||||
|
case "/gcube":
|
||||||
|
default:
|
||||||
|
clientId = "next.d4science.org";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "/pred4s":
|
||||||
|
clientId = "pre.d4science.org";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "/d4science.research-infrastructures.eu":
|
||||||
|
clientId = "services.d4science.org";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
clientSecret = null;
|
||||||
|
|
||||||
|
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return tr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
|
public static Secret getSecretByContextName(String context) throws Exception {
|
||||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
TokenResponse tr = getJWTAccessToken(context);
|
||||||
String context = authorizationEntry.getContext();
|
Secret secret = new JWTSecret(tr.getAccessToken());
|
||||||
logger.info("Context of token {} is {}", token, context);
|
return secret;
|
||||||
return context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
|
public static void setContext(String token) throws Exception {
|
||||||
String token = ContextTest.properties.getProperty(fullContextName);
|
Secret secret = getSecret(token);
|
||||||
setContext(token);
|
set(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setContext(String token) throws ObjectNotFound, Exception {
|
private static Secret getSecret(String token) throws Exception {
|
||||||
SecurityTokenProvider.instance.set(token);
|
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
return secret;
|
||||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
}
|
||||||
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
|
||||||
String qualifier = authorizationEntry.getQualifier();
|
public static String getUser() {
|
||||||
Caller caller = new Caller(clientInfo, qualifier);
|
String user = Metadata.UNKNOWN_USER;
|
||||||
AuthorizationProvider.instance.set(caller);
|
try {
|
||||||
ScopeProvider.instance.set(getCurrentScope(token));
|
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||||
|
} catch(Exception e) {
|
||||||
|
logger.error("Unable to retrieve user. {} will be used", user);
|
||||||
|
}
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -116,8 +173,7 @@ public class ContextTest {
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClass() throws Exception {
|
public static void afterClass() throws Exception {
|
||||||
SecurityTokenProvider.instance.reset();
|
SecretManagerProvider.instance.reset();
|
||||||
ScopeProvider.instance.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,12 @@ package org.gcube.informationsystem.resourceregistry.schema;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.gcube.informationsystem.model.reference.entities.Facet;
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
||||||
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
||||||
|
import org.gcube.informationsystem.model.reference.properties.Property;
|
||||||
|
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||||
|
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
|
||||||
|
import org.gcube.informationsystem.tree.Node;
|
||||||
import org.gcube.informationsystem.types.TypeMapper;
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
import org.gcube.informationsystem.types.annotations.Abstract;
|
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -15,23 +19,19 @@ public class ResourceRegistrySchemaClientTest extends ContextTest {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientTest.class);
|
private static Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientTest.class);
|
||||||
|
|
||||||
@Abstract
|
protected ResourceRegistrySchemaClient resourceRegistrySchemaClient;
|
||||||
interface AuxFacet extends Facet {
|
|
||||||
public static final String NAME = "aux"; //AuxFacet.class.getSimpleName();
|
public ResourceRegistrySchemaClientTest() {
|
||||||
public static final String DESCRIPTION = "This is AuxFacet used for Test";
|
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
|
||||||
public static final String VERSION = "1.0.0";
|
resourceRegistrySchemaClient = new ResourceRegistrySchemaClientImpl(ContextTest.RESOURCE_REGISTRY_URL);
|
||||||
|
}else {
|
||||||
|
resourceRegistrySchemaClient = ResourceRegistrySchemaClientFactory.create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test
|
|
||||||
public void testCreate() throws Exception {
|
|
||||||
ResourceRegistrySchemaClient resourceRegistrySchemaClient = ResourceRegistrySchemaClientFactory.create();
|
|
||||||
Type td = resourceRegistrySchemaClient.create(AuxFacet.class);
|
|
||||||
logger.debug("{}", td);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRead() throws Exception {
|
public void testRead() throws Exception {
|
||||||
ResourceRegistrySchemaClient resourceRegistrySchemaClient = ResourceRegistrySchemaClientFactory.create();
|
|
||||||
List<Type> types = resourceRegistrySchemaClient.read(Facet.class, true);
|
List<Type> types = resourceRegistrySchemaClient.read(Facet.class, true);
|
||||||
Assert.assertTrue(types.size()>1);
|
Assert.assertTrue(types.size()>1);
|
||||||
for(Type td : types) {
|
for(Type td : types) {
|
||||||
|
@ -45,9 +45,22 @@ public class ResourceRegistrySchemaClientTest extends ContextTest {
|
||||||
|
|
||||||
|
|
||||||
Assert.assertTrue(gotFacetDefinition.getName().compareTo(facetDefinition.getName())==0);
|
Assert.assertTrue(gotFacetDefinition.getName().compareTo(facetDefinition.getName())==0);
|
||||||
// Uncomment when OrientDB team solve bug https://github.com/orientechnologies/orientdb/issues/8730
|
Assert.assertTrue(gotFacetDefinition.getDescription().compareTo(facetDefinition.getDescription())==0);
|
||||||
//Assert.assertTrue(gotFacetDefinition.getDescription().compareTo(facetDefinition.getDescription())==0);
|
|
||||||
|
Node<Type> node = resourceRegistrySchemaClient.getTypeTreeNode(Resource.class);
|
||||||
|
logger.debug("{}",node);
|
||||||
|
|
||||||
|
node = resourceRegistrySchemaClient.getTypeTreeNode(Facet.class);
|
||||||
|
logger.debug("{}",node);
|
||||||
|
|
||||||
|
node = resourceRegistrySchemaClient.getTypeTreeNode(Property.class);
|
||||||
|
logger.debug("{}",node);
|
||||||
|
|
||||||
|
node = resourceRegistrySchemaClient.getTypeTreeNode(IsRelatedTo.class);
|
||||||
|
logger.debug("{}",node);
|
||||||
|
|
||||||
|
node = resourceRegistrySchemaClient.getTypeTreeNode(ConsistsOf.class);
|
||||||
|
logger.debug("{}",node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,3 +6,4 @@
|
||||||
/registry.properties.empty
|
/registry.properties.empty
|
||||||
/registry.properties.redirect-test
|
/registry.properties.redirect-test
|
||||||
/registry.properties.test
|
/registry.properties.test
|
||||||
|
/config.ini
|
||||||
|
|
Loading…
Reference in New Issue