Using gcat-api refs #13216

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/gcat@176866 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-29 16:46:22 +00:00
parent 7b214c54e7
commit b6891d4d89
15 changed files with 207 additions and 104 deletions

36
pom.xml
View File

@ -38,7 +38,7 @@
</dependencyManagement> </dependencyManagement>
<properties> <properties>
<version.jersey>2.13</version.jersey> <version.jersey>2.24.1</version.jersey>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<webappDirectory>${project.basedir}/src/main/webapp/WEB-INF</webappDirectory> <webappDirectory>${project.basedir}/src/main/webapp/WEB-INF</webappDirectory>
<!-- webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory --> <!-- webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory -->
@ -54,6 +54,12 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.gcube.data-publishing</groupId>
<artifactId>gcat-api</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<!-- ehCAChe --> <!-- ehCAChe -->
<dependency> <dependency>
<groupId>javax.cache</groupId> <groupId>javax.cache</groupId>
@ -99,13 +105,9 @@
<artifactId>jersey-container-servlet</artifactId> <artifactId>jersey-container-servlet</artifactId>
<version>${version.jersey}</version> <version>${version.jersey}</version>
</dependency> </dependency>
<!-- Required with jersey 2.27 <!-- Required with jersey 2.27 <dependency> <groupId>org.glassfish.jersey.inject</groupId>
<dependency> <artifactId>jersey-hk2</artifactId> <version>${version.jersey}</version>
<groupId>org.glassfish.jersey.inject</groupId> </dependency> -->
<artifactId>jersey-hk2</artifactId>
<version>${version.jersey}</version>
</dependency>
-->
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
@ -137,18 +139,18 @@
<!-- Libraries explicitly forced to provided --> <!-- Libraries explicitly forced to provided -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.resources</groupId> <groupId>org.gcube.resources</groupId>
@ -175,7 +177,7 @@
<artifactId>discovery-client</artifactId> <artifactId>discovery-client</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId> <artifactId>common-encryption</artifactId>
<scope>provided</scope> <scope>provided</scope>
@ -225,8 +227,8 @@
<dependency> <dependency>
<groupId>org.gcube.data-publishing</groupId> <groupId>org.gcube.data-publishing</groupId>
<artifactId>storagehub-application-persistence</artifactId> <artifactId>storagehub-application-persistence</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version> <version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>

View File

@ -121,7 +121,7 @@ public class CKANResource extends CKAN {
public JsonNode getPreviousRepresentation() { public JsonNode getPreviousRepresentation() {
if(previousRepresentation==null && resourceID!=null) { if(previousRepresentation==null && resourceID!=null) {
super.read(); sendGetRequest(READ, getMapWithID(resourceID));
validate(result); validate(result);
previousRepresentation = result; previousRepresentation = result;
} }
@ -386,7 +386,7 @@ public class CKANResource extends CKAN {
public void delete() { public void delete() {
try { try {
deleteFile(); deleteFile();
super.delete(); sendPostRequest(DELETE, createJsonNodeWithID(resourceID));
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {

View File

@ -1,7 +1,6 @@
package org.gcube.gcat.rest; package org.gcube.gcat.rest;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.ResponseBuilder;
@ -16,24 +15,8 @@ public class BaseREST {
@Context @Context
private UriInfo uriInfo; private UriInfo uriInfo;
public static final String GROUPS = "groups";
public static final String ITEMS = "items";
public static final String LICENSES = "licenses";
public static final String NAMESPACES = "namespaces";
public static final String ORGANIZATIONS = "organizations";
public static final String PROFILES = "profiles";
public static final String RESOURCES = "resources";
public static final String USERS = "users";
public static final String PURGE_QUERY_PARAMETER = "purge";
public static final String APPLICATION_JSON_CHARSET_UTF_8 = MediaType.APPLICATION_JSON + ";charset=UTF-8";
protected static final String LOCATION_HEADER = "Location"; protected static final String LOCATION_HEADER = "Location";
public static final String LIMIT_PARAMETER = "limit";
public static final String OFFSET_PARAMETER = "offset";
protected void setCalledMethod(String method) { protected void setCalledMethod(String method) {
CalledMethodProvider.instance.set(method); CalledMethodProvider.instance.set(method);
logger.info("{}", uriInfo.getAbsolutePath()); logger.info("{}", uriInfo.getAbsolutePath());

View File

@ -15,25 +15,26 @@ import javax.ws.rs.core.Response;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PATCH;
import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.persistence.ckan.CKANGroup; import org.gcube.gcat.persistence.ckan.CKANGroup;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.GROUPS) @Path(Group.GROUPS)
public class Group extends REST<CKANGroup> { public class Group extends REST<CKANGroup> implements org.gcube.gcat.api.interfaces.Group<Response,Response>{
protected static final String GROUP_ID_PARAMETER = "GROUP_ID"; protected static final String GROUP_ID_PARAMETER = "GROUP_ID";
public Group() { public Group() {
super(BaseREST.GROUPS, GROUP_ID_PARAMETER, CKANGroup.class); super(GROUPS, GROUP_ID_PARAMETER, CKANGroup.class);
} }
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Override @Override
public String list(@QueryParam(BaseREST.LIMIT_PARAMETER) @DefaultValue("10") int limit, public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(BaseREST.OFFSET_PARAMETER) @DefaultValue("0") int offset) { @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) {
return super.list(limit, offset); return super.list(limit, offset);
} }
@ -75,7 +76,7 @@ public class Group extends REST<CKANGroup> {
@Path("/{" + GROUP_ID_PARAMETER + "}") @Path("/{" + GROUP_ID_PARAMETER + "}")
@Override @Override
public Response delete(@PathParam(GROUP_ID_PARAMETER) String id, public Response delete(@PathParam(GROUP_ID_PARAMETER) String id,
@QueryParam(BaseREST.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) { @QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) {
return super.delete(id, purge); return super.delete(id, purge);
} }
@ -86,4 +87,9 @@ public class Group extends REST<CKANGroup> {
return delete(id, true); return delete(id, true);
} }
@Override
public Response delete(String name, boolean purge) {
return delete(name, new Boolean(purge));
}
} }

View File

@ -11,28 +11,30 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.persistence.ckan.CKANPackage; import org.gcube.gcat.persistence.ckan.CKANPackage;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.ITEMS) @Path(Item.ITEMS)
public class Item extends REST<CKANPackage> { public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interfaces.Item<Response,Response>{
public static final String ITEM_ID_PARAMETER = "ITEM_ID"; public static final String ITEM_ID_PARAMETER = "ITEM_ID";
public Item() { public Item() {
super(BaseREST.ITEMS, ITEM_ID_PARAMETER, CKANPackage.class); super(ITEMS, ITEM_ID_PARAMETER, CKANPackage.class);
} }
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Override @Override
public String list(@QueryParam(BaseREST.LIMIT_PARAMETER) @DefaultValue("10") int limit, public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(BaseREST.OFFSET_PARAMETER) @DefaultValue("0") int offset) { @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) {
return super.list(limit, offset); return super.list(limit, offset);
} }
@ -76,7 +78,7 @@ public class Item extends REST<CKANPackage> {
@Path("/{" + ITEM_ID_PARAMETER + "}") @Path("/{" + ITEM_ID_PARAMETER + "}")
@Override @Override
public Response delete(@PathParam(ITEM_ID_PARAMETER) String id, public Response delete(@PathParam(ITEM_ID_PARAMETER) String id,
@QueryParam(BaseREST.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) { @QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) {
return super.delete(id, purge); return super.delete(id, purge);
} }
@ -87,4 +89,9 @@ public class Item extends REST<CKANPackage> {
return super.purge(id); return super.purge(id);
} }
@Override
public Response delete(String name, boolean purge) throws WebServiceException {
return delete(name, new Boolean(purge));
}
} }

View File

@ -10,11 +10,11 @@ import org.gcube.gcat.persistence.ckan.CKANLicense;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.LICENSES) @Path(License.LICENSES)
public class License extends REST<CKANLicense> { public class License extends REST<CKANLicense> implements org.gcube.gcat.api.interfaces.License {
public License() { public License() {
super(BaseREST.LICENSES, null, CKANLicense.class); super(LICENSES, null, CKANLicense.class);
} }
@GET @GET

View File

@ -18,13 +18,13 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.NAMESPACES) @Path(Namespace.NAMESPACES)
public class Namespace extends BaseREST { public class Namespace extends BaseREST implements org.gcube.gcat.api.interfaces.Namespace {
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String list() { public String list() {
setCalledMethod("GET /" + BaseREST.NAMESPACES); setCalledMethod("GET /" + NAMESPACES);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ArrayNode arrayNode = mapper.createArrayNode(); ArrayNode arrayNode = mapper.createArrayNode();

View File

@ -15,25 +15,27 @@ import javax.ws.rs.core.Response;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PATCH;
import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.persistence.ckan.CKANOrganization; import org.gcube.gcat.persistence.ckan.CKANOrganization;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.ORGANIZATIONS) @Path(Organization.ORGANIZATIONS)
public class Organization extends REST<CKANOrganization> { public class Organization extends REST<CKANOrganization>
implements org.gcube.gcat.api.interfaces.Organization<Response,Response> {
public static final String ORGANIZATION_ID_PARAMETER = "ORGANIZATION_ID"; public static final String ORGANIZATION_ID_PARAMETER = "ORGANIZATION_ID";
public Organization() { public Organization() {
super(BaseREST.ORGANIZATIONS, ORGANIZATION_ID_PARAMETER, CKANOrganization.class); super(ORGANIZATIONS, ORGANIZATION_ID_PARAMETER, CKANOrganization.class);
} }
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Override @Override
public String list(@QueryParam(BaseREST.LIMIT_PARAMETER) @DefaultValue("10") int limit, public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(BaseREST.OFFSET_PARAMETER) @DefaultValue("0") int offset) { @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) {
return super.list(limit, offset); return super.list(limit, offset);
} }
@ -75,7 +77,7 @@ public class Organization extends REST<CKANOrganization> {
@Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @Path("/{" + ORGANIZATION_ID_PARAMETER + "}")
@Override @Override
public Response delete(@PathParam(ORGANIZATION_ID_PARAMETER) String id, public Response delete(@PathParam(ORGANIZATION_ID_PARAMETER) String id,
@QueryParam(BaseREST.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) { @QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) {
return super.delete(id, purge); return super.delete(id, purge);
} }

View File

@ -16,9 +16,12 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.Resources;
@ -44,17 +47,20 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.PROFILES) @Path(Profile.PROFILES)
public class Profile extends BaseREST { public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.Profile<Response,Response> {
private static Logger logger = LoggerFactory.getLogger(Profile.class); private static Logger logger = LoggerFactory.getLogger(Profile.class);
public static final String PROFILE_NAME_PARAMETER = "PROFILE_NAME"; public static final String PROFILE_NAME_PARAMETER = "PROFILE_NAME";
@Context
private UriInfo uriInfo;
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String list() { public String list() {
setCalledMethod("GET /" + BaseREST.PROFILES); setCalledMethod("GET /" + PROFILES);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ArrayNode arrayNode = mapper.createArrayNode(); ArrayNode arrayNode = mapper.createArrayNode();
@ -76,8 +82,8 @@ public class Profile extends BaseREST {
@Path("/{" + PROFILE_NAME_PARAMETER + "}") @Path("/{" + PROFILE_NAME_PARAMETER + "}")
@Produces({MediaType.APPLICATION_XML, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) @Produces({MediaType.APPLICATION_XML, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
public String read(@PathParam(PROFILE_NAME_PARAMETER) String name, public String read(@PathParam(PROFILE_NAME_PARAMETER) String name,
@DefaultValue(MediaType.APPLICATION_JSON) @HeaderParam("Accept") String accept) { @DefaultValue(MediaType.APPLICATION_XML) @HeaderParam("Accept") String accept) {
setCalledMethod("GET /" + BaseREST.PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}"); setCalledMethod("GET /" + PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}");
try { try {
String profile = MetadataUtility.getInstance().getMetadataFormat(name).getMetadataSource(); String profile = MetadataUtility.getInstance().getMetadataFormat(name).getMetadataSource();
if(profile != null) { if(profile != null) {
@ -192,17 +198,21 @@ public class Profile extends BaseREST {
@Path("/{" + PROFILE_NAME_PARAMETER + "}") @Path("/{" + PROFILE_NAME_PARAMETER + "}")
@Consumes(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML)
public String createOrUpdate(@PathParam(PROFILE_NAME_PARAMETER) String name, String xml) { public Response createOrUpdate(@PathParam(PROFILE_NAME_PARAMETER) String name, String xml) {
setCalledMethod("PUT /" + BaseREST.PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}"); setCalledMethod("PUT /" + PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}");
try { try {
MetadataUtility metadataUtility = MetadataUtility.getInstance(); MetadataUtility metadataUtility = MetadataUtility.getInstance();
metadataUtility.getDataCalogueMetadataFormatReader().validateProfile(xml); metadataUtility.getDataCalogueMetadataFormatReader().validateProfile(xml);
if(metadataUtility.getMetadataFormat(name) == null) { if(metadataUtility.getMetadataFormat(name) == null) {
createGenericResource(name, xml); createGenericResource(name, xml);
ResponseBuilder responseBuilder = Response.status(Status.CREATED).entity(xml);
responseBuilder.header(LOCATION_HEADER, uriInfo.getAbsolutePath());
return responseBuilder.type(MediaType.APPLICATION_XML).build();
} else { } else {
updateGenericResource(name, xml); updateGenericResource(name, xml);
ResponseBuilder responseBuilder = Response.status(Status.OK).entity(xml);
return responseBuilder.type(MediaType.APPLICATION_XML).build();
} }
return xml;
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
@ -217,7 +227,7 @@ public class Profile extends BaseREST {
@DELETE @DELETE
@Path("/{" + PROFILE_NAME_PARAMETER + "}") @Path("/{" + PROFILE_NAME_PARAMETER + "}")
public Response delete(@PathParam(PROFILE_NAME_PARAMETER) String name) { public Response delete(@PathParam(PROFILE_NAME_PARAMETER) String name) {
setCalledMethod("DELETE /" + BaseREST.PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}"); setCalledMethod("DELETE /" + PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}");
try { try {
MetadataUtility metadataUtility = MetadataUtility.getInstance(); MetadataUtility metadataUtility = MetadataUtility.getInstance();
if(metadataUtility.getMetadataFormat(name) == null) { if(metadataUtility.getMetadataFormat(name) == null) {
@ -236,4 +246,20 @@ public class Profile extends BaseREST {
} }
} }
@Override
public Response create(String name, String xml) {
return createOrUpdate(name, xml);
}
@Override
public String read(String name) {
return read(name, MediaType.APPLICATION_XML);
}
@Override
public String update(String name, String xml) {
return createOrUpdate(name, xml).getEntity().toString();
}
} }

View File

@ -6,9 +6,10 @@ import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.api.interfaces.CRUD;
import org.gcube.gcat.persistence.ckan.CKAN; import org.gcube.gcat.persistence.ckan.CKAN;
public class REST<C extends CKAN> extends BaseREST { public class REST<C extends CKAN> extends BaseREST implements CRUD<Response, Response> {
protected final String COLLECTION_PARAMETER; protected final String COLLECTION_PARAMETER;
protected final String ID_PARAMETER; protected final String ID_PARAMETER;
@ -35,6 +36,7 @@ public class REST<C extends CKAN> extends BaseREST {
return ckan.list(limit, offset); return ckan.list(limit, offset);
} }
@Override
public Response create(String json) { public Response create(String json) {
setCalledMethod("POST /" + COLLECTION_PARAMETER); setCalledMethod("POST /" + COLLECTION_PARAMETER);
C ckan = getInstance(); C ckan = getInstance();
@ -45,6 +47,7 @@ public class REST<C extends CKAN> extends BaseREST {
return responseBuilder.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build(); return responseBuilder.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
} }
@Override
public String read(String id) { public String read(String id) {
setCalledMethod("GET /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}"); setCalledMethod("GET /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}");
C ckan = getInstance(); C ckan = getInstance();
@ -52,6 +55,7 @@ public class REST<C extends CKAN> extends BaseREST {
return ckan.read(); return ckan.read();
} }
@Override
public String update(String id, String json) { public String update(String id, String json) {
setCalledMethod("PUT /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}"); setCalledMethod("PUT /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}");
C ckan = getInstance(); C ckan = getInstance();
@ -67,6 +71,11 @@ public class REST<C extends CKAN> extends BaseREST {
return ckan.patch(json); return ckan.patch(json);
} }
@Override
public Response delete(String id) {
return delete(id, false);
}
public Response delete(String id, Boolean purge) { public Response delete(String id, Boolean purge) {
if(purge) { if(purge) {
setCalledMethod("PURGE /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}"); setCalledMethod("PURGE /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}");

View File

@ -8,6 +8,9 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PATCH;
@ -16,13 +19,13 @@ import org.gcube.gcat.persistence.ckan.CKANResource;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.ITEMS + "/{" + Resource.ITEM_ID_PARAMETER + "}/" + BaseREST.RESOURCES) @Path(Resource.COLLECTION)
public class Resource extends BaseREST { public class Resource extends BaseREST implements org.gcube.gcat.api.interfaces.Resource<Response,Response> {
protected static final String ITEM_ID_PARAMETER = "ITEM_ID"; protected static final String ITEM_ID_PARAMETER = Item.ITEM_ID_PARAMETER;
protected static final String RESOURCE_ID_PARAMETER = "RESOURCE_ID"; protected static final String RESOURCE_ID_PARAMETER = "RESOURCE_ID";
protected static final String COLLECTION = BaseREST.ITEMS + "/{" + Resource.ITEM_ID_PARAMETER + "}/" + BaseREST.RESOURCES; protected static final String COLLECTION = Item.ITEMS + "/{" + ITEM_ID_PARAMETER + "}/" + RESOURCES;
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@ -36,10 +39,14 @@ public class Resource extends BaseREST {
@POST @POST
@Consumes(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Consumes(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String create(@PathParam(ITEM_ID_PARAMETER) String itemID, String json) { public Response create(@PathParam(ITEM_ID_PARAMETER) String itemID, String json) {
setCalledMethod("POST /" + COLLECTION); setCalledMethod("POST /" + COLLECTION);
CKANResource ckanResource = new CKANResource(itemID); CKANResource ckanResource = new CKANResource(itemID);
return ckanResource.create(json); String ret = ckanResource.create(json);
ResponseBuilder responseBuilder = Response.status(Status.CREATED).entity(ret);
responseBuilder = addLocation(responseBuilder, ckanResource.getResourceID());
return responseBuilder.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
} }
@GET @GET
@ -49,7 +56,7 @@ public class Resource extends BaseREST {
@PathParam(RESOURCE_ID_PARAMETER) String resourceID) { @PathParam(RESOURCE_ID_PARAMETER) String resourceID) {
setCalledMethod("GET /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}"); setCalledMethod("GET /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}");
CKANResource ckanResource = new CKANResource(itemID); CKANResource ckanResource = new CKANResource(itemID);
ckanResource.setName(resourceID); ckanResource.setResourceID(resourceID);
return ckanResource.read(); return ckanResource.read();
} }
@ -61,7 +68,7 @@ public class Resource extends BaseREST {
@PathParam(RESOURCE_ID_PARAMETER) String resourceID, String json) { @PathParam(RESOURCE_ID_PARAMETER) String resourceID, String json) {
setCalledMethod("PUT /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}"); setCalledMethod("PUT /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}");
CKANResource ckanResource = new CKANResource(itemID); CKANResource ckanResource = new CKANResource(itemID);
ckanResource.setName(resourceID); ckanResource.setResourceID(resourceID);
return ckanResource.update(json); return ckanResource.update(json);
} }
@ -73,18 +80,19 @@ public class Resource extends BaseREST {
@PathParam(RESOURCE_ID_PARAMETER) String resourceID, String json) { @PathParam(RESOURCE_ID_PARAMETER) String resourceID, String json) {
setCalledMethod("PATCH /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}"); setCalledMethod("PATCH /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}");
CKANResource ckanResource = new CKANResource(itemID); CKANResource ckanResource = new CKANResource(itemID);
ckanResource.setName(resourceID); ckanResource.setResourceID(resourceID);
return ckanResource.patch(json); return ckanResource.patch(json);
} }
@DELETE @DELETE
@Path("/{" + RESOURCE_ID_PARAMETER + "}") @Path("/{" + RESOURCE_ID_PARAMETER + "}")
public void delete(@PathParam(ITEM_ID_PARAMETER) String itemID, public Response delete(@PathParam(ITEM_ID_PARAMETER) String itemID,
@PathParam(RESOURCE_ID_PARAMETER) String resourceID) { @PathParam(RESOURCE_ID_PARAMETER) String resourceID) {
setCalledMethod("DELETE /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}"); setCalledMethod("DELETE /" + COLLECTION + "/{" + RESOURCE_ID_PARAMETER + "}");
CKANResource ckanResource = new CKANResource(itemID); CKANResource ckanResource = new CKANResource(itemID);
ckanResource.setName(resourceID); ckanResource.setResourceID(resourceID);
ckanResource.delete(false); ckanResource.delete(false);
return Response.status(Status.NO_CONTENT).build();
} }
} }

View File

@ -16,13 +16,13 @@ import org.gcube.gcat.persistence.ckan.CKANUser;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@Path(BaseREST.USERS) @Path(User.USERS)
public class User extends REST<CKANUser> { public class User extends REST<CKANUser> implements org.gcube.gcat.api.interfaces.User<Response,Response> {
protected static final String USER_ID_PARAMETER = "USER_ID"; protected static final String USER_ID_PARAMETER = "USER_ID";
public User() { public User() {
super(BaseREST.USERS, USER_ID_PARAMETER, CKANUser.class); super(USERS, USER_ID_PARAMETER, CKANUser.class);
} }
@GET @GET
@ -43,8 +43,8 @@ public class User extends REST<CKANUser> {
@Path("/{" + USER_ID_PARAMETER + "}") @Path("/{" + USER_ID_PARAMETER + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Override @Override
public String read(@PathParam(USER_ID_PARAMETER) String id) { public String read(@PathParam(USER_ID_PARAMETER) String username) {
return super.read(id); return super.read(username);
} }
@PUT @PUT
@ -52,14 +52,14 @@ public class User extends REST<CKANUser> {
@Consumes(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Consumes(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
@Override @Override
public String update(@PathParam(USER_ID_PARAMETER) String id, String json) { public String update(@PathParam(USER_ID_PARAMETER) String username, String json) {
return super.update(id, json); return super.update(username, json);
} }
@DELETE @DELETE
@Path("/{" + USER_ID_PARAMETER + "}") @Path("/{" + USER_ID_PARAMETER + "}")
public Response delete(@PathParam(USER_ID_PARAMETER) String id) { public Response delete(@PathParam(USER_ID_PARAMETER) String username) {
return super.delete(id, false); return super.delete(username, false);
} }
} }

View File

@ -1,8 +1,12 @@
package org.gcube.gcat.utils; package org.gcube.gcat.utils;
import java.io.InputStream; import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
/** /**
@ -12,22 +16,36 @@ public class Constants {
public static final String CATALOGUE_NAME = "gCat"; public static final String CATALOGUE_NAME = "gCat";
private static final String PROPERTY_FILENAME = "config.properties"; private static final String PROPERTY_FILENAME = "config.properties";
private static final String TOKEN_VARNAME = "TOKEN";
private static final String CATALOGUE_APPLICATION_TOKEN; /*
* Key : Context
* Value : Application Token
*/
protected static final Map<String,String> applicationTokens;
public static String getCatalogueApplicationToken() { public static String getCatalogueApplicationToken() {
return CATALOGUE_APPLICATION_TOKEN; try {
return applicationTokens.get(ContextUtility.getCurrentContext());
}catch (Exception e) {
throw new InternalServerErrorException("Unable to retrieve Application Token for context " + ContextUtility.getCurrentContext(), e);
}
} }
static { static {
try { try {
applicationTokens = new HashMap<>();
Properties properties = new Properties(); Properties properties = new Properties();
InputStream input = Constants.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME); InputStream input = Constants.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
// load a properties file // load a properties file
properties.load(input); properties.load(input);
CATALOGUE_APPLICATION_TOKEN = properties.getProperty(TOKEN_VARNAME); Enumeration<?> enumeration = properties.propertyNames();
while(enumeration.hasMoreElements()) {
String context = (String) enumeration.nextElement();
String applicationToken = properties.getProperty(context);
applicationTokens.put(context, applicationToken);
}
}catch (Exception e) { }catch (Exception e) {
throw new WebApplicationException(e); throw new WebApplicationException(e);
} }

View File

@ -0,0 +1,22 @@
package org.gcube.gcat.rest;
import org.gcube.gcat.ContextTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ProfileTest.class);
// @Test
public void read() throws Exception {
Resource resource = new Resource();
String itemID = "";
String resourceID = "";
String ret = resource.read(itemID,resourceID);
logger.debug("{}", ret);
}
}

View File

@ -0,0 +1,20 @@
package org.gcube.gcat.utils;
import org.gcube.gcat.ContextTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConstantsTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ConstantsTest.class);
@Test
public void testGetApplicationToken() {
logger.debug("Application token for Context {} is {}", ContextUtility.getCurrentContext(), Constants.getCatalogueApplicationToken());
}
}