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 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); public <F extends Facet> boolean deleteFacet(F facet);

View File

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

View File

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