Aligned client with new REST interface
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@141530 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
6c51f27068
commit
610d98c874
5
pom.xml
5
pom.xml
|
@ -79,6 +79,11 @@
|
|||
<version>1.0.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.information-system</groupId>
|
||||
<artifactId>gcube-resources</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public enum Direction {
|
||||
|
||||
in, out, both;
|
||||
|
||||
}
|
|
@ -1,37 +1,39 @@
|
|||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.informationsystem.model.entity.Facet;
|
||||
import org.gcube.informationsystem.model.entity.Resource;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||
import org.gcube.informationsystem.model.ER;
|
||||
import org.gcube.informationsystem.model.entity.Entity;
|
||||
import org.gcube.informationsystem.model.relation.Relation;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public interface ResourceRegistryClient {
|
||||
|
||||
public Facet getFacet(UUID uuid)
|
||||
throws FacetNotFoundException, ResourceRegistryException;
|
||||
public <ERType extends ER> ERType getInstance(
|
||||
Class<ERType> clazz, UUID uuid) throws ERNotFoundException,
|
||||
ResourceRegistryException;
|
||||
|
||||
public <F extends Facet> F getFacet(Class<F> clazz, UUID uuid)
|
||||
throws FacetNotFoundException, ResourceRegistryException;
|
||||
public <ERType extends ER> List<ERType> getInstances(
|
||||
Class<ERType> clazz, Boolean polymorphic) throws
|
||||
ERException, SchemaException, ResourceRegistryException;
|
||||
|
||||
public String getFacetSchema(String facetType)
|
||||
throws SchemaNotFoundException;
|
||||
|
||||
public <R extends Relation<Entity, Entity>> List<R> getInstancesFromEntity(
|
||||
Class<R> clazz, Boolean polymorphic,
|
||||
UUID reference, Direction direction) throws
|
||||
ERException, SchemaException, ResourceRegistryException;
|
||||
|
||||
public Resource getResource(UUID uuid)
|
||||
throws ResourceNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <R extends Resource> R getResource(Class<R> clazz, UUID uuid)
|
||||
throws ResourceNotFoundException, ResourceRegistryException;
|
||||
|
||||
public String getResourceSchema(String resourceType)
|
||||
public <ERTYpe extends ER> List<TypeDefinition> getSchema(
|
||||
Class<ERTYpe> clazz, Boolean polymorphic)
|
||||
throws SchemaNotFoundException;
|
||||
|
||||
|
||||
|
|
|
@ -12,8 +12,10 @@ import java.io.UnsupportedEncodingException;
|
|||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.xml.ws.EndpointReference;
|
||||
|
@ -25,120 +27,126 @@ import org.gcube.common.clients.delegates.AsyncProxyDelegate;
|
|||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.impl.utils.Entities;
|
||||
import org.gcube.informationsystem.model.entity.Facet;
|
||||
import org.gcube.informationsystem.model.entity.Resource;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||
import org.gcube.informationsystem.model.ER;
|
||||
import org.gcube.informationsystem.model.entity.Entity;
|
||||
import org.gcube.informationsystem.model.relation.Relation;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
||||
import org.gcube.informationsystem.types.TypeBinder;
|
||||
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(ResourceRegistryClientImpl.class);
|
||||
|
||||
|
||||
private final AsyncProxyDelegate<EndpointReference> delegate;
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
|
||||
public final class RREntry<K, V> implements Map.Entry<K, V> {
|
||||
|
||||
|
||||
public final class RREntry<K, V> implements Entry<K, V> {
|
||||
|
||||
private final K key;
|
||||
private V value;
|
||||
private V value;
|
||||
|
||||
public RREntry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
public RREntry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
@Override
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
@Override
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
V old = this.value;
|
||||
this.value = value;
|
||||
return old;
|
||||
}
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
V old = this.value;
|
||||
this.value = value;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ResourceRegistryClientImpl(ProxyDelegate<EndpointReference> config) {
|
||||
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
||||
}
|
||||
|
||||
|
||||
protected enum HTTPMETHOD {
|
||||
GET, POST, PUT, DELETE;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
return this.name();
|
||||
}
|
||||
}
|
||||
|
||||
class HTTPInputs {
|
||||
|
||||
|
||||
public static final String PARAM_STARTER = "?";
|
||||
public static final String PARAM_EQUALS = "=";
|
||||
public static final String PARAM_SEPARATOR = "&";
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
|
||||
|
||||
protected final String path;
|
||||
protected final HTTPMETHOD method;
|
||||
protected final String urlParameters;
|
||||
|
||||
protected String getParametersDataString(List<Map.Entry<String, String>> parameters) throws UnsupportedEncodingException {
|
||||
if(parameters==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected String getParametersDataString(
|
||||
List<Map.Entry<String, String>> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
if (parameters == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for(Map.Entry<String, String> entry : parameters){
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append(PARAM_SEPARATOR);
|
||||
}
|
||||
boolean first = true;
|
||||
for (Entry<String, String> entry : parameters) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append(PARAM_SEPARATOR);
|
||||
}
|
||||
|
||||
result.append(URLEncoder.encode(entry.getKey(), UTF8));
|
||||
result.append(PARAM_EQUALS);
|
||||
result.append(URLEncoder.encode(entry.getValue(), UTF8));
|
||||
result.append(URLEncoder.encode(entry.getKey(), UTF8));
|
||||
result.append(PARAM_EQUALS);
|
||||
result.append(URLEncoder.encode(entry.getValue(), UTF8));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path
|
||||
* @param method
|
||||
* @param requestProperties
|
||||
* @throws UnsupportedEncodingException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public HTTPInputs(String path, HTTPMETHOD method,
|
||||
List<Map.Entry<String, String>> parameters) throws UnsupportedEncodingException {
|
||||
List<Entry<String, String>> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
super();
|
||||
this.path = path;
|
||||
this.method = method;
|
||||
this.urlParameters = getParametersDataString(parameters);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the path
|
||||
*/
|
||||
|
@ -159,9 +167,9 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
public String getUrlParameters() {
|
||||
return urlParameters;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class ResourceRegistryCall<C> implements Call<EndpointReference, C> {
|
||||
|
||||
protected final Class<C> clazz;
|
||||
|
@ -182,11 +190,12 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method)
|
||||
throws Exception {
|
||||
/*
|
||||
if(method!=HTTPMETHOD.POST && httpInputs.getUrlParameters()!=null){
|
||||
*/
|
||||
* if(method!=HTTPMETHOD.POST &&
|
||||
* httpInputs.getUrlParameters()!=null){
|
||||
*/
|
||||
url = new URL(url + "?" + httpInputs.getUrlParameters());
|
||||
//}
|
||||
|
||||
// }
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) url
|
||||
.openConnection();
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
|
@ -201,22 +210,21 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
SecurityTokenProvider.instance.get());
|
||||
}
|
||||
connection.setDoOutput(true);
|
||||
|
||||
|
||||
connection.setRequestProperty("Content-type", "application/json");
|
||||
connection.setRequestProperty("User-Agent", ResourceRegistryClient.class.getSimpleName());
|
||||
|
||||
connection.setRequestProperty("User-Agent",
|
||||
ResourceRegistryClient.class.getSimpleName());
|
||||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
|
||||
/*
|
||||
if(method==HTTPMETHOD.POST){
|
||||
connection.setDoOutput(true);
|
||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||
wr.writeBytes(httpInputs.getUrlParameters());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}*/
|
||||
|
||||
|
||||
* if(method==HTTPMETHOD.POST){ connection.setDoOutput(true);
|
||||
* DataOutputStream wr = new
|
||||
* DataOutputStream(connection.getOutputStream());
|
||||
* wr.writeBytes(httpInputs.getUrlParameters()); wr.flush();
|
||||
* wr.close(); }
|
||||
*/
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
@ -229,10 +237,9 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
|
||||
URL url = new URL(callUrl.toString());
|
||||
HttpURLConnection connection = getConnection(url, httpInputs.method);
|
||||
|
||||
logger.debug("Response code for {} is {} : {}",
|
||||
connection.getURL(),
|
||||
connection.getResponseCode(),
|
||||
|
||||
logger.debug("Response code for {} is {} : {}",
|
||||
connection.getURL(), connection.getResponseCode(),
|
||||
connection.getResponseMessage());
|
||||
|
||||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
||||
|
@ -249,141 +256,163 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
}
|
||||
}
|
||||
|
||||
String res = result.toString();
|
||||
String res = result.toString();
|
||||
logger.trace("Server returned content : {}", res);
|
||||
|
||||
if(String.class.isAssignableFrom(clazz)){
|
||||
|
||||
if (String.class.isAssignableFrom(clazz)) {
|
||||
return (C) res;
|
||||
}
|
||||
|
||||
|
||||
return Entities.unmarshal(clazz, res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Facet getFacet(UUID uuid)
|
||||
throws FacetNotFoundException, ResourceRegistryException {
|
||||
return getFacet(Facet.class, uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <F extends Facet> F getFacet(Class<F> clazz, UUID uuid)
|
||||
throws FacetNotFoundException, ResourceRegistryException {
|
||||
public <ERType extends ER> ERType getInstance(Class<ERType> clazz, UUID uuid)
|
||||
throws ERNotFoundException, ResourceRegistryException {
|
||||
String type = clazz.getSimpleName();
|
||||
try {
|
||||
logger.info("Going to get {} ({}) with UUID {}", Facet.NAME, clazz.getSimpleName(), uuid);
|
||||
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.FACET_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.INSTANCE_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(type);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(uuid.toString());
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||
|
||||
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(clazz, httpInputs);
|
||||
|
||||
F f = delegate.make(call);
|
||||
logger.info("Got {} ({}) with UUID {} is {}", Facet.NAME, clazz.getSimpleName(), uuid, f);
|
||||
return f;
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
|
||||
HTTPMETHOD.GET, null);
|
||||
|
||||
ResourceRegistryCall<ERType> call = new ResourceRegistryCall<>(
|
||||
clazz, httpInputs);
|
||||
|
||||
ERType erType = delegate.make(call);
|
||||
logger.info("Got {} with UUID {} is {}", type, uuid, erType);
|
||||
return erType;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting {} with UUID {}", Facet.class.getSimpleName(), uuid, e);
|
||||
logger.error("Error while getting {} with UUID {}", type, uuid, e);
|
||||
throw new ResourceRegistryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFacetSchema(String facetType)
|
||||
throws SchemaNotFoundException {
|
||||
public <ERType extends ER> List<ERType> getInstances(Class<ERType> clazz,
|
||||
Boolean polymorphic) throws ERNotFoundException,
|
||||
ResourceRegistryException {
|
||||
String type = clazz.getSimpleName();
|
||||
try {
|
||||
logger.info("Going to get {} schema", facetType);
|
||||
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.FACET_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.SCHEMA_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(facetType);
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||
|
||||
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(String.class, httpInputs);
|
||||
String schema = delegate.make(call);
|
||||
logger.info("Got schema for {} is {}", facetType, schema);
|
||||
return schema;
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting {} Schema for {}", Facet.class.getSimpleName(), facetType, e);
|
||||
throw new SchemaNotFoundException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource getResource(UUID uuid)
|
||||
throws ResourceNotFoundException, ResourceRegistryException {
|
||||
return getResource(Resource.class, uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R extends Resource> R getResource(Class<R> clazz, UUID uuid)
|
||||
throws ResourceNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
logger.info("Going to get {} ({}) with UUID {}", Resource.NAME, clazz.getSimpleName(), uuid);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.ACCESS_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.RESOURCE_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.INSTANCE_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(uuid.toString());
|
||||
stringWriter.append(type);
|
||||
|
||||
List<Entry<String, String>> parameters = new ArrayList<>();
|
||||
parameters.add(new RREntry<String, String>(
|
||||
AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()));
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
|
||||
HTTPMETHOD.GET, parameters);
|
||||
|
||||
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(
|
||||
String.class, httpInputs);
|
||||
|
||||
String ret = delegate.make(call);
|
||||
logger.info("Got instances of {} are {}", type, ret);
|
||||
|
||||
return Entities.unmarshalList(clazz, ret);
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||
|
||||
ResourceRegistryCall<R> call = new ResourceRegistryCall<>(clazz, httpInputs);
|
||||
|
||||
|
||||
R r = delegate.make(call);
|
||||
logger.info("Got {} ({}) with UUID {} is {}", Resource.NAME, clazz.getSimpleName(), uuid, r);
|
||||
return r;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting {} with UUID {}", Resource.class.getSimpleName(), uuid, e);
|
||||
logger.error("Error while getting {} instances", type, e);
|
||||
throw new ResourceRegistryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResourceSchema(String resourceType)
|
||||
throws SchemaNotFoundException {
|
||||
public <R extends Relation<Entity, Entity>> List<R> getInstancesFromEntity(
|
||||
Class<R> clazz, Boolean polymorphic, UUID reference,
|
||||
Direction direction) throws ERException, SchemaException,
|
||||
ResourceRegistryException {
|
||||
String type = clazz.getSimpleName();
|
||||
try {
|
||||
logger.info("Going to get {} schema", resourceType);
|
||||
logger.info("Going to get all instances of {} from/to {}", type,
|
||||
reference.toString());
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.ACCESS_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(AccessPath.RESOURCE_PATH_PART);
|
||||
stringWriter.append(AccessPath.INSTANCE_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(type);
|
||||
|
||||
List<Entry<String, String>> parameters = new ArrayList<>();
|
||||
parameters.add(new RREntry<String, String>(
|
||||
AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()));
|
||||
parameters.add(new RREntry<String, String>(AccessPath.REFERENCE,
|
||||
reference.toString()));
|
||||
parameters.add(new RREntry<String, String>(AccessPath.DIRECTION,
|
||||
direction.toString()));
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
|
||||
HTTPMETHOD.GET, parameters);
|
||||
|
||||
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(
|
||||
String.class, httpInputs);
|
||||
|
||||
String ret = delegate.make(call);
|
||||
logger.info("Got instances of {} from/to {} are {}", type,
|
||||
reference.toString(), ret);
|
||||
|
||||
return Entities.unmarshalList(clazz, ret);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while getting instances of {} from/to {}", type, e);
|
||||
throw new ResourceRegistryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ERTYpe extends ER> List<TypeDefinition> getSchema(
|
||||
Class<ERTYpe> clazz, Boolean polymorphic)
|
||||
throws SchemaNotFoundException {
|
||||
|
||||
String type = clazz.getSimpleName();
|
||||
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.SCHEMA_PATH_PART);
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
stringWriter.append(resourceType);
|
||||
stringWriter.append(type);
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||
|
||||
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(String.class, httpInputs);
|
||||
|
||||
List<Entry<String, String>> parameters = new ArrayList<>();
|
||||
parameters.add(new RREntry<String, String>(
|
||||
AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()));
|
||||
|
||||
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(),
|
||||
HTTPMETHOD.GET, parameters);
|
||||
|
||||
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(
|
||||
String.class, httpInputs);
|
||||
String schema = delegate.make(call);
|
||||
logger.info("Got schema for {} is {}", resourceType, schema);
|
||||
return schema;
|
||||
|
||||
logger.info("Got schema for {} is {}", type, schema);
|
||||
|
||||
return TypeBinder.deserializeTypeDefinitions(schema);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting {} Schema for {}", Resource.class.getSimpleName(), resourceType, e);
|
||||
logger.error("Error while getting {}schema for {}",
|
||||
polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "",
|
||||
type, e);
|
||||
throw new SchemaNotFoundException(e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.gcube.common.clients.delegates.AsyncProxyDelegate;
|
|||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||
import org.gcube.common.clients.exceptions.ServiceException;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
|
@ -3,9 +3,13 @@
|
|||
*/
|
||||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.informationsystem.model.entity.facet.ContactFacet;
|
||||
import org.gcube.informationsystem.model.entity.resource.HostingNode;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||
import org.junit.BeforeClass;
|
||||
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -14,18 +18,13 @@ import org.slf4j.LoggerFactory;
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ResourceRegistryClientTest {
|
||||
public class ResourceRegistryClientTest extends ScopedTest {
|
||||
|
||||
private static Logger logger = LoggerFactory
|
||||
.getLogger(ResourceRegistryClientTest.class);
|
||||
|
||||
protected ResourceRegistryClient resourceRegistryClient;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
ScopedTest.beforeClass();
|
||||
}
|
||||
|
||||
public ResourceRegistryClientTest(){
|
||||
resourceRegistryClient = ResourceRegistryClientFactory.create();
|
||||
}
|
||||
|
@ -39,14 +38,14 @@ public class ResourceRegistryClientTest {
|
|||
|
||||
@Test
|
||||
public void testGetFacetSchema() throws SchemaNotFoundException {
|
||||
String res = resourceRegistryClient.getFacetSchema("ContactFacet");
|
||||
logger.trace(res);
|
||||
List<TypeDefinition> typeDefinitions = resourceRegistryClient.getSchema(ContactFacet.class, true);
|
||||
logger.trace("{}", typeDefinitions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetResourceSchema() throws SchemaNotFoundException {
|
||||
String res = resourceRegistryClient.getResourceSchema("HostingNode");
|
||||
logger.trace(res);
|
||||
List<TypeDefinition> typeDefinitions = resourceRegistryClient.getSchema(HostingNode.class, true);
|
||||
logger.trace("{}", typeDefinitions);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,10 +25,22 @@ public class ScopedTest {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class);
|
||||
|
||||
protected static final String PROPERTIES_FILENAME = "config.properties";
|
||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||
|
||||
private static final String TOKEN_VARNAME = "TOKEN";
|
||||
private static final String TOKEN;
|
||||
private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT";
|
||||
public static final String GCUBE_DEVNEXT;
|
||||
|
||||
private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT";
|
||||
public static final String GCUBE_DEVNEXT_NEXTNEXT;
|
||||
|
||||
public static final String GCUBE_DEVSEC_VARNAME = "GCUBE_DEVSEC";
|
||||
public static final String GCUBE_DEVSEC;
|
||||
|
||||
public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE";
|
||||
public static final String GCUBE_DEVSEC_DEVVRE;
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE;
|
||||
public static final String ALTERNATIVE_TEST_SCOPE;
|
||||
|
||||
static {
|
||||
Properties properties = new Properties();
|
||||
|
@ -41,20 +53,32 @@ public class ScopedTest {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
TOKEN = properties.getProperty(TOKEN_VARNAME);
|
||||
GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME);
|
||||
GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME);
|
||||
|
||||
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
|
||||
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
|
||||
|
||||
DEFAULT_TEST_SCOPE = GCUBE_DEVSEC;
|
||||
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE;
|
||||
}
|
||||
|
||||
private static String getCurrentScope() throws ObjectNotFound, Exception{
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(TOKEN);
|
||||
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);
|
||||
logger.info("Context of token {} is {}", token, context);
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
public static void setContext(String token) throws ObjectNotFound, Exception{
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
ScopeProvider.instance.set(getCurrentScope(token));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception{
|
||||
SecurityTokenProvider.instance.set(TOKEN);
|
||||
ScopeProvider.instance.set(getCurrentScope());
|
||||
setContext(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
|
Loading…
Reference in New Issue