From 55cf7dd702d7b3c70ed12aaea4c7f22174f4dcba Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 18 Jul 2016 09:57:37 +0000 Subject: [PATCH] Creating Trunk Branch git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@130463 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 ++++++ .project | 23 ++++ .settings/org.eclipse.core.resources.prefs | 6 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 113 +++++++++++++++++ .../client/plugin/AbstractPlugin.java | 33 +++++ .../plugin/ResourceRegistryClientPlugin.java | 53 ++++++++ .../client/proxy/ResourceRegistryClient.java | 114 ++++++++++++++++++ 9 files changed, 387 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e43402f --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..5ce874d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + resource-registry-client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8fc069e --- /dev/null +++ b/pom.xml @@ -0,0 +1,113 @@ + + 4.0.0 + + org.gcube.tools + maven-parent + 1.0.0 + + org.gcube.information-system + resource-registry-client + 1.0.0-SNAPSHOT + Resource Registry Client + Contains Idempotent API for Resource Registry + + + + + org.gcube.distribution + gcube-bom + 1.0.0-SNAPSHOT + pom + import + + + + + + + + org.gcube.core + common-generic-clients + + + org.gcube.core + common-gcube-calls + + + org.gcube.core + common-fw-clients + + + + org.gcube.information-system + resource-registry-api + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + + org.slf4j + slf4j-api + + + + + junit + junit + 4.11 + test + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java new file mode 100644 index 0000000..314ee42 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/AbstractPlugin.java @@ -0,0 +1,33 @@ +package org.gcube.informationsystem.resourceregistry.client.plugin; + +import org.gcube.common.clients.Plugin; +import org.gcube.informationsystem.resourceregistry.Constants; + +/** + * + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + * @param + * @param

+ */ +public abstract class AbstractPlugin implements Plugin { + + 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; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java new file mode 100644 index 0000000..ce44021 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/plugin/ResourceRegistryClientPlugin.java @@ -0,0 +1,53 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.client.plugin; + +import org.gcube.common.clients.GCubeEndpoint; +import org.gcube.common.clients.config.ProxyConfig; +import org.gcube.common.clients.delegates.ProxyDelegate; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class ResourceRegistryClientPlugin extends AbstractPlugin{ + + public ResourceRegistryClientPlugin(){ + super(ResourceRegistryClientPlugin.class.getSimpleName()); + } + + @Override + public String namespace() { + // TODO Auto-generated method stub + 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 GCubeEndpoint resolve(GCubeEndpoint address, ProxyConfig config) + throws Exception { + return null; + } + + @Override + public ResourceRegistryClient newProxy( + ProxyDelegate delegate) { + return new ResourceRegistryClient(delegate); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java new file mode 100644 index 0000000..7a4279b --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/proxy/ResourceRegistryClient.java @@ -0,0 +1,114 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.client.proxy; + +import java.io.BufferedReader; +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 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.GCubeEndpoint; +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.resourceregistry.AccessRESTPath; +import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + */ +public class ResourceRegistryClient { + + private static Logger logger = LoggerFactory + .getLogger(ResourceRegistryClient.class); + + private final AsyncProxyDelegate delegate; + + public ResourceRegistryClient(ProxyDelegate config) { + this.delegate = new AsyncProxyDelegate(config); + } + + protected HttpURLConnection makeRequest(URL url, String method, + boolean includeTokenInHeader) throws Exception { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + if (includeTokenInHeader) { + connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, + SecurityTokenProvider.instance.get()); + } + connection.setRequestMethod(method); + return connection; + } + + protected void appendQueryParameter(StringBuilder builder, String name, + String value) throws UnsupportedEncodingException { + builder.append(name).append("="); + String encodedValue = URLEncoder.encode(value, "UTF-8"); + builder.append(encodedValue).append("&"); + } + + public String query(final String query, final String fetchPlan) + throws InvalidQueryException { + + Call call = new Call() { + + public String call(GCubeEndpoint endpoint) throws Exception { + + StringBuilder callUrl = new StringBuilder(endpoint.getAddress()); + callUrl.append("/").append(AccessRESTPath.ACCESS_PATH_PART) + .append("/"); + + appendQueryParameter(callUrl, AccessRESTPath.QUERY_PARAM, query); + + if (fetchPlan != null) { + appendQueryParameter(callUrl, + AccessRESTPath.FETCH_PLAN_PARAM, fetchPlan); + } + + URL url = new URL(callUrl.toString()); + HttpURLConnection connection = makeRequest(url, "GET", false); + connection.setDoOutput(true); + connection.setDoInput(false); + connection.setRequestProperty("Content-type", "text/plain"); + + 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); + } + + } + +}