Reimplemented HTTPCall to better suite new client and publisher implementation. Client and Publisher now does not uses FWS but ICClient to discover resource-registry.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@146531 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-04-03 16:44:47 +00:00
parent 1f41f9c9e1
commit 2cdb3cf881
14 changed files with 140 additions and 425 deletions

12
pom.xml
View File

@ -51,16 +51,8 @@
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-gcube-calls</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-fw-clients</artifactId>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.proxy;
package org.gcube.informationsystem.resourceregistry.client;
/**
* @author Luca Frosini (ISTI - CNR)

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.resourceregistry.client.proxy;
package org.gcube.informationsystem.resourceregistry.client;
import java.util.List;
import java.util.UUID;
@ -40,6 +40,7 @@ public interface ResourceRegistryClient {
throws SchemaNotFoundException, ResourceRegistryException;
public String query(final String query, final int limit,
final String fetchPlan) throws InvalidQueryException;
final String fetchPlan) throws InvalidQueryException,
ResourceRegistryException;
}

View File

@ -0,0 +1,53 @@
package org.gcube.informationsystem.resourceregistry.client;
import java.util.List;
import java.util.Random;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.informationsystem.resourceregistry.api.Constants;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceRegistryClientFactory {
protected static ResourceRegistryClient singleton;
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 SimpleQuery getQuery(){
return ICFactory.queryFor(GCoreEndpoint.class)
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
.addCondition(String.format(nameFormat, Constants.SERVICE_NAME))
.addCondition(String.format(statusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
.setResult("$entry/text()");
}
public static ResourceRegistryClient create(){
if(singleton==null){
SimpleQuery query = getQuery();
List<String> addresses = ICFactory.client().submit(query);
if(addresses==null || addresses.isEmpty()){
String error = String.format("No %s:%s found in the current context", Constants.SERVICE_CLASS, Constants.SERVICE_NAME);
throw new RuntimeException(error);
}
Random random = new Random();
int index = random.nextInt(addresses.size());
singleton = new ResourceRegistryClientImpl(addresses.get(index));
}
return singleton;
}
}

View File

@ -1,19 +1,15 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.proxy;
package org.gcube.informationsystem.resourceregistry.client;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.xml.ws.EndpointReference;
import org.gcube.common.clients.delegates.AsyncProxyDelegate;
import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.clients.exceptions.ServiceException;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.ER;
import org.gcube.informationsystem.model.ISManageable;
@ -39,14 +35,22 @@ 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 ResourceRegistryClientImpl(ProxyDelegate<EndpointReference> config) {
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
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 <ERType extends ER> void exists(Class<ERType> clazz, UUID uuid)
@ -64,18 +68,14 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
HTTPCall<ERType> httpCall = new HTTPCall<>(stringWriter.toString(),
HTTPMETHOD.HEAD, null);
ResourceRegistryClientCall<ERType> call = new ResourceRegistryClientCall<>(
clazz, httpCall);
delegate.make(call);
logger.info("{} with UUID {} exists", type, uuid);
HTTPCall httpCall = getHTTPCall();
httpCall.call(clazz, stringWriter.toString(), HTTPMETHOD.HEAD);
logger.debug("{} with UUID {} exists", type, uuid);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ServiceException(e);
throw new RuntimeException(e);
}
}
@ -95,21 +95,18 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
HTTPCall<ERType> httpCall = new HTTPCall<>(stringWriter.toString(),
HTTPMETHOD.GET, null);
HTTPCall httpCall = getHTTPCall();
ERType erType = httpCall.call(clazz, stringWriter.toString(),
HTTPMETHOD.GET);
ResourceRegistryClientCall<ERType> call = new ResourceRegistryClientCall<>(
clazz, httpCall);
ERType erType = delegate.make(call);
logger.info("Got {} with UUID {} is {}", type, uuid, erType);
logger.debug("Got {} with UUID {} is {}", type, uuid, erType);
return erType;
} catch (ResourceRegistryException e) {
logger.error("Error while getting {} with UUID {}", type, uuid, e);
throw e;
} catch (Exception e) {
logger.error("Error while getting {} with UUID {}", type, uuid, e);
throw new ServiceException(e);
throw new RuntimeException(e);
}
}
@ -128,15 +125,12 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
Map<String, String> parameters = new HashMap<>();
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
HTTPCall<String> httpCall = new HTTPCall<>(stringWriter.toString(),
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(),
HTTPMETHOD.GET, parameters);
ResourceRegistryClientCall<String> call = new ResourceRegistryClientCall<>(
String.class, httpCall);
String ret = delegate.make(call);
logger.info("Got instances of {} are {}", type, ret);
logger.debug("Got instances of {} are {}", type, ret);
return ISMapper.unmarshalList(Entity.class, ret);
@ -145,7 +139,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
throw e;
} catch (Exception e) {
logger.error("Error while getting {} instances", type, e);
throw new ServiceException(e);
throw new RuntimeException(e);
}
}
@ -169,14 +163,11 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
parameters.put(AccessPath.REFERENCE, reference.toString());
parameters.put(AccessPath.DIRECTION, direction.toString());
HTTPCall<String> httpCall = new HTTPCall<>(stringWriter.toString(),
HTTPMETHOD.GET, parameters);
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
ResourceRegistryClientCall<String> call = new ResourceRegistryClientCall<>(
String.class, httpCall);
String ret = delegate.make(call);
logger.info("Got instances of {} from/to {} are {}", relationType,
logger.debug("Got instances of {} from/to {} are {}", relationType,
reference.toString(), ret);
return ISMapper.unmarshalList(Resource.class, ret);
@ -186,7 +177,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
throw e;
} catch (Exception e) {
logger.error("Error while getting instances of {} from/to {}", relationType, e);
throw new ServiceException(e);
throw new RuntimeException(e);
}
}
@ -209,13 +200,11 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
Map<String, String> parameters = new HashMap<>();
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
HTTPCall<String> httpCall = new HTTPCall<>(stringWriter.toString(),
HTTPMETHOD.GET, parameters);
HTTPCall httpCall = getHTTPCall();
String schema = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
ResourceRegistryClientCall<String> call = new ResourceRegistryClientCall<>(
String.class, httpCall);
String schema = delegate.make(call);
logger.info("Got schema for {} is {}", type, schema);
logger.debug("Got schema for {} is {}", type, schema);
return TypeBinder.deserializeTypeDefinitions(schema);
@ -228,15 +217,45 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
logger.error("Error while getting {}schema for {}",
polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "",
type, e);
throw new ServiceException(e);
throw new RuntimeException(e);
}
}
@Override
public String query(String query, int limit, String fetchPlan)
throws InvalidQueryException {
ResourceRegistryQuery rrq = new ResourceRegistryQuery(delegate);
return rrq.query(query, limit, fetchPlan);
throws InvalidQueryException, ResourceRegistryException {
try {
logger.debug("Going to query. {}", query);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
Map<String, String> parameters = new HashMap<>();
parameters.put(AccessPath.QUERY_PARAM, query);
if(limit<=0){
limit = AccessPath.DEFAULT_LIMIT;
}
parameters.put(AccessPath.LIMIT_PARAM, Integer.toString(limit));
if (fetchPlan != null) {
parameters.put(AccessPath.FETCH_PLAN_PARAM, fetchPlan);
}
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters);
logger.debug("Query result is {}", ret);
return ret;
} catch (ResourceRegistryException e) {
logger.error("Error while querying", e);
throw e;
} catch (Exception e) {
logger.error("Error while querying", e);
throw new RuntimeException(e);
}
}
}

View File

@ -1,34 +0,0 @@
package org.gcube.informationsystem.resourceregistry.client.plugin;
import org.gcube.common.clients.fw.plugin.Plugin;
import org.gcube.informationsystem.resourceregistry.api.Constants;
/**
*
* @author Luca Frosini (ISTI - CNR)
*
* @param <S>
* @param <P>
*/
public abstract class AbstractPlugin<S, P> implements Plugin<S, P> {
public final String name;
public AbstractPlugin(String name) {
this.name = name;
}
public String serviceClass() {
return Constants.SERVICE_CLASS;
}
public String serviceName() {
return Constants.SERVICE_NAME;
}
public String name() {
return name;
}
}

View File

@ -1,57 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.plugin;
import javax.xml.ws.EndpointReference;
import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.informationsystem.resourceregistry.api.Constants;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClientImpl;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ResourceRegistryClientPlugin extends AbstractPlugin<EndpointReference, ResourceRegistryClient>{
public ResourceRegistryClientPlugin(){
super(Constants.SERVICE_ENTRY_NAME);
}
@Override
public String namespace() {
return null;
}
@Override
public Exception convert(Exception fault, ProxyConfig<?, ?> config) {
// The Jersey client wraps the exception. So we need to get the wrapped
// exception thrown by ResourceRegistry Service.
Throwable throwable = fault.getCause();
if(throwable != null && throwable instanceof ResourceRegistryException){
return (Exception) throwable;
}
return fault;
}
@Override
public EndpointReference resolve(EndpointReference address,
ProxyConfig<?, ?> config) throws Exception {
return address;
}
@Override
public ResourceRegistryClient newProxy(
ProxyDelegate<EndpointReference> delegate) {
return new ResourceRegistryClientImpl(delegate);
}
}

View File

@ -1,64 +0,0 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.proxy;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.EndpointReference;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* @author Luca Frosini (ISTI - CNR)
*/
class JaxRSEndpointReference {
private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
private static final String addressLocalName = "Address";
String address;
static {
factory.setNamespaceAware(true);
}
public JaxRSEndpointReference(EndpointReference reference) {
this(serialise(reference));
}
public JaxRSEndpointReference(String reference) {
try {
Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(reference)));
NodeList addresses = document.getElementsByTagNameNS("*", addressLocalName);
if (addresses.getLength() == 0)
throw new RuntimeException("reference does not contain an address");
address = addresses.item(0).getTextContent();
} catch (Exception e) {
throw new IllegalArgumentException("reference is not a gCore reference", e);
}
}
@Override
public String toString() {
return address;
}
// helper
private static String serialise(EndpointReference reference) {
StringWriter writer = new StringWriter();
reference.writeTo(new StreamResult(writer));
return writer.toString();
}
}

