package org.gcube.informationsystem.resourceregistry.publisher; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.isrelatedto.IsRelatedToNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.informationsystem.utils.UUIDManager; import org.gcube.informationsystem.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher { private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryPublisherImpl.class); protected final String address; protected boolean hierarchicalMode; protected boolean includeContextsInHeader; @Override public boolean isHierarchicalMode() { return hierarchicalMode; } @Override public void setHierarchicalMode(boolean hierarchicalMode) { this.hierarchicalMode = hierarchicalMode; } @Override public boolean isIncludeContextsInHeader() { return includeContextsInHeader; } @Override public void setIncludeContextsInHeader(boolean includeContextsInHeader) { this.includeContextsInHeader = includeContextsInHeader; } private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException { return includeAdditionalQueryParameters(gxHTTPStringRequest, null); } private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest, Map queryParams) throws UnsupportedEncodingException { gxHTTPStringRequest = checkHierarchicalMode(gxHTTPStringRequest, queryParams); return checkIncludeContextsInInstanceHeader(gxHTTPStringRequest, queryParams); } private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, Map queryParams) throws UnsupportedEncodingException { if(hierarchicalMode) { if(queryParams==null) { queryParams = new HashMap<>(); } queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode)); } return gxHTTPStringRequest.queryParams(queryParams); } private GXHTTPStringRequest checkIncludeContextsInInstanceHeader(GXHTTPStringRequest gxHTTPStringRequest, Map queryParams) throws UnsupportedEncodingException { if(includeContextsInHeader) { if(queryParams==null) { queryParams = new HashMap<>(); } queryParams.put(AccessPath.INCLUDE_CONTEXTS_IN_HEADER_QUERY_PARAMETER, Boolean.toString(includeContextsInHeader)); } return gxHTTPStringRequest.queryParams(queryParams); } protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { @Override public List renew() throws ResourceRegistryException { return getAllContextFromServer(); } }; public ResourceRegistryPublisherImpl(String address) { this.address = address; ContextCache contextCache = ContextCache.getInstance(); contextCache.setContextCacheRenewal(contextCacheRenewal); } public List getAllContextFromServer() throws ResourceRegistryException { try { logger.info("Going to read all {}s", Context.NAME); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got Contexts are {}", ret); return ElementMapper.unmarshalList(Context.class, ret); } catch (ResourceRegistryException e) { // logger.trace("Error while getting {} schema for {}", polymorphic ? // AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); throw e; } catch (Exception e) { // logger.trace("Error while getting {}schema for {}", polymorphic ? // AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); throw new RuntimeException(e); } } @Override public List getAllContext() throws ResourceRegistryException { ContextCache contextCache = ContextCache.getInstance(); return contextCache.getContexts(); } protected Context getContextFromServer(String id) throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to get current {} ", Context.NAME); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); gxHTTPStringRequest.path(id); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); Context context = HTTPUtility.getResponse(Context.class, httpURLConnection); logger.debug("Got Context is {}", ElementMapper.marshal(context)); return context; } catch (ResourceRegistryException e) { // logger.trace("Error while getting {} schema for {}", polymorphic ? // AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); throw e; } catch (Exception e) { // logger.trace("Error while getting {}schema for {}", polymorphic ? // AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); throw new RuntimeException(e); } } @Override public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { ContextCache contextCache = ContextCache.getInstance(); Context context = ContextCache.getInstance().getContextByUUID(uuid); if (context == null) { context = getContextFromServer(uuid.toString()); contextCache.cleanCache(); contextCache.refreshContextsIfNeeded(); Context c = contextCache.getContextByUUID(context.getHeader().getUUID()); if (c != null) { context = c; } else { logger.error( "Context with UUID {} is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", uuid, context); } } return context; } @Override public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException { String contextFullName = ResourceRegistryPublisherFactory.getCurrentContextFullName(); ContextCache contextCache = ContextCache.getInstance(); UUID uuid = contextCache.getUUIDByFullName(contextFullName); Context context = null; if (uuid == null) { context = getContextFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART); contextCache.cleanCache(); contextCache.refreshContextsIfNeeded(); Context c = contextCache.getContextByUUID(context.getHeader().getUUID()); if (c != null) { context = c; } else { logger.error( "Current Context is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", contextFullName); } } else { context = contextCache.getContextByUUID(uuid); } return context; } private UUID getCurrentContextUUID() throws ResourceRegistryException { return getCurrentContext().getHeader().getUUID(); } @SuppressWarnings("unchecked") @Override public List list(Class clazz, Boolean polymorphic) throws ResourceRegistryException { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(clazz); String ret = list(type, polymorphic); try { return (List) ElementMapper.unmarshalList(ERElement.class, ret); } catch(Exception e) { throw new RuntimeException(e); } } @Override public String list(String type, Boolean polymorphic) throws ResourceRegistryException { try { logger.info("Going to get all instances of {} ", type); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); Map parameters = new HashMap<>(); parameters.put(InstancePath.POLYMORPHIC_QUERY_PARAMETER, polymorphic.toString()); includeAdditionalQueryParameters(gxHTTPStringRequest, parameters); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got instances of {} are {}", type, ret); return ret; } catch(ResourceRegistryException e) { // logger.trace("Error while getting {} instances", type, e); throw e; } catch(Exception e) { // logger.trace("Error while getting {} instances", type, e); throw new RuntimeException(e); } } protected String create(String type, String json, UUID uuid) throws SchemaViolationException, AlreadyPresentException, ResourceRegistryException { try { logger.trace("Going to create {} : {}", type, json); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("{} successfully created", ret); return ret; } catch (ResourceRegistryException e) { // logger.trace("Error Creating {}", facet, e); throw e; } catch (Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); } } protected String internalCreate(ERElem er) throws SchemaViolationException, AlreadyPresentException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); String json = ElementMapper.marshal(er); Header header = er.getHeader(); if (header == null) { UUID randomUUID = UUIDManager.generateValidRandomUUID(); header = new HeaderImpl(randomUUID); er.setHeader(header); } UUID uuid = er.getHeader().getUUID(); return create(type, json, uuid); } catch (ResourceRegistryException e) { // logger.trace("Error Creating {}", facet, e); throw e; } catch (Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") @Override public ERElem create(ERElem er) throws SchemaViolationException, AlreadyPresentException, ResourceRegistryException { try { String ret = internalCreate(er); return (ERElem) ElementMapper.unmarshal(ERElement.class, ret); } 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 json) throws SchemaViolationException, AlreadyPresentException, ResourceRegistryException { try { ERElement e = ElementMapper.unmarshal(ERElement.class, json); return internalCreate(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 boolean exist(ERElem er) throws AvailableInAnotherContextException, ResourceRegistryException { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(er); UUID uuid = er.getHeader().getUUID(); return exist(type, uuid); } @Override public boolean exist(Class clazz, UUID uuid) throws AvailableInAnotherContextException, ResourceRegistryException { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(clazz); return exist(type, uuid); } @Override public boolean exist(String type, UUID uuid) throws AvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to check if {} with UUID {} exists", type, uuid); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(uuid.toString()); includeAdditionalQueryParameters(gxHTTPStringRequest); HttpURLConnection httpURLConnection = gxHTTPStringRequest.head(); HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("{} with UUID {} exists", type, uuid); return true; } catch (NotFoundException e) { return false; } catch (ResourceRegistryException e) { // logger.trace("Error while checking if {} with UUID {} exists.", type, uuid, // e); throw e; } catch (Exception e) { // logger.trace("Error while checking if {} with UUID {} exists.", type, uuid, // e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") @Override public ERElem read(ERElem er) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); UUID uuid = er.getHeader().getUUID(); String ret = read(type, uuid); return (ERElem) ElementMapper.unmarshal(ERElement.class, ret); } catch (ResourceRegistryException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") @Override public ERElem read(Class clazz, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(clazz); String ret = read(type, uuid); return (ERElem) ElementMapper.unmarshal(ERElement.class, ret); } catch (ResourceRegistryException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } @Override public String read(String type, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { logger.trace("Going to read {} with UUID {}", type, uuid); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(uuid.toString()); includeAdditionalQueryParameters(gxHTTPStringRequest); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got {} with UUID {} is {}", type, uuid, ret); return ret; } catch (ResourceRegistryException e) { // logger.trace("Error Creating {}", facet, e); throw e; } catch (Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") @Override public ERElem update(ERElem er) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); String json = ElementMapper.marshal(er); UUID uuid = er.getHeader().getUUID(); String ret = update(type, json, uuid); return (ERElem) ElementMapper.unmarshal(ERElement.class, ret); } 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 update(String json) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getClassFromJsonString(json); return update(type, json); } 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 update(String type, String json) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { UUID uuid = Utility.getUUIDFromJSONString(json); return update(type, json, uuid); } 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 update(String type, String json, UUID uuid) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { logger.trace("Going to create {} : {}", type, json); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.header("Content-type", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(json); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("{} with UUID {} successfully created : {}", type, uuid, ret); return ret; } 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 delete(ERElem er) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); UUID uuid = er.getHeader().getUUID(); return delete(type, uuid); } 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 delete(String type, UUID uuid) throws SchemaViolationException, NotFoundException, ResourceRegistryException { try { logger.trace("Going to delete {} with UUID {}", type, uuid); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete(); HTTPUtility.getResponse(String.class, httpURLConnection); boolean deleted = true; logger.info("{} with UUID {} {}", type, uuid, deleted ? " successfully deleted" : "was NOT deleted"); return deleted; } 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 F createFacet(F facet) throws SchemaViolationException, FacetAlreadyPresentException, ResourceRegistryException { return create(facet); } @Override public String createFacet(String facet) throws SchemaViolationException, FacetAlreadyPresentException, ResourceRegistryException { return create(facet); } @Override public F readFacet(F facet) throws FacetNotFoundException, FacetAvailableInAnotherContextException, ResourceRegistryException { return read(facet); } @Override public String readFacet(String facetType, UUID uuid) throws FacetNotFoundException, FacetAvailableInAnotherContextException, ResourceRegistryException { return read(facetType, uuid); } @Override public F updateFacet(F facet) throws SchemaViolationException, FacetNotFoundException, ResourceRegistryException { return update(facet); } @Override public String updateFacet(String facet) throws SchemaViolationException, FacetNotFoundException, ResourceRegistryException { return update(facet); } @Override public boolean deleteFacet(F facet) throws SchemaViolationException, FacetNotFoundException, ResourceRegistryException { return delete(facet); } @Override public boolean deleteFacet(String facetType, UUID uuid) throws SchemaViolationException, FacetNotFoundException, ResourceRegistryException { return delete(facetType, uuid); } @Override public R createResource(R resource) throws SchemaViolationException, ResourceAlreadyPresentException, ResourceRegistryException { return create(resource); } @Override public String createResource(String resource) throws SchemaViolationException, ResourceAlreadyPresentException, ResourceRegistryException { return create(resource); } @Override public R readResource(R resource) throws ResourceNotFoundException, ResourceAvailableInAnotherContextException, ResourceRegistryException { return read(resource); } @Override public String readResource(String resourceType, UUID uuid) throws ResourceNotFoundException, ResourceAvailableInAnotherContextException, ResourceRegistryException { return read(resourceType, uuid); } @Override public R updateResource(R resource) throws SchemaViolationException, ResourceNotFoundException, ResourceRegistryException { return update(resource); } @Override public String updateResource(String resource) throws SchemaViolationException, ResourceNotFoundException, ResourceRegistryException { return update(resource); } @Override public boolean deleteResource(R resource) throws ResourceNotFoundException, ResourceRegistryException { return delete(resource); } @Override public boolean deleteResource(String resourceType, UUID uuid) throws ResourceNotFoundException, ResourceRegistryException { return delete(resourceType, uuid); } @Override public > C createConsistsOf(C consistsOf) throws SchemaViolationException, ConsistsOfAlreadyPresentException, ResourceNotFoundException, ResourceRegistryException { return create(consistsOf); } @Override public String createConsistsOf(String consistsOf) throws SchemaViolationException, ConsistsOfAlreadyPresentException, ResourceNotFoundException, ResourceRegistryException { return create(consistsOf); } @Override public > C readConsistsOf(C consistsOf) throws ConsistsOfNotFoundException, ConsistsOfAvailableInAnotherContextException, ResourceRegistryException { return read(consistsOf); } @Override public String readConsistsOf(String consistsOfType, UUID uuid) throws ConsistsOfNotFoundException, ConsistsOfAvailableInAnotherContextException, ResourceRegistryException { return read(consistsOfType, uuid); } @Override public > C updateConsistsOf(C consistsOf) throws SchemaViolationException, ConsistsOfNotFoundException, ResourceRegistryException { return update(consistsOf); } @Override public String updateConsistsOf(String consistsOf) throws SchemaViolationException, ConsistsOfNotFoundException, ResourceRegistryException { return update(consistsOf); } @Override public > boolean deleteConsistsOf(C consistsOf) throws SchemaViolationException, ConsistsOfNotFoundException, ResourceRegistryException { return delete(consistsOf); } @Override public boolean deleteConsistsOf(String consistsOfType, UUID uuid) throws SchemaViolationException, ConsistsOfNotFoundException, ResourceRegistryException { return delete(consistsOfType, uuid); } @Override public > I createIsRelatedTo(I isRelatedTo) throws SchemaViolationException, IsRelatedToAlreadyPresentException, ResourceNotFoundException, ResourceRegistryException { return create(isRelatedTo); } @Override public String createIsRelatedTo(String isRelatedTo) throws SchemaViolationException, IsRelatedToAlreadyPresentException, ResourceNotFoundException, ResourceRegistryException { return create(isRelatedTo); } @Override public > I readIsRelatedTo(I isRelatedTo) throws IsRelatedToNotFoundException, IsRelatedToAvailableInAnotherContextException, ResourceRegistryException { return read(isRelatedTo); } @Override public String readIsRelatedTo(String isRelatedToType, UUID uuid) throws IsRelatedToNotFoundException, IsRelatedToAvailableInAnotherContextException, ResourceRegistryException { return read(isRelatedToType, uuid); } @Override public > I updateIsRelatedTo(I isRelatedTo) throws SchemaViolationException, IsRelatedToNotFoundException, ResourceRegistryException { return update(isRelatedTo); } @Override public String updateIsRelatedTo(String isRelatedTo) throws SchemaViolationException, IsRelatedToNotFoundException, ResourceRegistryException { return update(isRelatedTo); } @Override public > boolean deleteIsRelatedTo(I isRelatedTo) throws IsRelatedToNotFoundException, ResourceRegistryException { return delete(isRelatedTo); } @Override public boolean deleteIsRelatedTo(String isRelatedToType, UUID uuid) throws IsRelatedToNotFoundException, ResourceRegistryException { return delete(isRelatedToType, uuid); } @Override public List addToContext(String type, UUID instanceUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { try { logger.trace("Going to add {} with UUID {} to {} with UUID {} ", type, instanceUUID, Context.NAME, contextUUID); 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(type); gxHTTPStringRequest.path(instanceUUID.toString()); Map queryParams = new HashMap<>(); queryParams.put(SharingPath.OPERATION_QUERY_PARAMETER, SharingOperation.ADD.name()); queryParams.put(SharingPath.DRY_RUN_QUERY_QUERY_PARAMETER, dryRun.toString()); Boolean forceAddToContext = getCurrentContextUUID().compareTo(contextUUID)==0; queryParams.put(SharingPath.FORCE_ADD_TO_CONTEXT_QUERY_PARAMETER, forceAddToContext.toString()); includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams); HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(); String jsonArray = HTTPUtility.getResponse(String.class, httpURLConnection); logger.info("{} with UUID {} successfully added to {} with UUID {}", type, instanceUUID, Context.NAME, contextUUID); List affectedInstaces = ElementMapper.unmarshalList(jsonArray); return affectedInstaces; } 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 addToContext(ERElement er, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); UUID instanceUUID = er.getHeader().getUUID(); return addToContext(type, instanceUUID, contextUUID, dryRun); } 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 addToCurrentContext(String type, UUID instanceUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return addToContext(type, instanceUUID, contextUUID, dryRun); } @Override public List addToCurrentContext(ERElement er, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return addToContext(er, contextUUID, dryRun); } @Override public List removeFromContext(String type, UUID instanceUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { try { logger.trace("Going to add {} with UUID {} to {} with UUID {} ", type, instanceUUID, Context.NAME, contextUUID); 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(type); gxHTTPStringRequest.path(instanceUUID.toString()); Map queryParams = new HashMap<>(); queryParams.put(SharingPath.OPERATION_QUERY_PARAMETER, SharingOperation.REMOVE.name()); queryParams.put(SharingPath.DRY_RUN_QUERY_QUERY_PARAMETER, dryRun.toString()); includeAdditionalQueryParameters(gxHTTPStringRequest, queryParams); HttpURLConnection httpURLConnection = gxHTTPStringRequest.post(); String jsonArray = HTTPUtility.getResponse(String.class, httpURLConnection); logger.info("{} with UUID {} successfully removed from {} with UUID {}", type, instanceUUID, Context.NAME, contextUUID); List affectedInstaces = ElementMapper.unmarshalList(jsonArray); return affectedInstaces; } 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 removeFromContext(ERElement er, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { try { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility .getTypeName(er); UUID instanceUUID = er.getHeader().getUUID(); return removeFromContext(type, instanceUUID, contextUUID, dryRun); } 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 removeFromCurrentContext(String type, UUID instanceUUID, Boolean dryRun) throws SchemaViolationException, NotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return removeFromContext(type, instanceUUID, contextUUID, dryRun); } @Override public List removeFromCurrentContext(ERElement er, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return removeFromContext(er, contextUUID, dryRun); } @Override public Map getElementContexts(String type, UUID instanceUUID) throws NotFoundException, ResourceRegistryException { try { logger.trace("Going to get contexts of {} with UUID {}", type, instanceUUID); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName()); gxHTTPStringRequest.path(InstancePath.INSTANCES_PATH_PART); gxHTTPStringRequest.path(type); gxHTTPStringRequest.path(instanceUUID.toString()); gxHTTPStringRequest.path(SharingPath.CONTEXTS_PATH_PART); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String objectNode = HTTPUtility.getResponse(String.class, httpURLConnection); logger.info("Contexts of {} with UUID {} are {}", type, instanceUUID, objectNode); Map contexts = ContextUtility.getContextMap(objectNode); return contexts; } 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 Map getElementContexts(ERElem er) throws NotFoundException, ResourceRegistryException { String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(er); UUID instanceUUID = er.getHeader().getUUID(); return getElementContexts(type, instanceUUID); } @Override public List addResourceToContext(String resourceType, UUID resourceUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(resourceType, resourceUUID, contextUUID, dryRun); } @Override public List addResourceToContext(R resource, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(resource, contextUUID, dryRun); } @Override public List addResourceToCurrentContext(String resourceType, UUID resourceUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(resourceType, resourceUUID, dryRun); } @Override public List addResourceToCurrentContext(R resource, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(resource, dryRun); } @Override public List removeResourceFromContext(String resourceType, UUID resourceUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(resourceType, resourceUUID, contextUUID, dryRun); } @Override public List removeResourceFromContext(R resource, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(resource, contextUUID, dryRun); } @Override public List removeResourceFromCurrentContext(String resourceType, UUID resourceUUID, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(resourceType, resourceUUID, dryRun); } @Override public List removeResourceFromCurrentContext(R resource, Boolean dryRun) throws SchemaViolationException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(resource, dryRun); } @Override public Map getResourceContexts(String resourceType, UUID resourceUUID) throws ResourceNotFoundException, ResourceRegistryException { return getElementContexts(resourceType, resourceUUID); } @Override public Map getResourceContexts(R resource) throws ResourceNotFoundException, ResourceRegistryException { return getElementContexts(resource); } @Override public List addFacetToContext(String facetType, UUID facetUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(facetType, facetUUID, contextUUID, dryRun); } @Override public List addFacetToContext(F facet, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(facet, contextUUID, dryRun); } @Override public List addFacetToCurrentContext(String facetType, UUID facetUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(facetType, facetUUID, dryRun); } @Override public List addFacetToCurrentContext(F facet, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(facet, dryRun); } @Override public List removeFacetFromContext(String facetType, UUID facetUUID, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(facetType, facetUUID, contextUUID, dryRun); } @Override public List removeFacetFromContext(F facet, UUID contextUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(facet, contextUUID, dryRun); } @Override public List removeFacetFromCurrentContext(String facetType, UUID facetUUID, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(facetType, facetUUID, dryRun); } @Override public List removeFacetFromCurrentContext(F facet, Boolean dryRun) throws SchemaViolationException, FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(facet, dryRun); } @Override public Map getFacetContexts(String facetType, UUID facetUUID) throws FacetNotFoundException, ResourceRegistryException { return getElementContexts(facetType, facetUUID); } @Override public Map getFacetContexts(F facet) throws FacetNotFoundException, ResourceRegistryException { return getElementContexts(facet); } }