Exposed missing API
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@133313 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
58505a8a6f
commit
dedec490d7
5
pom.xml
5
pom.xml
|
@ -49,6 +49,11 @@
|
||||||
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.information-system</groupId>
|
||||||
|
<artifactId>information-system-model</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||||
import org.gcube.informationsystem.resourceregistry.Constants;
|
import org.gcube.informationsystem.resourceregistry.Constants;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient;
|
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClientImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
@ -48,7 +49,7 @@ public class ResourceRegistryClientPlugin extends AbstractPlugin<EndpointReferen
|
||||||
@Override
|
@Override
|
||||||
public ResourceRegistryClient newProxy(
|
public ResourceRegistryClient newProxy(
|
||||||
ProxyDelegate<EndpointReference> delegate) {
|
ProxyDelegate<EndpointReference> delegate) {
|
||||||
return new ResourceRegistryClient(delegate);
|
return new ResourceRegistryClientImpl(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,135 +1,41 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.util.UUID;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
|
|
||||||
import javax.xml.ws.EndpointReference;
|
import org.gcube.informationsystem.model.entity.Facet;
|
||||||
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
import org.gcube.common.authorization.client.Constants;
|
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
||||||
import org.gcube.common.clients.Call;
|
|
||||||
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.InvalidQueryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.slf4j.Logger;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
public class ResourceRegistryClient {
|
public interface ResourceRegistryClient {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory
|
public Facet getFacet(UUID uuid)
|
||||||
.getLogger(ResourceRegistryClient.class);
|
throws FacetNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
private final AsyncProxyDelegate<EndpointReference> delegate;
|
public <F extends Facet> F getFacet(Class<F> clazz, UUID uuid)
|
||||||
|
throws FacetNotFoundException, ResourceRegistryException;
|
||||||
public ResourceRegistryClient(ProxyDelegate<EndpointReference> config) {
|
|
||||||
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected HttpURLConnection makeRequest(URL url, String method) throws Exception {
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
||||||
if (SecurityTokenProvider.instance.get()==null) {
|
|
||||||
if(ScopeProvider.instance.get()==null){
|
|
||||||
throw new RuntimeException("Null Token and Scope. Please set your token first.");
|
|
||||||
}
|
|
||||||
connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get());
|
|
||||||
}else{
|
|
||||||
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, SecurityTokenProvider.instance.get());
|
|
||||||
}
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setRequestProperty("Content-type", "text/plain");
|
|
||||||
connection.setRequestMethod(method);
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void appendQueryParameter(StringBuilder builder, String name,
|
|
||||||
String value) throws UnsupportedEncodingException {
|
|
||||||
builder.append("?").append(name).append("=");
|
|
||||||
String encodedValue = URLEncoder.encode(value, "UTF-8");
|
|
||||||
builder.append(encodedValue).append("&");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void appendQueryParameter(StringBuilder builder, String name,
|
public String getFacetSchema(String facetType)
|
||||||
int value) throws UnsupportedEncodingException {
|
throws SchemaNotFoundException;
|
||||||
builder.append("?").append(name).append("=");
|
|
||||||
String encodedValue = URLEncoder.encode(String.valueOf(value), "UTF-8");
|
|
||||||
builder.append(encodedValue).append("&");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String query(final String query, final int limit, final String fetchPlan)
|
public Resource getResource(UUID uuid)
|
||||||
throws InvalidQueryException {
|
throws ResourceNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public <R extends Resource> R getResource(Class<R> clazz, UUID uuid)
|
||||||
|
throws ResourceNotFoundException, ResourceRegistryException;
|
||||||
|
|
||||||
|
public String getResourceSchema(String resourceType)
|
||||||
|
throws SchemaNotFoundException;
|
||||||
|
|
||||||
Call<EndpointReference, String> call = new Call<EndpointReference, String>() {
|
|
||||||
|
public String query(final String query, final int limit,
|
||||||
private String getURLStringFromEndpointReference(EndpointReference endpoint) throws IOException {
|
final String fetchPlan) throws InvalidQueryException;
|
||||||
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint);
|
|
||||||
return jaxRSEndpointReference.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String call(EndpointReference endpoint) throws Exception {
|
|
||||||
|
|
||||||
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
|
|
||||||
|
|
||||||
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
|
|
||||||
callUrl.append("/").append(AccessPath.ACCESS_PATH_PART)
|
|
||||||
.append("/");
|
|
||||||
appendQueryParameter(callUrl, AccessPath.QUERY_PARAM, query);
|
|
||||||
|
|
||||||
appendQueryParameter(callUrl, AccessPath.LIMIT_PARAM, limit);
|
|
||||||
|
|
||||||
if (fetchPlan != null) {
|
|
||||||
appendQueryParameter(callUrl,
|
|
||||||
AccessPath.FETCH_PLAN_PARAM, fetchPlan);
|
|
||||||
}
|
|
||||||
|
|
||||||
URL url = new URL(callUrl.toString());
|
|
||||||
HttpURLConnection connection = makeRequest(url, "GET");
|
|
||||||
|
|
||||||
logger.debug("Response code for {} is {} : {}",
|
|
||||||
callUrl.toString(), connection.getResponseCode(),
|
|
||||||
connection.getResponseMessage());
|
|
||||||
|
|
||||||
if (connection.getResponseCode() != 200) {
|
|
||||||
throw new Exception(
|
|
||||||
"Error Querying Resource Registry Service");
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
try (BufferedReader reader = new BufferedReader(
|
|
||||||
new InputStreamReader(
|
|
||||||
(InputStream) connection.getContent()))) {
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
result.append(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
return delegate.make(call);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ServiceException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
|
import javax.xml.ws.EndpointReference;
|
||||||
|
|
||||||
|
import org.gcube.common.clients.fw.builders.StatelessBuilderImpl;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.client.plugin.ResourceRegistryClientPlugin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class ResourceRegistryClientFactory {
|
||||||
|
|
||||||
|
protected static ResourceRegistryClient singleton;
|
||||||
|
|
||||||
|
public static ResourceRegistryClient create(){
|
||||||
|
if(singleton==null){
|
||||||
|
ResourceRegistryClientPlugin plugin = new ResourceRegistryClientPlugin();
|
||||||
|
singleton = new StatelessBuilderImpl<EndpointReference, ResourceRegistryClient>(plugin).build();
|
||||||
|
|
||||||
|
}
|
||||||
|
return singleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,386 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.xml.ws.EndpointReference;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.client.Constants;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.clients.Call;
|
||||||
|
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.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
||||||
|
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> {
|
||||||
|
|
||||||
|
private final K key;
|
||||||
|
private V value;
|
||||||
|
|
||||||
|
public RREntry(K key, V value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public K getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(){
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
boolean first = true;
|
||||||
|
for(Map.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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param requestProperties
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public HTTPInputs(String path, HTTPMETHOD method,
|
||||||
|
List<Map.Entry<String, String>> parameters) throws UnsupportedEncodingException {
|
||||||
|
super();
|
||||||
|
this.path = path;
|
||||||
|
this.method = method;
|
||||||
|
this.urlParameters = getParametersDataString(parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the path
|
||||||
|
*/
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the method
|
||||||
|
*/
|
||||||
|
public HTTPMETHOD getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the urlParameters
|
||||||
|
*/
|
||||||
|
public String getUrlParameters() {
|
||||||
|
return urlParameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ResourceRegistryCall<C> implements Call<EndpointReference, C> {
|
||||||
|
|
||||||
|
protected final Class<C> clazz;
|
||||||
|
protected final HTTPInputs httpInputs;
|
||||||
|
|
||||||
|
public ResourceRegistryCall(Class<C> clazz, HTTPInputs httpInputs) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
this.httpInputs = httpInputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getURLStringFromEndpointReference(
|
||||||
|
EndpointReference endpoint) throws IOException {
|
||||||
|
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(
|
||||||
|
endpoint);
|
||||||
|
return jaxRSEndpointReference.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method)
|
||||||
|
throws Exception {
|
||||||
|
/*
|
||||||
|
if(method!=HTTPMETHOD.POST && httpInputs.getUrlParameters()!=null){
|
||||||
|
*/
|
||||||
|
url = new URL(url + "?" + httpInputs.getUrlParameters());
|
||||||
|
//}
|
||||||
|
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url
|
||||||
|
.openConnection();
|
||||||
|
if (SecurityTokenProvider.instance.get() == null) {
|
||||||
|
if (ScopeProvider.instance.get() == null) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Null Token and Scope. Please set your token first.");
|
||||||
|
}
|
||||||
|
connection.setRequestProperty("gcube-scope",
|
||||||
|
ScopeProvider.instance.get());
|
||||||
|
} else {
|
||||||
|
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,
|
||||||
|
SecurityTokenProvider.instance.get());
|
||||||
|
}
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
connection.setRequestProperty("Content-type", "text/plain");
|
||||||
|
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();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public C call(EndpointReference endpoint) throws Exception {
|
||||||
|
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
|
||||||
|
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
|
||||||
|
callUrl.append(httpInputs.getPath());
|
||||||
|
|
||||||
|
URL url = new URL(callUrl.toString());
|
||||||
|
HttpURLConnection connection = getConnection(url, httpInputs.method);
|
||||||
|
|
||||||
|
logger.debug("Response code for {} is {} : {}",
|
||||||
|
connection.getURL(),
|
||||||
|
connection.getResponseCode(),
|
||||||
|
connection.getResponseMessage());
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
||||||
|
throw new Exception(
|
||||||
|
"Error Contacting Resource Registry Service");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader((InputStream) connection.getContent()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String res = result.toString();
|
||||||
|
logger.trace("Server returned content : {}", res);
|
||||||
|
|
||||||
|
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 {
|
||||||
|
try {
|
||||||
|
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(uuid.toString());
|
||||||
|
|
||||||
|
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||||
|
|
||||||
|
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(clazz, httpInputs);
|
||||||
|
|
||||||
|
return delegate.make(call);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error getting {} with UUID {}", Facet.class.getSimpleName(), uuid, e);
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFacetSchema(String facetType)
|
||||||
|
throws SchemaNotFoundException {
|
||||||
|
try {
|
||||||
|
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);
|
||||||
|
|
||||||
|
return delegate.make(call);
|
||||||
|
} 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 {
|
||||||
|
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());
|
||||||
|
|
||||||
|
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||||
|
|
||||||
|
ResourceRegistryCall<R> call = new ResourceRegistryCall<>(clazz, httpInputs);
|
||||||
|
|
||||||
|
return delegate.make(call);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error getting {} with UUID {}", Resource.class.getSimpleName(), uuid, e);
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getResourceSchema(String resourceType)
|
||||||
|
throws SchemaNotFoundException {
|
||||||
|
try {
|
||||||
|
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.SCHEMA_PATH_PART);
|
||||||
|
stringWriter.append(PATH_SEPARATOR);
|
||||||
|
stringWriter.append(resourceType);
|
||||||
|
|
||||||
|
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.GET, null);
|
||||||
|
|
||||||
|
ResourceRegistryCall<String> call = new ResourceRegistryCall<>(String.class, httpInputs);
|
||||||
|
|
||||||
|
return delegate.make(call);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error getting {} Schema for {}", Resource.class.getSimpleName(), resourceType, e);
|
||||||
|
throw new SchemaNotFoundException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient#query(java.lang.String, int, java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String query(String query, int limit, String fetchPlan)
|
||||||
|
throws InvalidQueryException {
|
||||||
|
ResourceRegistryQuery rrq = new ResourceRegistryQuery(delegate);
|
||||||
|
return rrq.query(query, limit, fetchPlan);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,136 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import javax.xml.ws.EndpointReference;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.client.Constants;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.clients.Call;
|
||||||
|
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.rest.AccessPath;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
class ResourceRegistryQuery {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory
|
||||||
|
.getLogger(ResourceRegistryQuery.class);
|
||||||
|
|
||||||
|
|
||||||
|
private final AsyncProxyDelegate<EndpointReference> delegate;
|
||||||
|
|
||||||
|
public ResourceRegistryQuery(ProxyDelegate<EndpointReference> config) {
|
||||||
|
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected HttpURLConnection makeRequest(URL url, String method) throws Exception {
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
if (SecurityTokenProvider.instance.get()==null) {
|
||||||
|
if(ScopeProvider.instance.get()==null){
|
||||||
|
throw new RuntimeException("Null Token and Scope. Please set your token first.");
|
||||||
|
}
|
||||||
|
connection.setRequestProperty("gcube-scope", ScopeProvider.instance.get());
|
||||||
|
}else{
|
||||||
|
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, SecurityTokenProvider.instance.get());
|
||||||
|
}
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setRequestProperty("Content-type", "text/plain");
|
||||||
|
connection.setRequestMethod(method);
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void appendQueryParameter(StringBuilder builder, String name,
|
||||||
|
String value) throws UnsupportedEncodingException {
|
||||||
|
builder.append("?").append(name).append("=");
|
||||||
|
String encodedValue = URLEncoder.encode(value, "UTF-8");
|
||||||
|
builder.append(encodedValue).append("&");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void appendQueryParameter(StringBuilder builder, String name,
|
||||||
|
int value) throws UnsupportedEncodingException {
|
||||||
|
builder.append("?").append(name).append("=");
|
||||||
|
String encodedValue = URLEncoder.encode(String.valueOf(value), "UTF-8");
|
||||||
|
builder.append(encodedValue).append("&");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String query(final String query, final int limit, final String fetchPlan)
|
||||||
|
throws InvalidQueryException {
|
||||||
|
|
||||||
|
Call<EndpointReference, String> call = new Call<EndpointReference, String>() {
|
||||||
|
|
||||||
|
private String getURLStringFromEndpointReference(EndpointReference endpoint) throws IOException {
|
||||||
|
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint);
|
||||||
|
return jaxRSEndpointReference.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String call(EndpointReference endpoint) throws Exception {
|
||||||
|
|
||||||
|
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
|
||||||
|
|
||||||
|
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
|
||||||
|
callUrl.append("/").append(AccessPath.ACCESS_PATH_PART)
|
||||||
|
.append("/");
|
||||||
|
appendQueryParameter(callUrl, AccessPath.QUERY_PARAM, query);
|
||||||
|
|
||||||
|
appendQueryParameter(callUrl, AccessPath.LIMIT_PARAM, limit);
|
||||||
|
|
||||||
|
if (fetchPlan != null) {
|
||||||
|
appendQueryParameter(callUrl,
|
||||||
|
AccessPath.FETCH_PLAN_PARAM, fetchPlan);
|
||||||
|
}
|
||||||
|
|
||||||
|
URL url = new URL(callUrl.toString());
|
||||||
|
HttpURLConnection connection = makeRequest(url, "GET");
|
||||||
|
|
||||||
|
logger.debug("Response code for {} is {} : {}",
|
||||||
|
callUrl.toString(), connection.getResponseCode(),
|
||||||
|
connection.getResponseMessage());
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new Exception(
|
||||||
|
"Error Querying Resource Registry Service");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(
|
||||||
|
(InputStream) connection.getContent()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
return delegate.make(call);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ServiceException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,12 +3,9 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
import javax.xml.ws.EndpointReference;
|
|
||||||
|
|
||||||
import org.gcube.common.clients.fw.builders.StatelessBuilderImpl;
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.client.plugin.ResourceRegistryClientPlugin;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -22,15 +19,18 @@ public class ResourceRegistryClientTest {
|
||||||
private static Logger logger = LoggerFactory
|
private static Logger logger = LoggerFactory
|
||||||
.getLogger(ResourceRegistryClientTest.class);
|
.getLogger(ResourceRegistryClientTest.class);
|
||||||
|
|
||||||
|
protected ResourceRegistryClient resourceRegistryClient;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before(){
|
||||||
|
ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
||||||
|
resourceRegistryClient = ResourceRegistryClientFactory.create();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuery() throws InvalidQueryException{
|
public void testQuery() throws InvalidQueryException{
|
||||||
|
String res = resourceRegistryClient.query("SELECT FROM V", 0, null);
|
||||||
ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
ResourceRegistryClientPlugin plugin = new ResourceRegistryClientPlugin();
|
|
||||||
|
|
||||||
ResourceRegistryClient rrc = new StatelessBuilderImpl<EndpointReference, ResourceRegistryClient>(plugin).build();
|
|
||||||
|
|
||||||
String res = rrc.query("SELECT FROM V", 0, null);
|
|
||||||
logger.debug(res);
|
logger.debug(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue