diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java index 637ac32..26a9774 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClient.java @@ -13,11 +13,17 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.Cont */ public interface ResourceRegistryContextClient { + public List all() throws ResourceRegistryException; + public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException; public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException; public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException; + + public boolean exist(UUID uuid) throws ResourceRegistryException; + + public boolean exist(String uuid) throws ResourceRegistryException; public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; @@ -35,6 +41,4 @@ public interface ResourceRegistryContextClient { public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException; - public List all() throws ResourceRegistryException; - } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java index 1abf469..d1c7dd4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java @@ -13,6 +13,7 @@ import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; +import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; @@ -57,6 +58,35 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } } + protected List getAllContextFromServer() throws ResourceRegistryException { + try { + logger.trace("Going to read {} with UUID {}", Context.NAME); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + 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 ElementMapper.unmarshalList(Context.class, all); + + } 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 List all() throws ResourceRegistryException { + ContextCache contextCache = ContextCache.getInstance(); + return contextCache.getContexts(); + } + protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException { try { Header header = context.getHeader(); @@ -122,6 +152,45 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } } + public boolean existFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException { + try { + logger.trace("Going to read {} with UUID {}", Context.NAME, uuid); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(ContextPath.CONTEXTS_PATH_PART); + gxHTTPStringRequest.path(uuid); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); + HTTPUtility.getResponse(String.class, httpURLConnection); + + return true; + } catch (NotFoundException e) { + return false; + } 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 boolean exist(String uuid) throws ResourceRegistryException { + return exist(UUID.fromString(uuid)); + } + + @Override + public boolean exist(UUID uuid) throws ResourceRegistryException { + try { + read(uuid); + return true; + }catch (ContextNotFoundException e) { + return false; + } + } + @Override public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException { return read(context.getHeader().getUUID()); @@ -318,33 +387,4 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } } - protected List getAllContextFromServer() throws ResourceRegistryException { - try { - logger.trace("Going to read {} with UUID {}", Context.NAME); - GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); - gxHTTPStringRequest.from(ResourceRegistryContextClient.class.getSimpleName()); - gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); - 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 ElementMapper.unmarshalList(Context.class, all); - - } 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 List all() throws ResourceRegistryException { - ContextCache contextCache = ContextCache.getInstance(); - return contextCache.getContexts(); - } - }