Implementing Client
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@131369 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
e19bd27472
commit
7f30efb5b3
|
@ -1,6 +1,7 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.client.plugin;
|
package org.gcube.informationsystem.resourceregistry.client.plugin;
|
||||||
|
|
||||||
import org.gcube.common.clients.Plugin;
|
|
||||||
|
import org.gcube.common.clients.fw.plugin.Plugin;
|
||||||
import org.gcube.informationsystem.resourceregistry.Constants;
|
import org.gcube.informationsystem.resourceregistry.Constants;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,6 +7,7 @@ import javax.xml.ws.EndpointReference;
|
||||||
|
|
||||||
import org.gcube.common.clients.config.ProxyConfig;
|
import org.gcube.common.clients.config.ProxyConfig;
|
||||||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@ -17,12 +18,11 @@ import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistr
|
||||||
public class ResourceRegistryClientPlugin extends AbstractPlugin<EndpointReference, ResourceRegistryClient>{
|
public class ResourceRegistryClientPlugin extends AbstractPlugin<EndpointReference, ResourceRegistryClient>{
|
||||||
|
|
||||||
public ResourceRegistryClientPlugin(){
|
public ResourceRegistryClientPlugin(){
|
||||||
super(ResourceRegistryClientPlugin.class.getSimpleName());
|
super(Constants.SERVICE_ENTRY_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String namespace() {
|
public String namespace() {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,24 +39,16 @@ public class ResourceRegistryClientPlugin extends AbstractPlugin<EndpointReferen
|
||||||
return fault;
|
return fault;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see org.gcube.common.clients.delegates.ProxyPlugin#resolve(java.lang.Object, org.gcube.common.clients.config.ProxyConfig)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public EndpointReference resolve(EndpointReference address,
|
public EndpointReference resolve(EndpointReference address,
|
||||||
ProxyConfig<?, ?> config) throws Exception {
|
ProxyConfig<?, ?> config) throws Exception {
|
||||||
// TODO Auto-generated method stub
|
return address;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see org.gcube.common.clients.delegates.ProxyPlugin#newProxy(org.gcube.common.clients.delegates.ProxyDelegate)
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public ResourceRegistryClient newProxy(
|
public ResourceRegistryClient newProxy(
|
||||||
ProxyDelegate<EndpointReference> delegate) {
|
ProxyDelegate<EndpointReference> delegate) {
|
||||||
// TODO Auto-generated method stub
|
return new ResourceRegistryClient(delegate);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
|
||||||
|
class JaxRSEndpointReference {
|
||||||
|
|
||||||
|
private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
|
||||||
|
private static final String addressLocalName = "Address";
|
||||||
|
//private static final String keyLocalName = "ResourceKey";
|
||||||
|
|
||||||
|
String address;
|
||||||
|
//Element key;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
package org.gcube.informationsystem.resourceregistry.client.proxy;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
@ -19,6 +20,7 @@ import org.gcube.common.clients.Call;
|
||||||
import org.gcube.common.clients.delegates.AsyncProxyDelegate;
|
import org.gcube.common.clients.delegates.AsyncProxyDelegate;
|
||||||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||||
import org.gcube.common.clients.exceptions.ServiceException;
|
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.rest.AccessPath;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -38,20 +40,26 @@ public class ResourceRegistryClient {
|
||||||
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HttpURLConnection makeRequest(URL url, String method,
|
protected HttpURLConnection makeRequest(URL url, String method) throws Exception {
|
||||||
boolean includeTokenInHeader) throws Exception {
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
if (includeTokenInHeader) {
|
if (SecurityTokenProvider.instance.get()==null) {
|
||||||
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,
|
if(ScopeProvider.instance.get()==null){
|
||||||
SecurityTokenProvider.instance.get());
|
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);
|
connection.setRequestMethod(method);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void appendQueryParameter(StringBuilder builder, String name,
|
protected void appendQueryParameter(StringBuilder builder, String name,
|
||||||
String value) throws UnsupportedEncodingException {
|
String value) throws UnsupportedEncodingException {
|
||||||
builder.append(name).append("=");
|
builder.append("?").append(name).append("=");
|
||||||
String encodedValue = URLEncoder.encode(value, "UTF-8");
|
String encodedValue = URLEncoder.encode(value, "UTF-8");
|
||||||
builder.append(encodedValue).append("&");
|
builder.append(encodedValue).append("&");
|
||||||
}
|
}
|
||||||
|
@ -61,12 +69,18 @@ public class ResourceRegistryClient {
|
||||||
|
|
||||||
Call<EndpointReference, String> call = new Call<EndpointReference, String>() {
|
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 {
|
public String call(EndpointReference endpoint) throws Exception {
|
||||||
|
|
||||||
StringBuilder callUrl = new StringBuilder(endpoint.toString());
|
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
|
||||||
|
|
||||||
|
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
|
||||||
callUrl.append("/").append(AccessPath.ACCESS_PATH_PART)
|
callUrl.append("/").append(AccessPath.ACCESS_PATH_PART)
|
||||||
.append("/");
|
.append("/");
|
||||||
|
|
||||||
appendQueryParameter(callUrl, AccessPath.QUERY_PARAM, query);
|
appendQueryParameter(callUrl, AccessPath.QUERY_PARAM, query);
|
||||||
|
|
||||||
if (fetchPlan != null) {
|
if (fetchPlan != null) {
|
||||||
|
@ -75,11 +89,8 @@ public class ResourceRegistryClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
URL url = new URL(callUrl.toString());
|
URL url = new URL(callUrl.toString());
|
||||||
HttpURLConnection connection = makeRequest(url, "GET", false);
|
HttpURLConnection connection = makeRequest(url, "GET");
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setDoInput(false);
|
|
||||||
connection.setRequestProperty("Content-type", "text/plain");
|
|
||||||
|
|
||||||
logger.debug("Response code for {} is {} : {}",
|
logger.debug("Response code for {} is {} : {}",
|
||||||
callUrl.toString(), connection.getResponseCode(),
|
callUrl.toString(), connection.getResponseCode(),
|
||||||
connection.getResponseMessage());
|
connection.getResponseMessage());
|
||||||
|
@ -100,7 +111,7 @@ public class ResourceRegistryClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
};
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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.informationsystem.resourceregistry.api.exceptions.InvalidQueryException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.client.plugin.ResourceRegistryClientPlugin;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ResourceRegistryClientTest {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory
|
||||||
|
.getLogger(ResourceRegistryClientTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testQuery() throws InvalidQueryException{
|
||||||
|
|
||||||
|
ScopeProvider.instance.set("/gcube/devNext");
|
||||||
|
ResourceRegistryClientPlugin plugin = new ResourceRegistryClientPlugin();
|
||||||
|
|
||||||
|
ResourceRegistryClient rrc = new StatelessBuilderImpl<EndpointReference, ResourceRegistryClient>(plugin).build();
|
||||||
|
|
||||||
|
String res = rrc.query("SELECT FROM V", null);
|
||||||
|
logger.debug(res);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue