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-context-client@176626 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-17 10:18:02 +00:00
parent 34b138c77f
commit 0e6d73e3cd
1 changed files with 88 additions and 106 deletions

View File

@ -1,10 +1,10 @@
package org.gcube.informationsystem.resourceregistry.context;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.UUID;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.informationsystem.model.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.model.impl.utils.ISMapper;
import org.gcube.informationsystem.model.reference.embedded.Header;
@ -13,8 +13,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
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;
@ -22,31 +21,20 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceRegistryContextClientImpl implements ResourceRegistryContextClient {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientImpl.class);
public static final String PATH_SEPARATOR = "/";
protected final String address;
protected HTTPCall httpCall;
public ResourceRegistryContextClientImpl(String address) {
this.address = address;
}
private HTTPCall getHTTPCall() throws MalformedURLException {
if (httpCall == null) {
httpCall = new HTTPCall(address, ResourceRegistryContextClient.class.getSimpleName());
}
return httpCall;
}
protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Header header = context.getHeader();
if(header==null) {
if(header == null) {
header = new HeaderImpl(UUID.randomUUID());
context.setHeader(header);
}
@ -56,22 +44,21 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
logger.trace("Going to create {}", contextString);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(ContextPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid.toString());
HTTPCall httpCall = getHTTPCall();
String c = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, contextString);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.trace("{} successfully created", c);
return c;
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
@ -82,69 +69,67 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
try {
String res = internalCreate(context);
return ISMapper.unmarshal(Context.class, res);
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Context c = ISMapper.unmarshal(Context.class, context);
return internalCreate(c);
} catch (ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Context c = ISMapper.unmarshal(Context.class, context);
return internalCreate(c);
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException {
return read(context.getHeader().getUUID());
}
@Override
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
String res = read(uuid.toString());
return ISMapper.unmarshal(Context.class, res);
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public String read(String uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(ContextPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid);
HTTPCall httpCall = getHTTPCall();
String c = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got {} is {}", Context.NAME, c);
return c;
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
@ -157,22 +142,21 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
UUID uuid = context.getHeader().getUUID();
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(ContextPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid.toString());
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid.toString());
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
HTTPCall httpCall = getHTTPCall();
String c = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, contextString);
logger.trace("{} successfully updated", c);
return c;
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
@ -183,86 +167,84 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
try {
String res = internalUpdate(context);
return ISMapper.unmarshal(Context.class, res);
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Updating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Updating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public String update(String context) throws ResourceRegistryException {
try {
Context c = ISMapper.unmarshal(Context.class, context);
return internalUpdate(c);
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Updating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Updating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public boolean delete(Context context) throws ContextNotFoundException, ResourceRegistryException {
return delete(context.getHeader().getUUID());
}
@Override
public boolean delete(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
return delete(uuid.toString());
}
@Override
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.trace("Going to delete {} with UUID {}", Context.NAME, uuid);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(ContextPath.CONTEXTS_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(uuid);
HTTPCall httpCall = getHTTPCall();
boolean deleted = httpCall.call(Boolean.class, stringWriter.toString(), HTTPMETHOD.DELETE);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
gxHTTPStringRequest.path(uuid);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete();
boolean deleted = HTTPUtility.getResponse(Boolean.class, httpURLConnection);
logger.info("{} with UUID {} {}", Context.NAME, uuid,
deleted ? " successfully deleted" : "was NOT deleted");
return deleted;
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@Override
public List<Context> all() throws ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME);
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(ContextPath.CONTEXTS_PATH_PART);
HTTPCall httpCall = getHTTPCall();
String all = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName());
gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String all = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("Got contexts are {}", Context.NAME, all);
return ISMapper.unmarshalList(Context.class, all);
} catch (ResourceRegistryException e) {
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
}