View File

@ -1,36 +0,0 @@
package org.gcube.informationsystem.resourceregistry.client.proxy;
import java.io.IOException;
import java.net.URL;
import javax.xml.ws.EndpointReference;
import org.gcube.common.clients.Call;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall;
public class ResourceRegistryClientCall<C> implements Call<EndpointReference, C> {
protected final Class<C> clazz;
protected final HTTPCall<C> httpCall;
public ResourceRegistryClientCall(Class<C> clazz, HTTPCall<C> httpCall) {
this.clazz = clazz;
this.httpCall = httpCall;
}
protected String getURLStringFromEndpointReference(
EndpointReference endpoint) throws IOException {
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint);
return jaxRSEndpointReference.toString();
}
@Override
public C call(EndpointReference endpoint) throws Exception {
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
callUrl.append(httpCall.getPath());
URL url = new URL(callUrl.toString());
return httpCall.call(clazz, url, ResourceRegistryClient.class.getSimpleName());
}
}

View File

@ -1,24 +0,0 @@
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;
}
}

View File

@ -1,136 +0,0 @@
/**
*
*/
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.query.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);
}
}
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.proxy;
package org.gcube.informationsystem.resourceregistry.client;
import java.util.List;
import java.util.UUID;
@ -11,8 +11,9 @@ import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.model.entity.resource.HostingNode;
import org.gcube.informationsystem.model.entity.resource.Service;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
import org.junit.Test;
import org.slf4j.Logger;
@ -33,7 +34,7 @@ public class ResourceRegistryClientTest extends ScopedTest {
}
@Test
public void testQuery() throws InvalidQueryException{
public void testQuery() throws ResourceRegistryException{
String res = resourceRegistryClient.query("SELECT FROM V", 0, null);
logger.trace(res);
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.client.proxy;
package org.gcube.informationsystem.resourceregistry.client;
import java.io.IOException;
import java.io.InputStream;
@ -59,8 +59,8 @@ public class ScopedTest {
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC;
DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVNEXT;
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{

View File

@ -10,7 +10,7 @@
<logger name="org.gcube" level="INFO" />
<logger name="org.gcube.informationsystem" level="INFO" />
<logger name="org.gcube.informationsystem.client" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />