diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java index 0414f1b..19c5c5a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisher.java @@ -9,7 +9,7 @@ public interface ResourceRegistryPublisher { public F createFacet(Class facetClass, F facet); - public F updateFacet(F facet); + public F updateFacet(Class facetClass, F facet); public boolean deleteFacet(F facet); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java index c5189ce..fd82799 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherImpl.java @@ -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 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 config) { this.delegate = new AsyncProxyDelegate(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 parameters) throws UnsupportedEncodingException { + if(parameters==null){ + return null; + } + + StringBuilder result = new StringBuilder(); + boolean first = true; + for(Map.Entry 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 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 implements Call { protected final Class clazz; - protected final StringWriter stringWriter; - protected final String method; + protected final HTTPInputs httpInputs; - public ResourceRegistryCall(Class clazz, StringWriter stringWriter, - String method) { + public ResourceRegistryCall(Class 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 call = new ResourceRegistryCall<>( - facetClass, stringWriter, "PUT"); + Map parameters = new HashMap<>(); + parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); + + ResourceRegistryCall 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 updateFacet(F facet) { - // TODO Auto-generated method stub - return null; + public F updateFacet(Class 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 parameters = new HashMap<>(); + parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, parameters); + + ResourceRegistryCall 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 call = new ResourceRegistryCall<>( - Boolean.class, stringWriter, "DELETE"); + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null); + + ResourceRegistryCall 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 parameters = new HashMap<>(); + parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(resource)); + + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); + + ResourceRegistryCall call = new ResourceRegistryCall<>(resourceClass, httpInputs); - Entities.marshal(resource, stringWriter); - ResourceRegistryCall 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 call = new ResourceRegistryCall<>( - Boolean.class, stringWriter, "DELETE"); + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null); + + ResourceRegistryCall 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 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 call = new ResourceRegistryCall<>( - consistsOfClass, stringWriter, "PUT"); + ResourceRegistryCall 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 call = new ResourceRegistryCall<>( - Boolean.class, stringWriter, "DELETE"); + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null); + + ResourceRegistryCall 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 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 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 call = new ResourceRegistryCall<>( - Boolean.class, stringWriter, "DELETE"); + HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.DELETE, null); + + ResourceRegistryCall call = new ResourceRegistryCall<>(Boolean.class, httpInputs); + return delegate.make(call); } catch (Exception e) { logger.error("Error Removing {}", isRelatedTo, e); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherTest.java index c2559b9..2224d20 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherTest.java @@ -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); } }