implementing Publisher

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@131377 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-15 11:46:48 +00:00
parent b1e3b8ea73
commit 7440fd98ef
2 changed files with 73 additions and 71 deletions

View File

@ -1,6 +1,5 @@
package org.gcube.informationsystem.resourceregistry.publisher.proxy;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf;
@ -8,13 +7,6 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
public interface ResourceRegistryPublisher {
public <E extends Embedded> E createEmbedded(E embedded);
public <E extends Embedded> E updateEmbedded(E embedded);
public <E extends Embedded> E deleteEmbedded(E embedded);
public <F extends Facet> F createFacet(Class<F> facetClass, F facet);
public <F extends Facet> F updateFacet(F facet);
@ -22,19 +14,20 @@ public interface ResourceRegistryPublisher {
public <F extends Facet> F deleteFacet(F facet);
public <R extends Resource> R createResource(R resource);
public <R extends Resource> R createResource(Class<R> resourceClass, R resource);
public <R extends Resource> R deleteResource(R resource);
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(C consistsOf);
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(Class<C> consistsOfClass, C consistsOf);
public <C extends ConsistsOf<Resource, Facet>> C updateConsistsOf(C consistsOf);
public <C extends ConsistsOf<Resource, Facet>> C deleteConsistsOf(C consistsOf);
public <I extends IsRelatedTo<Resource, Resource>> I create(I isRelatedTo);
public <I extends IsRelatedTo<Resource, Resource>> I create(Class<I> isRelatedToClass, I isRelatedTo);
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo);

View File

@ -18,7 +18,6 @@ import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.clients.exceptions.ServiceException;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf;
@ -27,48 +26,56 @@ import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryPublisher.class);
private static final Logger logger = LoggerFactory
.getLogger(ResourceRegistryPublisher.class);
private final AsyncProxyDelegate<EndpointReference> delegate;
public static final String PATH_SEPARATOR ="/";
public static final String PARAM_STARTER ="?";
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 static final String PARAM_SEPARATOR = "&";
public ResourceRegistryPublisherImpl(ProxyDelegate<EndpointReference> config) {
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
}
class ResourceRegistryCall<C> implements Call<EndpointReference, C> {
protected final Class<C> clazz;
protected final StringWriter stringWriter;
protected final String method;
public ResourceRegistryCall(Class<C> clazz, StringWriter stringWriter, String method){
public ResourceRegistryCall(Class<C> clazz, StringWriter stringWriter,
String method) {
this.clazz = clazz;
this.stringWriter = stringWriter;
this.method = method;
}
protected String getURLStringFromEndpointReference(EndpointReference endpoint) throws IOException {
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint);
protected String getURLStringFromEndpointReference(
EndpointReference endpoint) throws IOException {
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(
endpoint);
return jaxRSEndpointReference.toString();
}
protected HttpURLConnection makeRequest(URL url, String method) throws Exception {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (SecurityTokenProvider.instance.get()==null) {
if(ScopeProvider.instance.get()==null){
throw new RuntimeException("Null Token and Scope. Please set your token first.");
protected HttpURLConnection makeRequest(URL url, String method)
throws Exception {
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
if (SecurityTokenProvider.instance.get() == null) {
if (ScopeProvider.instance.get() == null) {
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.setRequestProperty("gcube-scope",
ScopeProvider.instance.get());
} else {
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,
SecurityTokenProvider.instance.get());
}
connection.setDoOutput(true);
connection.setDoInput(true);
@ -76,61 +83,42 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
connection.setRequestMethod(method);
return connection;
}
@Override
public C call(EndpointReference endpoint) throws Exception {
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
callUrl.append(stringWriter.toString());
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, method);
logger.debug("Response code for {} is {} : {}",
callUrl.toString(), connection.getResponseCode(),
logger.debug("Response code for {} is {} : {}", callUrl.toString(),
connection.getResponseCode(),
connection.getResponseMessage());
if (connection.getResponseCode() != 200) {
throw new Exception("Error Contacting Resource Registry Service");
throw new Exception(
"Error Contacting Resource Registry Service");
}
StringBuilder result = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
(InputStream) connection.getContent()))) {
new InputStreamReader((InputStream) connection.getContent()))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}
return Entities.unmarshal(clazz, result.toString());
}
}
@Override
public <E extends Embedded> E createEmbedded(E embedded) {
return null;
}
@Override
public <E extends Embedded> E updateEmbedded(E embedded) {
// TODO Auto-generated method stub
return null;
}
@Override
public <E extends Embedded> E deleteEmbedded(E embedded) {
// TODO Auto-generated method stub
return null;
}
@Override
public <F extends Facet> F createFacet(Class<F> facetClass, F facet) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
@ -142,12 +130,13 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
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");
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(
facetClass, stringWriter, "PUT");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
logger.error("Error Creating {}", facetClass.getSimpleName(), e);
throw new ServiceException(e);
}
}
@ -165,9 +154,28 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
}
@Override
public <R extends Resource> R createResource(R resource) {
// TODO Auto-generated method stub
return null;
public <R extends Resource> R createResource(Class<R> resourceClass,
R resource) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
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);
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);
throw new ServiceException(e);
}
}
@Override
@ -178,7 +186,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
@Override
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
C consistsOf) {
Class<C> consistsOfClass, C consistsOf) {
// TODO Auto-generated method stub
return null;
}
@ -198,7 +206,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I create(I isRelatedTo) {
public <I extends IsRelatedTo<Resource, Resource>> I create(
Class<I> isRelatedToClass, I isRelatedTo) {
// TODO Auto-generated method stub
return null;
}