implementing Publisher

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@131413 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-15 15:24:24 +00:00
parent fc70aaffa6
commit a677cb07a0
3 changed files with 194 additions and 74 deletions

View File

@ -9,7 +9,7 @@ public interface ResourceRegistryPublisher {
public <F extends Facet> F createFacet(Class<F> facetClass, F facet);
public <F extends Facet> F updateFacet(F facet);
public <F extends Facet> F updateFacet(Class<F> facetClass, F facet);
public <F extends Facet> boolean deleteFacet(F facet);

View File

@ -1,12 +1,17 @@
package org.gcube.informationsystem.resourceregistry.publisher.proxy;
import java.io.BufferedReader;
import java.io.DataOutputStream;
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.HashMap;
import java.util.Map;
import javax.xml.ws.EndpointReference;
@ -34,25 +39,99 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
private final AsyncProxyDelegate<EndpointReference> delegate;
public static final String PATH_SEPARATOR = "/";
public static final String PARAM_STARTER = "?";
public static final String PARAM_EQUALS = "=";
public static final String PARAM_SEPARATOR = "&";
public ResourceRegistryPublisherImpl(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(Map<String, String> parameters) throws UnsupportedEncodingException {
if(parameters==null){
return null;
}
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : parameters.entrySet()){
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,
Map<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 StringWriter stringWriter;
protected final String method;
protected final HTTPInputs httpInputs;
public ResourceRegistryCall(Class<C> clazz, StringWriter stringWriter,
String method) {
public ResourceRegistryCall(Class<C> clazz, HTTPInputs httpInputs) {
this.clazz = clazz;
this.stringWriter = stringWriter;
this.method = method;
this.httpInputs = httpInputs;
}
protected String getURLStringFromEndpointReference(
@ -62,8 +141,13 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return jaxRSEndpointReference.toString();
}
protected HttpURLConnection makeRequest(URL url, String method)
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) {
@ -78,9 +162,19 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
SecurityTokenProvider.instance.get());
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/plain");
connection.setRequestMethod(method);
connection.setRequestProperty("User-Agent", ResourceRegistryPublisher.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;
}
@ -88,16 +182,17 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
public C call(EndpointReference endpoint) throws Exception {
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
callUrl.append(stringWriter.toString());
callUrl.append(httpInputs.getPath());
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, method);
logger.debug("Response code for {} is {} : {}", callUrl.toString(),
HttpURLConnection connection = getConnection(url, httpInputs.method);
logger.debug("Response code for {} is {} : {}",
connection.getURL(),
connection.getResponseCode(),
connection.getResponseMessage());
if (connection.getResponseCode() != 200) {
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new Exception(
"Error Contacting Resource Registry Service");
}
@ -127,24 +222,44 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(EntityPath.FACET_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facetClass.getSimpleName());
stringWriter.append(PARAM_STARTER);
stringWriter.append(EntityPath.DEFINITION_PARAM);
stringWriter.append(PARAM_EQUALS);
Entities.marshal(facet, stringWriter);
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(
facetClass, stringWriter, "PUT");
Map<String, String> parameters = new HashMap<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(facetClass, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating {}", facetClass.getSimpleName(), e);
logger.error("Error Creating {}", facet, e);
throw new ServiceException(e);
}
}
@Override
public <F extends Facet> F updateFacet(F facet) {
// TODO Auto-generated method stub
return null;
public <F extends Facet> F updateFacet(Class<F> facetClass, F facet) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.FACET_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facet.getHeader().getUUID().toString());
Map<String, String> parameters = new HashMap<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, parameters);
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(facetClass, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Updating {}", facet, e);
throw new ServiceException(e);
}
}
@Override
@ -158,8 +273,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facet.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", facet, e);
@ -178,13 +295,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(EntityPath.RESOURCE_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(resourceClass.getSimpleName());
stringWriter.append(PARAM_STARTER);
stringWriter.append(EntityPath.DEFINITION_PARAM);
stringWriter.append(PARAM_EQUALS);
Map<String, String> parameters = new HashMap<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(resource));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
ResourceRegistryCall<R> call = new ResourceRegistryCall<>(resourceClass, httpInputs);
Entities.marshal(resource, stringWriter);
ResourceRegistryCall<R> call = new ResourceRegistryCall<>(
resourceClass, stringWriter, "PUT");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
@ -203,8 +321,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(resource.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", resource, e);
@ -229,17 +349,16 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(EntityPath.TARGET_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(consistsOf.getTarget().getHeader().getUUID().toString());
stringWriter.append(PARAM_STARTER);
stringWriter.append(EntityPath.TYPE_PARAM);
stringWriter.append(PARAM_EQUALS);
stringWriter.append(consistsOfClass.getSimpleName());
stringWriter.append(PARAM_SEPARATOR);
stringWriter.append(EntityPath.PROPERTIES_PARAM);
stringWriter.append(PARAM_EQUALS);
Entities.marshal(consistsOf, stringWriter);
Map<String, String> parameters = new HashMap<>();
parameters.put(EntityPath.TYPE_PARAM, consistsOfClass.getSimpleName());
parameters.put(EntityPath.PROPERTIES_PARAM, Entities.marshal(consistsOf));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
ResourceRegistryCall<C> call = new ResourceRegistryCall<>(
consistsOfClass, stringWriter, "PUT");
ResourceRegistryCall<C> call = new ResourceRegistryCall<>(consistsOfClass, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
@ -266,8 +385,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(consistsOf.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", consistsOf, e);
@ -293,17 +414,16 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(EntityPath.TARGET_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(isRelatedTo.getTarget().getHeader().getUUID().toString());
stringWriter.append(PARAM_STARTER);
stringWriter.append(EntityPath.TYPE_PARAM);
stringWriter.append(PARAM_EQUALS);
stringWriter.append(isRelatedToClass.getSimpleName());
stringWriter.append(PARAM_SEPARATOR);
stringWriter.append(EntityPath.PROPERTIES_PARAM);
stringWriter.append(PARAM_EQUALS);
Entities.marshal(isRelatedTo, stringWriter);
Map<String, String> parameters = new HashMap<>();
parameters.put(EntityPath.TYPE_PARAM, isRelatedToClass.getSimpleName());
parameters.put(EntityPath.PROPERTIES_PARAM, Entities.marshal(isRelatedTo));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
ResourceRegistryCall<I> call = new ResourceRegistryCall<>(
isRelatedToClass, stringWriter, "PUT");
isRelatedToClass, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
@ -328,8 +448,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(isRelatedTo.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null);
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(Boolean.class, httpInputs);
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", isRelatedTo, e);

View File

@ -3,10 +3,7 @@
*/
package org.gcube.informationsystem.resourceregistry.publisher;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.entity.DummyFacet;
import org.gcube.informationsystem.impl.entity.facet.ContactFacetImpl;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.facet.ContactFacet;
@ -35,21 +32,22 @@ public class ResourceRegistryPublisherTest {
}
@Test
public void testCreateFacet(){
public void testCreateUpdateDeleteFacet(){
ContactFacet contactFacet = new ContactFacetImpl();
contactFacet.setName("Luca");
contactFacet.setSurname("Frosini");
contactFacet.setEMail("info@lucafrosini.com");
logger.debug("Going to created {}", contactFacet);
ContactFacet created = resourceRegistryPublisher.createFacet(ContactFacet.class, contactFacet);
logger.trace("Created {} is {}", ContactFacet.NAME, created);
}
@Test
public void testRemoveFacet(){
Facet facet = new DummyFacet(UUID.fromString("03082640-289d-403e-8155-adc6b9276a04"));
boolean deleted = resourceRegistryPublisher.deleteFacet(facet);
logger.trace("{} with UUID {} deleted : {}", Facet.NAME, facet.getHeader().getUUID(), deleted);
logger.trace("Created {}", created);
created.setTitle("Dott. Ing.");
ContactFacet updated = resourceRegistryPublisher.updateFacet(ContactFacet.class, created);
logger.trace("Updated {}", updated);
boolean deleted = resourceRegistryPublisher.deleteFacet(updated);
logger.trace("{} {} deleted : {}", Facet.NAME, updated, deleted);
}
}