Refs #11455: Integrate GX REST in resource-registry client libraries

Task-Url: https://support.d4science.org/issues/11455

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@176626 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-17 10:18:02 +00:00
parent b6e9a3bf45
commit ce75bf5658
1 changed files with 56 additions and 85 deletions

View File

@ -1,12 +1,12 @@
package org.gcube.informationsystem.resourceregistry.publisher;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.util.UUID;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.model.impl.utils.ISMapper;
@ -29,8 +29,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resour
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath;
import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -38,22 +37,12 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryPublisherImpl.class);
public static final String PATH_SEPARATOR = "/";
protected final String address;
protected HTTPCall httpCall;
public ResourceRegistryPublisherImpl(String address) {
this.address = address;
}
protected HTTPCall getHTTPCall() throws MalformedURLException {
if(httpCall == null) {
httpCall = new HTTPCall(address, ResourceRegistryPublisher.class.getSimpleName());
}
return httpCall;
}
private static String getCurrentContext() {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = null;
@ -69,16 +58,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
logger.debug("Going to read current {} ({}) definition", Context.NAME, getCurrentContext());
try {
logger.info("Going to get current {} ", Context.NAME);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.ACCESS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(AccessPath.CURRENT_CONTEXT);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CURRENT_CONTEXT);
HTTPCall httpCall = getHTTPCall();
Context context = httpCall.call(Context.class, stringWriter.toString(), HTTPMETHOD.GET);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
Context context = HTTPUtility.getResponse(Context.class, httpURLConnection);
logger.debug("Got Context is {}", ISMapper.marshal(context));
return context.getHeader().getUUID();
@ -99,16 +86,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
throws AlreadyPresentException, ResourceRegistryException {
try {
logger.trace("Going to create {} : {}", erType, er);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(InstancePath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, er);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(er);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.trace("{} successfully created", ret);
return ret;
@ -193,16 +178,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
public String read(String erType, UUID uuid) throws NotFoundException, ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", erType, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(InstancePath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got {} with UUID {} is {}", erType, uuid, ret);
return ret;
@ -220,16 +203,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
throws AlreadyPresentException, ResourceRegistryException {
try {
logger.trace("Going to create {} : {}", erType, er);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(InstancePath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, er);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(er);
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.trace("{} with UUID {} successfully created : {}", erType, uuid, ret);
return ret;
@ -308,16 +289,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
public boolean delete(String erType, UUID uuid) throws NotFoundException, ResourceRegistryException {
try {
logger.trace("Going to delete {} with UUID {}", erType, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(InstancePath.INSTANCES_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
boolean deleted = httpCall.call(Boolean.class, stringWriter.toString(), HTTPMETHOD.DELETE);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete();
boolean deleted = HTTPUtility.getResponse(Boolean.class, httpURLConnection);
logger.info("{} with UUID {} {}", erType, uuid, deleted ? " successfully deleted" : "was NOT deleted");
return deleted;
@ -511,20 +490,16 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
try {
logger.trace("Going to add {} with UUID {} to {} with UUID {} ", erType, instanceUUID, Context.NAME,
contextUUID);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(SharingPath.SHARING_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(SharingPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(contextUUID.toString());
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(instanceUUID.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(SharingPath.SHARING_PATH_PART);
gxHTTPStringRequest.path(SharingPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(contextUUID.toString());
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(instanceUUID.toString());
HTTPCall httpCall = getHTTPCall();
boolean added = httpCall.call(Boolean.class, stringWriter.toString(), HTTPMETHOD.PUT);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put();
boolean added = HTTPUtility.getResponse(Boolean.class, httpURLConnection);
logger.info("{} with UUID {} {} to {} with UUID {}", erType, instanceUUID,
added ? " successfully added" : "was NOT added", Context.NAME, contextUUID);
@ -574,20 +549,16 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
try {
logger.trace("Going to add {} with UUID {} to {} with UUID {} ", erType, instanceUUID, Context.NAME,
contextUUID);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(SharingPath.SHARING_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(SharingPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(contextUUID.toString());
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(erType);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(instanceUUID.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.path(SharingPath.SHARING_PATH_PART);
gxHTTPStringRequest.path(SharingPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(contextUUID.toString());
gxHTTPStringRequest.path(erType);
gxHTTPStringRequest.path(instanceUUID.toString());
HTTPCall httpCall = getHTTPCall();
boolean removed = httpCall.call(Boolean.class, stringWriter.toString(), HTTPMETHOD.DELETE);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete();
boolean removed = HTTPUtility.getResponse(Boolean.class, httpURLConnection);
logger.info("{} with UUID {} {} to {} with UUID {}", erType, instanceUUID,
removed ? " successfully removed" : "was NOT removed", Context.NAME, contextUUID);