2016-09-14 14:49:50 +02:00
|
|
|
package org.gcube.informationsystem.resourceregistry.publisher.proxy;
|
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.StringWriter;
|
2016-09-14 14:49:50 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2016-09-14 18:08:46 +02:00
|
|
|
|
|
|
|
import javax.xml.ws.EndpointReference;
|
2016-09-14 14:49:50 +02:00
|
|
|
|
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-09-14 18:08:46 +02:00
|
|
|
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;
|
2016-09-14 14:49:50 +02:00
|
|
|
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 {
|
|
|
|
|
2016-09-15 13:46:48 +02:00
|
|
|
private static final Logger logger = LoggerFactory
|
|
|
|
.getLogger(ResourceRegistryPublisher.class);
|
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
private final AsyncProxyDelegate<EndpointReference> delegate;
|
|
|
|
|
2016-09-15 13:46:48 +02:00
|
|
|
public static final String PATH_SEPARATOR = "/";
|
|
|
|
public static final String PARAM_STARTER = "?";
|
2016-09-14 18:08:46 +02:00
|
|
|
public static final String PARAM_EQUALS = "=";
|
2016-09-15 13:46:48 +02:00
|
|
|
public static final String PARAM_SEPARATOR = "&";
|
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
public ResourceRegistryPublisherImpl(ProxyDelegate<EndpointReference> config) {
|
|
|
|
this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
class ResourceRegistryCall<C> implements Call<EndpointReference, C> {
|
|
|
|
|
|
|
|
protected final Class<C> clazz;
|
|
|
|
protected final StringWriter stringWriter;
|
|
|
|
protected final String method;
|
2016-09-15 13:46:48 +02:00
|
|
|
|
|
|
|
public ResourceRegistryCall(Class<C> clazz, StringWriter stringWriter,
|
|
|
|
String method) {
|
2016-09-14 18:08:46 +02:00
|
|
|
this.clazz = clazz;
|
|
|
|
this.stringWriter = stringWriter;
|
|
|
|
this.method = method;
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
|
|
|
|
protected String getURLStringFromEndpointReference(
|
|
|
|
EndpointReference endpoint) throws IOException {
|
|
|
|
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(
|
|
|
|
endpoint);
|
2016-09-14 18:08:46 +02:00
|
|
|
return jaxRSEndpointReference.toString();
|
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
|
|
|
|
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.");
|
2016-09-14 18:08:46 +02:00
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
connection.setRequestProperty("gcube-scope",
|
|
|
|
ScopeProvider.instance.get());
|
|
|
|
} else {
|
|
|
|
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,
|
|
|
|
SecurityTokenProvider.instance.get());
|
2016-09-14 18:08:46 +02:00
|
|
|
}
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setRequestProperty("Content-type", "text/plain");
|
|
|
|
connection.setRequestMethod(method);
|
|
|
|
return connection;
|
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
@Override
|
|
|
|
public C call(EndpointReference endpoint) throws Exception {
|
|
|
|
String urlFromEndpointReference = getURLStringFromEndpointReference(endpoint);
|
|
|
|
StringBuilder callUrl = new StringBuilder(urlFromEndpointReference);
|
|
|
|
callUrl.append(stringWriter.toString());
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
HttpURLConnection connection = makeRequest(url, method);
|
2016-09-15 13:46:48 +02:00
|
|
|
|
|
|
|
logger.debug("Response code for {} is {} : {}", callUrl.toString(),
|
|
|
|
connection.getResponseCode(),
|
2016-09-14 18:08:46 +02:00
|
|
|
connection.getResponseMessage());
|
|
|
|
|
|
|
|
if (connection.getResponseCode() != 200) {
|
2016-09-15 13:46:48 +02:00
|
|
|
throw new Exception(
|
|
|
|
"Error Contacting Resource Registry Service");
|
2016-09-14 18:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
try (BufferedReader reader = new BufferedReader(
|
2016-09-15 13:46:48 +02:00
|
|
|
new InputStreamReader((InputStream) connection.getContent()))) {
|
2016-09-14 18:08:46 +02:00
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
result.append(line);
|
|
|
|
}
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
return Entities.unmarshal(clazz, result.toString());
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-14 18:08:46 +02:00
|
|
|
public <F extends Facet> F createFacet(Class<F> facetClass, F facet) {
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
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);
|
2016-09-15 13:46:48 +02:00
|
|
|
|
2016-09-14 18:08:46 +02:00
|
|
|
Entities.marshal(facet, stringWriter);
|
2016-09-15 13:46:48 +02:00
|
|
|
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(
|
|
|
|
facetClass, stringWriter, "PUT");
|
2016-09-14 18:08:46 +02:00
|
|
|
return delegate.make(call);
|
|
|
|
} catch (Exception e) {
|
2016-09-15 13:46:48 +02:00
|
|
|
logger.error("Error Creating {}", facetClass.getSimpleName(), e);
|
2016-09-14 18:08:46 +02:00
|
|
|
throw new ServiceException(e);
|
|
|
|
}
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <F extends Facet> F updateFacet(F facet) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <F extends Facet> F deleteFacet(F facet) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-15 13:46:48 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-09-14 14:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <R extends Resource> R deleteResource(R resource) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
|
2016-09-15 13:46:48 +02:00
|
|
|
Class<C> consistsOfClass, C consistsOf) {
|
2016-09-14 14:49:50 +02:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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>> C deleteConsistsOf(
|
|
|
|
C consistsOf) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-15 13:46:48 +02:00
|
|
|
public <I extends IsRelatedTo<Resource, Resource>> I create(
|
|
|
|
Class<I> isRelatedToClass, I isRelatedTo) {
|
2016-09-14 14:49:50 +02:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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>> I delete(I isRelatedTo) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|