2016-12-22 17:27:26 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.rest;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2021-02-19 14:34:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-10-27 17:46:09 +02:00
|
|
|
import javax.ws.rs.Consumes;
|
2017-11-07 18:22:29 +01:00
|
|
|
import javax.ws.rs.DefaultValue;
|
|
|
|
import javax.ws.rs.GET;
|
2016-07-18 11:57:12 +02:00
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
import javax.ws.rs.Path;
|
2017-01-10 14:58:13 +01:00
|
|
|
import javax.ws.rs.PathParam;
|
2016-10-27 17:46:09 +02:00
|
|
|
import javax.ws.rs.Produces;
|
2017-11-07 18:22:29 +01:00
|
|
|
import javax.ws.rs.QueryParam;
|
2016-10-27 17:46:09 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2017-04-11 19:38:46 +02:00
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response.Status;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2017-03-24 17:04:09 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
|
2017-01-10 14:58:13 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2021-10-25 11:00:54 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
|
2018-06-06 19:26:19 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
|
2021-09-02 15:06:40 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
2021-10-22 10:24:16 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.types.TypeManagement;
|
2021-02-19 14:34:48 +01:00
|
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
2022-07-20 17:44:59 +02:00
|
|
|
import org.gcube.smartgears.utils.InnerMethodName;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2016-10-12 14:33:23 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2017-01-10 14:58:13 +01:00
|
|
|
*/
|
2018-06-06 19:26:19 +02:00
|
|
|
@Path(TypePath.TYPES_PATH_PART)
|
2021-06-25 12:25:06 +02:00
|
|
|
public class TypeManager {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-06-25 12:25:06 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(TypeManager.class);
|
2018-06-04 12:31:09 +02:00
|
|
|
|
2022-07-20 12:10:03 +02:00
|
|
|
public static final String TYPE_PATH_PARAMETER = "TYPE_NAME";
|
|
|
|
|
2021-09-02 15:06:40 +02:00
|
|
|
public TypeManager() {
|
|
|
|
ContextUtility.getHierarchicalMode().set(false);
|
|
|
|
ContextUtility.getIncludeInstanceContexts().set(false);
|
|
|
|
}
|
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* PUT /types/{TYPE_NAME}
|
|
|
|
* e.g. PUT /types/ContactFacet
|
2016-11-21 10:07:28 +01:00
|
|
|
*
|
|
|
|
* BODY: {...}
|
|
|
|
*
|
2016-09-01 18:36:41 +02:00
|
|
|
*/
|
2016-07-18 11:57:12 +02:00
|
|
|
@PUT
|
2022-07-20 12:10:03 +02:00
|
|
|
@Path("{" + TypeManager.TYPE_PATH_PARAMETER + "}")
|
2017-11-30 18:06:08 +01:00
|
|
|
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
2017-03-24 17:04:09 +01:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2022-07-20 12:10:03 +02:00
|
|
|
public Response create(@PathParam(TypeManager.TYPE_PATH_PARAMETER) String typeName, String json)
|
2017-01-10 14:58:13 +01:00
|
|
|
throws SchemaException, ResourceRegistryException {
|
2021-02-11 14:35:54 +01:00
|
|
|
logger.info("Requested {} creation with schema {}", typeName, json);
|
2022-07-20 17:44:59 +02:00
|
|
|
InnerMethodName.instance.set("createType");
|
2018-06-12 19:42:45 +02:00
|
|
|
|
2021-10-22 10:24:16 +02:00
|
|
|
TypeManagement schemaManagement = new TypeManagement();
|
2021-02-11 14:35:54 +01:00
|
|
|
schemaManagement.setTypeName(typeName);
|
|
|
|
schemaManagement.setJson(json);
|
|
|
|
String ret = schemaManagement.create();
|
2017-11-07 18:22:29 +01:00
|
|
|
return Response.status(Status.CREATED).entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
.build();
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-07 18:12:06 +02:00
|
|
|
/*
|
|
|
|
* GET /types/{TYPE_NAME}
|
|
|
|
* e.g. GET /types/ContactFacet?polymorphic=false
|
2018-06-06 19:26:19 +02:00
|
|
|
*
|
2017-11-07 18:22:29 +01:00
|
|
|
*/
|
|
|
|
@GET
|
2022-07-20 12:10:03 +02:00
|
|
|
@Path("{" + TypeManager.TYPE_PATH_PARAMETER + "}")
|
2017-11-07 18:22:29 +01:00
|
|
|
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
|
2022-07-20 12:10:03 +02:00
|
|
|
public String read(@PathParam(TypeManager.TYPE_PATH_PARAMETER) String type,
|
|
|
|
@QueryParam(TypePath.POLYMORPHIC_QUERY_PARAMETER) @DefaultValue("false") Boolean polymorphic)
|
2017-11-07 18:22:29 +01:00
|
|
|
throws SchemaNotFoundException, ResourceRegistryException {
|
2018-06-07 18:12:06 +02:00
|
|
|
logger.info("Requested Schema for type {}", type);
|
2022-07-20 17:44:59 +02:00
|
|
|
InnerMethodName.instance.set("readType");
|
2021-03-02 09:13:53 +01:00
|
|
|
|
2021-10-22 10:24:16 +02:00
|
|
|
TypeManagement schemaManagement = new TypeManagement();
|
2021-01-13 21:52:09 +01:00
|
|
|
schemaManagement.setTypeName(type);
|
2021-02-19 14:34:48 +01:00
|
|
|
List<Type> types = schemaManagement.read(polymorphic);
|
|
|
|
try {
|
|
|
|
return TypeMapper.serializeTypeDefinitions(types);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|