resource-registry-publisher/src/main/java/org/gcube/informationsystem/resourceregistry/publisher/proxy/ResourceRegistryPublisherIm...

341 lines
12 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.publisher.proxy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.ws.EndpointReference;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.AsyncProxyDelegate;
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.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
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 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);
}
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) {
this.clazz = clazz;
this.stringWriter = stringWriter;
this.method = method;
}
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.");
}
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);
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(),
connection.getResponseMessage());
if (connection.getResponseCode() != 200) {
throw new Exception(
"Error Contacting Resource Registry Service");
}
StringBuilder result = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader((InputStream) connection.getContent()))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}
return Entities.unmarshal(clazz, result.toString());
}
}
@Override
public <F extends Facet> F createFacet(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(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");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating {}", facetClass.getSimpleName(), e);
throw new ServiceException(e);
}
}
@Override
public <F extends Facet> F updateFacet(F facet) {
// TODO Auto-generated method stub
return null;
}
@Override
public <F extends Facet> boolean deleteFacet(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());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", facet, e);
throw new ServiceException(e);
}
}
@Override
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
public <R extends Resource> boolean deleteResource(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(resource.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", resource, e);
throw new ServiceException(e);
}
}
@Override
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
Class<C> consistsOfClass, C consistsOf) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.CONSISTS_OF_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.SOURCE_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(consistsOf.getSource().getHeader().getUUID().toString());
stringWriter.append(PATH_SEPARATOR);
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);
ResourceRegistryCall<C> call = new ResourceRegistryCall<>(
consistsOfClass, stringWriter, "PUT");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
throw new ServiceException(e);
}
}
@Override
public <C extends ConsistsOf<Resource, Facet>> C updateConsistsOf(
C consistsOf) {
// TODO Auto-generated method stub
return null;
}
@Override
public <C extends ConsistsOf<Resource, Facet>> boolean deleteConsistsOf(
C consistsOf) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.CONSISTS_OF_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(consistsOf.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", consistsOf, e);
throw new ServiceException(e);
}
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I create(
Class<I> isRelatedToClass, I isRelatedTo) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.IS_RELATED_TO_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.SOURCE_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(isRelatedTo.getSource().getHeader().getUUID().toString());
stringWriter.append(PATH_SEPARATOR);
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);
ResourceRegistryCall<I> call = new ResourceRegistryCall<>(
isRelatedToClass, stringWriter, "PUT");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Creating Facet", e);
throw new ServiceException(e);
}
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo) {
// TODO Auto-generated method stub
return null;
}
@Override
public <I extends IsRelatedTo<Resource, Resource>> boolean delete(I isRelatedTo) {
try {
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.IS_RELATED_TO_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(isRelatedTo.getHeader().getUUID().toString());
ResourceRegistryCall<Boolean> call = new ResourceRegistryCall<>(
Boolean.class, stringWriter, "DELETE");
return delegate.make(call);
} catch (Exception e) {
logger.error("Error Removing {}", isRelatedTo, e);
throw new ServiceException(e);
}
}
}