diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java index b76a533..2a2f972 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java @@ -13,16 +13,17 @@ import org.gcube.informationsystem.types.reference.Type; */ public interface ResourceRegistrySchemaClient { - public Type create(Class clz) - throws SchemaException, ResourceRegistryException; - - public String create(String typeDefinitition) - throws SchemaException, ResourceRegistryException; - + public Type create(Class clz) throws SchemaException, ResourceRegistryException; + + public String create(String typeDefinitition) throws SchemaException, ResourceRegistryException; + + public boolean exist(Class clz) throws ResourceRegistryException; + + public boolean exist(String typeName) throws ResourceRegistryException; + public List read(Class clz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; - - public String read(String typeName, Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException; - + + public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java index afdde28..f07c576 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java @@ -8,6 +8,7 @@ import java.util.Map; import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.informationsystem.base.reference.Element; +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; @@ -76,6 +77,45 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } } + @Override + public boolean exist(Class clz) throws ResourceRegistryException { + try { + String typeName = Utility.getTypeName(clz); + return exist(typeName); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean exist(String typeName) throws ResourceRegistryException { + try { + logger.info("Going to get {} schema", typeName); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistrySchemaClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(TypePath.TYPES_PATH_PART); + gxHTTPStringRequest.path(typeName); + + Map parameters = new HashMap<>(); + parameters.put(TypePath.POLYMORPHIC_PARAM, Boolean.FALSE.toString()); + gxHTTPStringRequest.queryParams(parameters); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + HTTPUtility.getResponse(String.class, httpURLConnection); + + return true; + } catch (NotFoundException e) { + return false; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + @Override public List read(Class clz, Boolean polymorphic)