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:
parent
b1e3b8ea73
commit
7440fd98ef
|
@ -1,6 +1,5 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.publisher.proxy;
|
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.Facet;
|
||||||
import org.gcube.informationsystem.model.entity.Resource;
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
||||||
|
@ -8,13 +7,6 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
||||||
|
|
||||||
public interface ResourceRegistryPublisher {
|
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 createFacet(Class<F> facetClass, F facet);
|
||||||
|
|
||||||
public <F extends Facet> F updateFacet(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 <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 <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 updateConsistsOf(C consistsOf);
|
||||||
|
|
||||||
public <C extends ConsistsOf<Resource, Facet>> C deleteConsistsOf(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);
|
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo);
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||||
import org.gcube.common.clients.exceptions.ServiceException;
|
import org.gcube.common.clients.exceptions.ServiceException;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.informationsystem.impl.utils.Entities;
|
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.Facet;
|
||||||
import org.gcube.informationsystem.model.entity.Resource;
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
||||||
|
@ -27,17 +26,17 @@ import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher {
|
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;
|
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_STARTER = "?";
|
||||||
public static final String PARAM_EQUALS = "=";
|
public static final String PARAM_EQUALS = "=";
|
||||||
public static final String PARAM_SEPARATOR ="&";
|
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);
|
||||||
|
@ -49,26 +48,34 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
protected final StringWriter stringWriter;
|
protected final StringWriter stringWriter;
|
||||||
protected final String method;
|
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.clazz = clazz;
|
||||||
this.stringWriter = stringWriter;
|
this.stringWriter = stringWriter;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getURLStringFromEndpointReference(EndpointReference endpoint) throws IOException {
|
protected String getURLStringFromEndpointReference(
|
||||||
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(endpoint);
|
EndpointReference endpoint) throws IOException {
|
||||||
|
JaxRSEndpointReference jaxRSEndpointReference = new JaxRSEndpointReference(
|
||||||
|
endpoint);
|
||||||
return jaxRSEndpointReference.toString();
|
return jaxRSEndpointReference.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HttpURLConnection makeRequest(URL url, String method) throws Exception {
|
protected HttpURLConnection makeRequest(URL url, String method)
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
throws Exception {
|
||||||
if (SecurityTokenProvider.instance.get()==null) {
|
HttpURLConnection connection = (HttpURLConnection) url
|
||||||
if(ScopeProvider.instance.get()==null){
|
.openConnection();
|
||||||
throw new RuntimeException("Null Token and Scope. Please set your token first.");
|
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());
|
connection.setRequestProperty("gcube-scope",
|
||||||
}else{
|
ScopeProvider.instance.get());
|
||||||
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY, SecurityTokenProvider.instance.get());
|
} else {
|
||||||
|
connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,
|
||||||
|
SecurityTokenProvider.instance.get());
|
||||||
}
|
}
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.setDoInput(true);
|
connection.setDoInput(true);
|
||||||
|
@ -86,18 +93,18 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
URL url = new URL(callUrl.toString());
|
URL url = new URL(callUrl.toString());
|
||||||
HttpURLConnection connection = makeRequest(url, method);
|
HttpURLConnection connection = makeRequest(url, method);
|
||||||
|
|
||||||
logger.debug("Response code for {} is {} : {}",
|
logger.debug("Response code for {} is {} : {}", callUrl.toString(),
|
||||||
callUrl.toString(), connection.getResponseCode(),
|
connection.getResponseCode(),
|
||||||
connection.getResponseMessage());
|
connection.getResponseMessage());
|
||||||
|
|
||||||
if (connection.getResponseCode() != 200) {
|
if (connection.getResponseCode() != 200) {
|
||||||
throw new Exception("Error Contacting Resource Registry Service");
|
throw new Exception(
|
||||||
|
"Error Contacting Resource Registry Service");
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
try (BufferedReader reader = new BufferedReader(
|
try (BufferedReader reader = new BufferedReader(
|
||||||
new InputStreamReader(
|
new InputStreamReader((InputStream) connection.getContent()))) {
|
||||||
(InputStream) connection.getContent()))) {
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
result.append(line);
|
result.append(line);
|
||||||
|
@ -109,25 +116,6 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
public <F extends Facet> F createFacet(Class<F> facetClass, F facet) {
|
public <F extends Facet> F createFacet(Class<F> facetClass, F facet) {
|
||||||
|
|
||||||
|
@ -144,10 +132,11 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
stringWriter.append(PARAM_EQUALS);
|
stringWriter.append(PARAM_EQUALS);
|
||||||
|
|
||||||
Entities.marshal(facet, stringWriter);
|
Entities.marshal(facet, stringWriter);
|
||||||
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(facetClass, stringWriter, "PUT");
|
ResourceRegistryCall<F> call = new ResourceRegistryCall<>(
|
||||||
|
facetClass, stringWriter, "PUT");
|
||||||
return delegate.make(call);
|
return delegate.make(call);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error Creating Facet", e);
|
logger.error("Error Creating {}", facetClass.getSimpleName(), e);
|
||||||
throw new ServiceException(e);
|
throw new ServiceException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,9 +154,28 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R extends Resource> R createResource(R resource) {
|
public <R extends Resource> R createResource(Class<R> resourceClass,
|
||||||
// TODO Auto-generated method stub
|
R resource) {
|
||||||
return null;
|
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
|
@Override
|
||||||
|
@ -178,7 +186,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
|
public <C extends ConsistsOf<Resource, Facet>> C createConsistsOf(
|
||||||
C consistsOf) {
|
Class<C> consistsOfClass, C consistsOf) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -198,7 +206,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue