Exposing AddToContext APi refs #5701

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134120 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-14 15:33:27 +00:00
parent b40503fa33
commit afc66c822e
2 changed files with 69 additions and 45 deletions

View File

@ -12,6 +12,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.EntityManagement; import org.gcube.informationsystem.resourceregistry.api.EntityManagement;
@ -22,6 +24,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetN
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath; import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath;
import org.gcube.informationsystem.resourceregistry.resources.impl.EntityManagementImpl; import org.gcube.informationsystem.resourceregistry.resources.impl.EntityManagementImpl;
import org.gcube.informationsystem.resourceregistry.resources.utils.ContextUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -40,13 +43,12 @@ public class EntityManager {
public static final String SOURCE_ID_PATH_PARAM = "sourceId"; public static final String SOURCE_ID_PATH_PARAM = "sourceId";
public static final String TARGET_ID_PATH_PARAM = "targetId"; public static final String TARGET_ID_PATH_PARAM = "targetId";
protected EntityManagement entityManager = new EntityManagementImpl(); protected EntityManagement entityManager = new EntityManagementImpl();
/* Facets Methods */ /* Facets Methods */
/** /**
* e.g. PUT /resource-registry/entity/facet/ContactFacet?definition={...} * e.g. PUT /resource-registry/entity/facet/ContactFacet?definition={...}
*
* @param type * @param type
* @param definition * @param definition
* @return * @return
@ -55,10 +57,9 @@ public class EntityManager {
*/ */
@PUT @PUT
@Path(EntityPath.FACET_PATH_PART + "/{" + TYPE_PATH_PARAM + "}") @Path(EntityPath.FACET_PATH_PART + "/{" + TYPE_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String createFacet( public String createFacet(@PathParam(TYPE_PATH_PARAM) String type,
@PathParam(TYPE_PATH_PARAM) String type,
@QueryParam(EntityPath.DEFINITION_PARAM) String definition) @QueryParam(EntityPath.DEFINITION_PARAM) String definition)
throws EntityException, ResourceRegistryException { throws EntityException, ResourceRegistryException {
logger.trace("requested facet creation for type {} defined by {} ", logger.trace("requested facet creation for type {} defined by {} ",
@ -67,7 +68,9 @@ public class EntityManager {
} }
/** /**
* e.g. POST /resource-registry/entity/facet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86?definition={...} * e.g. POST /resource-registry/entity/facet/4023d5b2-8601-47a5-
* 83ef-49ffcbfc7d86?definition={...}
*
* @param uuid * @param uuid
* @param definition * @param definition
* @return * @return
@ -76,10 +79,9 @@ public class EntityManager {
*/ */
@POST @POST
@Path(EntityPath.FACET_PATH_PART + "/{" + ID_PATH_PARAM + "}") @Path(EntityPath.FACET_PATH_PART + "/{" + ID_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String updateFacet( public String updateFacet(@PathParam(ID_PATH_PARAM) String uuid,
@PathParam(ID_PATH_PARAM) String uuid,
@QueryParam(EntityPath.DEFINITION_PARAM) String definition) @QueryParam(EntityPath.DEFINITION_PARAM) String definition)
throws FacetNotFoundException, ResourceRegistryException { throws FacetNotFoundException, ResourceRegistryException {
logger.trace("requested facet update for id {} with {}", uuid, logger.trace("requested facet update for id {} with {}", uuid,
@ -88,7 +90,9 @@ public class EntityManager {
} }
/** /**
* e.g. DELETE /resource-registry/entity/facet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86 * e.g. DELETE
* /resource-registry/entity/facet/4023d5b2-8601-47a5-83ef-49ffcbfc7d86
*
* @param uuid * @param uuid
* @return * @return
* @throws FacetNotFoundException * @throws FacetNotFoundException
@ -106,6 +110,7 @@ public class EntityManager {
/** /**
* e.g. PUT /resource-registry/entity/resource/HostingNode?definition={...} * e.g. PUT /resource-registry/entity/resource/HostingNode?definition={...}
*
* @param type * @param type
* @param definition * @param definition
* @return * @return
@ -114,10 +119,9 @@ public class EntityManager {
*/ */
@PUT @PUT
@Path(EntityPath.RESOURCE_PATH_PART + "/{" + TYPE_PATH_PARAM + "}") @Path(EntityPath.RESOURCE_PATH_PART + "/{" + TYPE_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String createResource( public String createResource(@PathParam(TYPE_PATH_PARAM) String type,
@PathParam(TYPE_PATH_PARAM) String type,
@QueryParam(EntityPath.DEFINITION_PARAM) String definition) @QueryParam(EntityPath.DEFINITION_PARAM) String definition)
throws FacetNotFoundException, ResourceRegistryException { throws FacetNotFoundException, ResourceRegistryException {
logger.trace("requested resource creation for type {} with json {}", logger.trace("requested resource creation for type {} with json {}",
@ -126,7 +130,9 @@ public class EntityManager {
} }
/** /**
* e.g. DELETE /resource-registry/entity/resource/67062c11-9c3a-4906-870d-7df6a43408b0 * e.g. DELETE
* /resource-registry/entity/resource/67062c11-9c3a-4906-870d-7df6a43408b0
*
* @param uuid * @param uuid
* @return * @return
* @throws ResourceNotFoundException * @throws ResourceNotFoundException
@ -143,7 +149,11 @@ public class EntityManager {
/* Relations Methods */ /* Relations Methods */
/** /**
* e.g. PUT /resource-registry/entity/consistOf/source/bbf80a93-2284-424a-930c-7ee20021aee1/target/f6931232-c034-4979-9b2f-7193d3fba7df?type=hasCreator&properties={...} * e.g. PUT
* /resource-registry/entity/consistOf/source/bbf80a93-2284-424a-930c-
* 7ee20021aee1/target/f6931232-c034-4979-9b2f-7193d3fba7df?type=hasCreator&properties={..
* . }
*
* @param resourceUUID * @param resourceUUID
* @param facetUUID * @param facetUUID
* @param type * @param type
@ -154,11 +164,9 @@ public class EntityManager {
* @throws ResourceRegistryException * @throws ResourceRegistryException
*/ */
@PUT @PUT
@Path(EntityPath.CONSISTS_OF_PATH_PART + "/" @Path(EntityPath.CONSISTS_OF_PATH_PART + "/" + EntityPath.SOURCE_PATH_PART
+ EntityPath.SOURCE_PATH_PART + "/{" + "/{" + SOURCE_ID_PATH_PARAM + "}/" + EntityPath.TARGET_PATH_PART
+ SOURCE_ID_PATH_PARAM + "}/" + "/{" + TARGET_ID_PATH_PARAM + "}")
+ EntityPath.TARGET_PATH_PART + "/{"
+ TARGET_ID_PATH_PARAM + "}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String attachFacet( public String attachFacet(
@PathParam(SOURCE_ID_PATH_PARAM) String resourceUUID, @PathParam(SOURCE_ID_PATH_PARAM) String resourceUUID,
@ -171,12 +179,14 @@ public class EntityManager {
"requested to attach resource {} to facet {} ({} Type {}) with properties {}", "requested to attach resource {} to facet {} ({} Type {}) with properties {}",
resourceUUID, facetUUID, ConsistsOf.class.getSimpleName(), resourceUUID, facetUUID, ConsistsOf.class.getSimpleName(),
type, properties); type, properties);
return entityManager.attachFacet(UUID.fromString(resourceUUID), UUID.fromString(facetUUID), type, return entityManager.attachFacet(UUID.fromString(resourceUUID),
properties); UUID.fromString(facetUUID), type, properties);
} }
/** /**
* e.g. DELETE /resource-registry/entity/consistOf/9bff49c8-c0a7-45de-827c-accb71defbd3 * e.g. DELETE /resource-registry/entity/consistOf/9bff49c8-c0a7-45de-827c-
* accb71defbd3
*
* @param consistOfUUID * @param consistOfUUID
* @return * @return
* @throws ResourceRegistryException * @throws ResourceRegistryException
@ -190,7 +200,12 @@ public class EntityManager {
} }
/** /**
* e.g. PUT /resource-registry/entity/relatedTo/source/4a81008a-6300-4a32-857f-cebe3f7b2925/target/985f7cf9-b6fa-463b-86c8-84ab0a77deea?type=callsFor&properties={...} * e.g. PUT
* /resource-registry/entity/relatedTo/source/4a81008a-6300-4a32-857f
* -cebe3f7b2925
* /target/985f7cf9-b6fa-463b-86c8-84ab0a77deea?type=callsFor&properties
* ={...}
*
* @param sourceResourceUUID * @param sourceResourceUUID
* @param targetResourceUUID * @param targetResourceUUID
* @param type * @param type
@ -201,11 +216,9 @@ public class EntityManager {
*/ */
@PUT @PUT
@Path(EntityPath.IS_RELATED_TO_PATH_PART + "/" @Path(EntityPath.IS_RELATED_TO_PATH_PART + "/"
+ EntityPath.SOURCE_PATH_PART + "/{" + EntityPath.SOURCE_PATH_PART + "/{" + SOURCE_ID_PATH_PARAM + "}/"
+ SOURCE_ID_PATH_PARAM + "}/" + EntityPath.TARGET_PATH_PART + "/{" + TARGET_ID_PATH_PARAM + "}")
+ EntityPath.TARGET_PATH_PART + "/{" @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
+ TARGET_ID_PATH_PARAM + "}")
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String attachResource( public String attachResource(
@PathParam(SOURCE_ID_PATH_PARAM) String sourceResourceUUID, @PathParam(SOURCE_ID_PATH_PARAM) String sourceResourceUUID,
@ -214,15 +227,19 @@ public class EntityManager {
@QueryParam(EntityPath.PROPERTIES_PARAM) String properties) @QueryParam(EntityPath.PROPERTIES_PARAM) String properties)
throws ResourceNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ResourceRegistryException {
logger.trace( logger.trace(
"requested to attach source resource {} and target resource {} ({} Type {}) with properties {}", "requested to attach source {} {} and target {} {} ({} Type {}) with properties {}",
sourceResourceUUID, targetResourceUUID, Resource.NAME, sourceResourceUUID, Resource.NAME,
IsRelatedTo.class.getSimpleName(), type, properties); targetResourceUUID, IsRelatedTo.class.getSimpleName(), type,
return entityManager.attachResource(UUID.fromString(sourceResourceUUID), properties);
return entityManager.attachResource(
UUID.fromString(sourceResourceUUID),
UUID.fromString(targetResourceUUID), type, properties); UUID.fromString(targetResourceUUID), type, properties);
} }
/** /**
* e.g. DELETE /resource-registry/entity/relatedTo/b3982715-a7aa-4530-9a5f-2f60008d256e * e.g. DELETE
* /resource-registry/entity/relatedTo/b3982715-a7aa-4530-9a5f-2f60008d256e
*
* @param relatedToUUID * @param relatedToUUID
* @return * @return
* @throws ResourceRegistryException * @throws ResourceRegistryException
@ -235,19 +252,26 @@ public class EntityManager {
return entityManager.detachResource(UUID.fromString(relatedToUUID)); return entityManager.detachResource(UUID.fromString(relatedToUUID));
} }
@POST @POST
@Path(EntityPath.ADD_PATH_PART + "/" + EntityPath.RESOURCE_PATH_PART + "/{" + ID_PATH_PARAM + "}") @Path(EntityPath.ADD_PATH_PART + "/" + EntityPath.RESOURCE_PATH_PART + "/{"
public boolean addResourceToContext(String uuid) throws ResourceNotFoundException, + ID_PATH_PARAM + "}")
ContextNotFoundException, ResourceRegistryException { public boolean addResourceToContext(@PathParam(ID_PATH_PARAM) String uuid)
throws ResourceNotFoundException, ContextNotFoundException,
ResourceRegistryException {
logger.trace("requested to add {} with UUID {} to current context {}",
Resource.NAME, uuid, ContextUtility.getCurrentContext());
return entityManager.addResourceToContext(UUID.fromString(uuid)); return entityManager.addResourceToContext(UUID.fromString(uuid));
} }
@POST @POST
@Path(EntityPath.ADD_PATH_PART + "/" + EntityPath.FACET_PATH_PART + "/{" + ID_PATH_PARAM + "}") @Path(EntityPath.ADD_PATH_PART + "/" + EntityPath.FACET_PATH_PART + "/{"
public boolean addFacetToContext(String uuid) throws FacetNotFoundException, + ID_PATH_PARAM + "}")
ContextNotFoundException, ResourceRegistryException { public boolean addFacetToContext(@PathParam(ID_PATH_PARAM) String uuid)
throws FacetNotFoundException, ContextNotFoundException,
ResourceRegistryException {
logger.trace("requested to add {} with UUID {} to current context {}",
Facet.NAME, uuid, ContextUtility.getCurrentContext());
return entityManager.addFacetToContext(UUID.fromString(uuid)); return entityManager.addFacetToContext(UUID.fromString(uuid));
} }
} }

View File

@ -76,7 +76,7 @@ public class ContextUtility {
return UUID.fromString(contextID); return UUID.fromString(contextID);
} }
private static String getCurrentScope(){ public static String getCurrentContext(){
String token = SecurityTokenProvider.instance.get(); String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = null; AuthorizationEntry authorizationEntry = null;
try { try {
@ -92,7 +92,7 @@ public class ContextUtility {
OrientGraph orientGraph = null; OrientGraph orientGraph = null;
try { try {
String scope = getCurrentScope(); String scope = getCurrentContext();
if(scope==null){ if(scope==null){
throw new ContextException("Null Token and Scope. Please set your token first."); throw new ContextException("Null Token and Scope. Please set your token first.");
} }