package org.gcube.informationsystem.resourceregistry.publisher; 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.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.base.impl.properties.HeaderImpl; import org.gcube.informationsystem.base.reference.ER; import org.gcube.informationsystem.base.reference.properties.Header; import org.gcube.informationsystem.context.reference.entities.Context; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; 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.HTTPUtility; import org.gcube.informationsystem.utils.ISMapper; 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; public ResourceRegistryPublisherImpl(String address) { this.address = address; } private static String getCurrentContext() { String token = SecurityTokenProvider.instance.get(); AuthorizationEntry authorizationEntry = null; try { authorizationEntry = Constants.authorizationService().get(token); } catch(Exception e) { return ScopeProvider.instance.get(); } return authorizationEntry.getContext(); } private UUID getCurrentContextUUID() throws ResourceRegistryException { logger.debug("Going to read current {} ({}) definition", Context.NAME, getCurrentContext()); 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(AccessPath.CURRENT_CONTEXT); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); Context context = HTTPUtility.getResponse(Context.class, httpURLConnection); logger.debug("Got Context is {}", ISMapper.marshal(context)); return context.getHeader().getUUID(); } 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); } } protected String create(String erType, String er, UUID uuid) throws AlreadyPresentException, ResourceRegistryException { try { logger.trace("Going to create {} : {}", erType, er); 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(erType); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(er); 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); } } public String internalCreate(E er) throws AlreadyPresentException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); String erString = ISMapper.marshal(er); Header header = er.getHeader(); if(header==null) { header = new HeaderImpl(UUID.randomUUID()); er.setHeader(header); } UUID uuid = er.getHeader().getUUID(); return create(erType, erString, 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 E create(E er) throws AlreadyPresentException, ResourceRegistryException { try { String ret = internalCreate(er); return (E) ISMapper.unmarshal(ER.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 er) throws AlreadyPresentException, ResourceRegistryException { try { ER e = ISMapper.unmarshal(ER.class, er); 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); } } @SuppressWarnings("unchecked") @Override public E read(E er) throws NotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); UUID uuid = er.getHeader().getUUID(); String ret = read(erType, uuid); return (E) ISMapper.unmarshal(ER.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 read(String erType, UUID uuid) throws NotFoundException, ResourceRegistryException { try { logger.trace("Going to read {} with UUID {}", erType, 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(erType); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.debug("Got {} with UUID {} is {}", erType, 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); } } protected String update(String erType, String er, UUID uuid) throws AlreadyPresentException, ResourceRegistryException { try { logger.trace("Going to create {} : {}", erType, er); 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(erType); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(er); String ret = HTTPUtility.getResponse(String.class, httpURLConnection); logger.trace("{} with UUID {} successfully created : {}", erType, 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 E update(E er) throws NotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); String erString = ISMapper.marshal(er); UUID uuid = er.getHeader().getUUID(); String ret = update(erType, erString, uuid); return (E) ISMapper.unmarshal(ER.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 erType, String er) throws NotFoundException, ResourceRegistryException { try { UUID uuid = Utility.getUUIDFromJSONString(er); return update(erType, er, 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 er) throws NotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getClassFromJsonString(er); return update(erType, er); } 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(E er) throws NotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); UUID uuid = er.getHeader().getUUID(); return delete(erType, 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 erType, UUID uuid) throws NotFoundException, ResourceRegistryException { try { logger.trace("Going to delete {} with UUID {}", erType, 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(erType); gxHTTPStringRequest.path(uuid.toString()); HttpURLConnection httpURLConnection = gxHTTPStringRequest.delete(); HTTPUtility.getResponse(String.class, httpURLConnection); boolean deleted = true; logger.info("{} with UUID {} {}", erType, 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 FacetAlreadyPresentException, ResourceRegistryException { return create(facet); } @Override public String createFacet(String facet) throws FacetAlreadyPresentException, ResourceRegistryException { return create(facet); } @Override public F readFacet(F facet) throws FacetNotFoundException, ResourceRegistryException { return read(facet); } @Override public String readFacet(String facetType, UUID uuid) throws FacetNotFoundException, ResourceRegistryException { return read(facetType, uuid); } @Override public F updateFacet(F facet) throws FacetNotFoundException, ResourceRegistryException { return update(facet); } @Override public String updateFacet(String facet) throws FacetNotFoundException, ResourceRegistryException { return update(facet); } @Override public boolean deleteFacet(F facet) throws FacetNotFoundException, ResourceRegistryException { return delete(facet); } @Override public boolean deleteFacet(String facetType, UUID uuid) throws FacetNotFoundException, ResourceRegistryException { return delete(facetType, uuid); } @Override public R createResource(R resource) throws ResourceAlreadyPresentException, ResourceRegistryException { return create(resource); } @Override public String createResource(String resource) throws ResourceAlreadyPresentException, ResourceRegistryException { return create(resource); } @Override public R readResource(R resource) throws ResourceNotFoundException, ResourceRegistryException { return read(resource); } @Override public String readResource(String resourceType, UUID uuid) throws ResourceNotFoundException, ResourceRegistryException { return read(resourceType, uuid); } @Override public R updateResource(R resource) throws ResourceNotFoundException, ResourceRegistryException { return update(resource); } @Override public String updateResource(String resource) throws 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 NotFoundException, ResourceRegistryException { return create(consistsOf); } @Override public String createConsistsOf(String consistsOf) throws NotFoundException, ResourceRegistryException { return create(consistsOf); } @Override public > C readConsistsOf(C consistsOf) throws NotFoundException, ResourceRegistryException { return read(consistsOf); } @Override public String readConsistsOf(String consistsOfType, UUID uuid) throws NotFoundException, ResourceRegistryException { return read(consistsOfType, uuid); } @Override public > C updateConsistsOf(C consistsOf) throws NotFoundException, ResourceRegistryException { return update(consistsOf); } @Override public String updateConsistsOf(String consistsOf) throws NotFoundException, ResourceRegistryException { return update(consistsOf); } @Override public > boolean deleteConsistsOf(C consistsOf) throws ResourceRegistryException { return delete(consistsOf); } @Override public boolean deleteConsistsOf(String consistsOfType, UUID uuid) throws ResourceRegistryException { return delete(consistsOfType, uuid); } @Override public > I createIsRelatedTo(I isRelatedTo) throws ResourceNotFoundException, ResourceRegistryException { return create(isRelatedTo); } @Override public String createIsRelatedTo(String isRelatedTo) throws ResourceNotFoundException, ResourceRegistryException { return create(isRelatedTo); } @Override public > I readIsRelatedTo(I isRelatedTo) throws NotFoundException, ResourceRegistryException { return read(isRelatedTo); } @Override public String readIsRelatedTo(String isRelatedToType, UUID uuid) throws NotFoundException, ResourceRegistryException { return read(isRelatedToType, uuid); } @Override public > I updateIsRelatedTo(I isRelatedTo) throws NotFoundException, ResourceRegistryException { return update(isRelatedTo); } @Override public String updateIsRelatedTo(String isRelatedTo) throws NotFoundException, ResourceRegistryException { return update(isRelatedTo); } @Override public > boolean deleteIsRelatedTo(I isRelatedTo) throws ResourceRegistryException { return delete(isRelatedTo); } @Override public boolean deleteIsRelatedTo(String isRelatedToType, UUID uuid) throws ResourceRegistryException { return delete(isRelatedToType, uuid); } @Override public boolean addToContext(UUID contextUUID, String erType, UUID instanceUUID) throws NotFoundException, ResourceRegistryException { try { logger.trace("Going to add {} with UUID {} to {} with UUID {} ", erType, 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(erType); gxHTTPStringRequest.path(instanceUUID.toString()); 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); return added; } 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 addToContext(UUID contextUUID, E er) throws NotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); UUID instanceUUID = er.getHeader().getUUID(); return addToContext(contextUUID, erType, instanceUUID); } 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 addToCurrentContext(String erType, UUID instanceUUID) throws NotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return addToContext(contextUUID, erType, instanceUUID); } @Override public boolean addToCurrentContext(E er) throws NotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return addToContext(contextUUID, er); } @Override public boolean removeFromContext(UUID contextUUID, String erType, UUID instanceUUID) throws NotFoundException, ResourceRegistryException { try { logger.trace("Going to add {} with UUID {} to {} with UUID {} ", erType, 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(erType); gxHTTPStringRequest.path(instanceUUID.toString()); 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); return removed; } 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 removeFromContext(UUID contextUUID, E er) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { try { String erType = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getType(er); UUID instanceUUID = er.getHeader().getUUID(); return removeFromContext(contextUUID, erType, instanceUUID); } 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 removeFromCurrentContext(String erType, UUID instanceUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return removeFromContext(contextUUID, erType, instanceUUID); } @Override public boolean removeFromCurrentContext(E er) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID contextUUID = getCurrentContextUUID(); return removeFromContext(contextUUID, er); } @Override public boolean addResourceToContext(UUID contextUUID, String resourceType, UUID resourceUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(contextUUID, resourceType, resourceUUID); } @Override public boolean addResourceToContext(UUID contextUUID, R resource) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(contextUUID, resource); } @Override public boolean addResourceToCurrentContext(String resourceType, UUID resourceUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(resourceType, resourceUUID); } @Override public boolean addResourceToCurrentContext(R resource) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(resource); } @Override public boolean removeResourceFromContext(UUID contextUUID, String resourceType, UUID resourceUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(contextUUID, resourceType, resourceUUID); } @Override public boolean removeResourceFromContext(UUID contextUUID, R resource) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(contextUUID, resource); } @Override public boolean removeResourceFromCurrentContext(String resourceType, UUID resourceUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(resourceType, resourceUUID); } @Override public boolean removeResourceFromCurrentContext(R resource) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(resource); } @Override public boolean addFacetToContext(UUID contextUUID, String facetType, UUID facetUUID) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(contextUUID, facetType, facetUUID); } @Override public boolean addFacetToContext(UUID contextUUID, F facet) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToContext(contextUUID, facet); } @Override public boolean addFacetToCurrentContext(String facetType, UUID facetUUID) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(facetType, facetUUID); } @Override public boolean addFacetToCurrentContext(F facet) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addToCurrentContext(facet); } @Override public boolean removeFacetFromContext(UUID contextUUID, String facetType, UUID facetUUID) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(contextUUID, facetType, facetUUID); } @Override public boolean removeFacetFromContext(UUID contextUUID, F facet) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromContext(contextUUID, facet); } @Override public boolean removeFacetFromCurrentContext(String facetType, UUID facetUUID) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(facetType, facetUUID); } @Override public boolean removeFacetFromCurrentContext(F facet) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return removeFromCurrentContext(facet); } }