Refs #11455: Integrate GX REST in resource-registry client libraries

Task-Url: https://support.d4science.org/issues/11455

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@176626 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-17 10:18:02 +00:00
parent c8df3b6caa
commit dd2300d1a9
4 changed files with 92 additions and 112 deletions

View File

@ -53,6 +53,10 @@
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-api</artifactId>

View File

@ -56,10 +56,10 @@ public interface ResourceRegistryClient {
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getFilteredResources(
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, boolean polymorphic,
Map<String,Object> map) throws ResourceRegistryException;
Map<String,String> map) throws ResourceRegistryException;
public String getFilteredResources(String resourceType, String consistsOfType, String facetType,
boolean polymorphic, Map<String,Object> map) throws ResourceRegistryException;
boolean polymorphic, Map<String,String> map) throws ResourceRegistryException;
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(

View File

@ -1,12 +1,12 @@
package org.gcube.informationsystem.resourceregistry.client;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.informationsystem.model.impl.utils.ISMapper;
import org.gcube.informationsystem.model.reference.ER;
import org.gcube.informationsystem.model.reference.ISManageable;
@ -24,8 +24,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
@ -39,37 +38,24 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientImpl.class);
public static final String PATH_SEPARATOR = "/";
protected final String address;
protected HTTPCall httpCall;
public ResourceRegistryClientImpl(String address) {
this.address = address;
}
private HTTPCall getHTTPCall() throws MalformedURLException {
if(httpCall == null) {
httpCall = new HTTPCall(address, ResourceRegistryClient.class.getSimpleName());
}
return httpCall;
}
@Override
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
try {
logger.info("Going to get current {} ", Context.NAME);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CURRENT_CONTEXT);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CURRENT_CONTEXT);
HTTPCall httpCall = getHTTPCall();
Context context = httpCall.call(Context.class, stringWriter.toString(), HTTPMETHOD.GET);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
Context context = HTTPUtility.getResponse(Context.class, httpURLConnection);
logger.debug("Got Context is {}", ISMapper.marshal(context));
return context;
@ -91,16 +77,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.info("Going to get {} with UUID {}", Context.NAME, uuid.toString());
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
HTTPCall httpCall = getHTTPCall();
Context context = httpCall.call(Context.class, stringWriter.toString(), HTTPMETHOD.GET);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid.toString());
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
Context context = HTTPUtility.getResponse(Context.class, httpURLConnection);
logger.debug("Got Context is {}", ISMapper.marshal(context));
return context;
@ -121,14 +106,13 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
public List<Context> getAllContext() throws ContextNotFoundException, ResourceRegistryException {
try {
logger.info("Going to read all {}s", Context.NAME);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CONTEXTS_PATH_PART);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got Contexts are {}", ret);
return ISMapper.unmarshalList(Context.class, ret);
@ -151,19 +135,18 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
String type = Utility.getType(clazz);
try {
logger.info("Going to get {} schema", type);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.TYPES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(type);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART);
gxHTTPStringRequest.path(type);
Map<String,String> parameters = new HashMap<>();
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
gxHTTPStringRequest.queryParams(parameters);
HTTPCall httpCall = getHTTPCall();
String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got schema for {} is {}", type, json);
return TypeBinder.deserializeTypeDefinitions(json);
@ -191,18 +174,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try {
logger.info("Going to check if {} with UUID {} exists", type, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(type);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.HEAD);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("{} with UUID {} exists", type, uuid);
return true;
@ -233,18 +213,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try {
logger.info("Going to get {} with UUID {}", type, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(type);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got {} with UUID {} is {}", type, uuid, ret);
return ret;
@ -274,19 +251,19 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException {
try {
logger.info("Going to get all instances of {} ", type);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(type);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type);
Map<String,String> parameters = new HashMap<>();
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
gxHTTPStringRequest.queryParams(parameters);
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got instances of {} are {}", type, ret);
return ret;
@ -305,11 +282,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
try {
logger.info("Going to query. {}", query);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.QUERY_PATH_PART);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART);
Map<String,String> parameters = new HashMap<>();
parameters.put(AccessPath.QUERY_PARAM, query);
@ -322,8 +298,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
parameters.put(AccessPath.FETCH_PLAN_PARAM, fetchPlan);
}
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
gxHTTPStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Query result is {}", ret);
return ret;
@ -337,25 +315,21 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
}
protected String getRelated(String entityType, String relationType, String referenceEntityType,
UUID referenceEntity, Direction direction, boolean polymorphic, Map<String,Object> map)
UUID referenceEntity, Direction direction, Boolean polymorphic, Map<String,String> map)
throws ResourceRegistryException {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.QUERY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(entityType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(relationType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(referenceEntityType);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART);
gxHTTPStringRequest.path(entityType);
gxHTTPStringRequest.path(relationType);
gxHTTPStringRequest.path(referenceEntityType);
Map<String,Object> parameters = new HashMap<>();
parameters.put(AccessPath.DIRECTION_PARAM, direction);
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic);
Map<String,String> parameters = new HashMap<>();
parameters.put(AccessPath.DIRECTION_PARAM, direction.name());
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
if(referenceEntity == null) {
if(map != null && map.size() > 0) {
@ -372,8 +346,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
parameters.put(AccessPath.REFERENCE_PARAM, referenceEntity.toString());
}
HTTPCall httpCall = getHTTPCall();
String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
gxHTTPStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
if(referenceEntity == null) {
logger.info("{} linked by {} to/from {} having {} are {}", entityType, relationType,
@ -428,7 +404,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Override
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getFilteredResources(
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, boolean polymorphic,
Map<String,Object> map) throws ResourceRegistryException {
Map<String,String> map) throws ResourceRegistryException {
String resourceType = Utility.getType(resourceClass);
String consistsOfType = Utility.getType(consistsOfClass);
String facetType = Utility.getType(facetClass);
@ -442,7 +418,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Override
public String getFilteredResources(String resourceType, String consistsOfType, String facetType,
boolean polymorphic, Map<String,Object> map) throws ResourceRegistryException {
boolean polymorphic, Map<String,String> map) throws ResourceRegistryException {
return getRelated(resourceType, consistsOfType, facetType, Direction.out, polymorphic, map);
}
@ -506,7 +482,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
// @Override
protected <E extends Entity, R extends Relation<?,?>, RE extends Entity> List<E> getRelated(Class<E> entityClass,
Class<R> relationClass, Class<RE> referenceEntityClass, Direction direction, boolean polymorphic,
Map<String,Object> map) throws ResourceRegistryException {
Map<String,String> map) throws ResourceRegistryException {
String entityType = Utility.getType(entityClass);
String relationType = Utility.getType(relationClass);
String referenceEntityType = Utility.getType(referenceEntityClass);
@ -520,7 +496,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
// @Override
protected String getRelated(String entityType, String relationType, String referenceEntityType, Direction direction,
boolean polymorphic, Map<String,Object> map) throws ResourceRegistryException {
boolean polymorphic, Map<String,String> map) throws ResourceRegistryException {
return getRelated(entityType, relationType, referenceEntityType, null, direction, polymorphic, map);
}

View File

@ -132,7 +132,7 @@ public class ResourceRegistryClientTest extends ScopedTest {
// @Test
public void testGetFilteredResourcesByClasses() throws ResourceRegistryException, JsonProcessingException {
Map<String,Object> map = new HashMap<>();
Map<String,String> map = new HashMap<>();
map.put("group", "VREManagement");
map.put("name", "SmartExecutor");
List<EService> eServices = resourceRegistryClient.getFilteredResources(EService.class, IsIdentifiedBy.class,