2018-12-04 12:06:22 +01:00
|
|
|
|
package org.gcube.gcat.rest;
|
|
|
|
|
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
|
import javax.ws.rs.DefaultValue;
|
|
|
|
|
import javax.ws.rs.GET;
|
2022-04-12 11:48:12 +02:00
|
|
|
|
//import javax.ws.rs.NotAuthorizedException;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
import javax.ws.rs.POST;
|
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
2021-12-02 11:50:18 +01:00
|
|
|
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
|
|
|
|
import javax.ws.rs.core.Response.Status;
|
2019-01-29 17:46:22 +01:00
|
|
|
|
import javax.xml.ws.WebServiceException;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
|
2022-04-12 11:48:12 +02:00
|
|
|
|
//import org.gcube.common.authorization.control.annotations.AuthorizationControl;
|
2020-11-30 19:44:15 +01:00
|
|
|
|
import org.gcube.gcat.annotation.PATCH;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
import org.gcube.gcat.annotation.PURGE;
|
2019-01-29 17:46:22 +01:00
|
|
|
|
import org.gcube.gcat.api.GCatConstants;
|
2022-04-12 11:48:12 +02:00
|
|
|
|
//import org.gcube.gcat.api.moderation.Moderated;
|
|
|
|
|
//import org.gcube.gcat.api.roles.Role;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
import org.gcube.gcat.persistence.ckan.CKANPackage;
|
2021-12-03 17:41:38 +01:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-12-04 12:06:22 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
|
*/
|
2019-01-29 17:46:22 +01:00
|
|
|
|
@Path(Item.ITEMS)
|
2019-09-16 14:48:18 +02:00
|
|
|
|
public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interfaces.Item<Response,Response> {
|
2018-12-04 12:06:22 +01:00
|
|
|
|
|
2021-12-03 17:41:38 +01:00
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(Item.class);
|
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public static final String ITEM_ID_PARAMETER = "ITEM_ID";
|
|
|
|
|
|
2021-11-19 16:49:43 +01:00
|
|
|
|
protected String moderationMessage;
|
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public Item() {
|
2019-01-29 17:46:22 +01:00
|
|
|
|
super(ITEMS, ITEM_ID_PARAMETER, CKANPackage.class);
|
2018-12-04 12:06:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 11:27:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* The listing API provides paginated results by using the query parameters limit and offset.<br/>
|
|
|
|
|
* It returns an array list of string containing the ids (i.e. names) of the items.<br/>
|
|
|
|
|
* Each name can be used as <code>{ITEM_ID_PARAMETER}</code> path parameter to manage such item.
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* <h4>Filtering options</h4>
|
|
|
|
|
* <p>
|
|
|
|
|
* The listing method offers options to filter the results, thus enacting to search for items including spatial search (see ext_bbox below).<br/>
|
|
|
|
|
* It accepts the following query parameters (a subset of Solr search query parameters, see Solr Query Syntax):
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* <dl>
|
|
|
|
|
* <dt>q (string)</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* the solr query. Optional. Default: "*:*" See Solr Query Syntax.
|
|
|
|
|
* E.g. <code>/items?q=title:foo</code> returns the items with word "foo" in the title;
|
|
|
|
|
* </dd>
|
|
|
|
|
*
|
|
|
|
|
* <dt>fq (string)</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* Filter query. A query string that limits the query results without influencing their scores.
|
|
|
|
|
* Note: <code>+site_id:{ckan_site_id}</code> is added to this string prior to the query being executed.<br/>
|
|
|
|
|
* E.g. <code>/items?q=title:foo&fq=notes:bar</code> returns with word "foo" in the 'title' and the word "bar" in the 'notes';
|
|
|
|
|
* </dd>
|
|
|
|
|
*
|
|
|
|
|
* <dt>fq_list (list of strings)</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* additional filter queries to apply.<br/>
|
|
|
|
|
* E.g. <code>/items?q=title:foo&fq_list=...</code> returns the items with word "foo" in the 'title'
|
|
|
|
|
* </dd>
|
|
|
|
|
*
|
|
|
|
|
* <dt>sort (string)</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* sorting of the search results. Optional. Default: 'relevance asc, metadata_modified desc'.
|
|
|
|
|
* As per the solr documentation, this is a comma-separated string of field names and sort-orderings.<br/>
|
|
|
|
|
* E.g. <code>/items?q=title:foo&sort=name+asc</code> returns the items with word "foo" in the 'title'
|
|
|
|
|
* sorting the results by name ascending;
|
|
|
|
|
* </dd>
|
|
|
|
|
*
|
|
|
|
|
* <dt>include_private (bool)</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* if True, private datasets will be included in the results.
|
|
|
|
|
* Only private datasets from the user’s organizations will be returned. For the sysadmins will be returned all private datasets.
|
|
|
|
|
* Optional, the default is False.<br/>
|
|
|
|
|
* E.g. <code>/items?include_private=true</code>
|
|
|
|
|
* </dd>
|
|
|
|
|
*
|
|
|
|
|
* <dt>ext_bbox</dt>
|
|
|
|
|
* <dd>
|
|
|
|
|
* The coordinates of the upper-right and bottom-left angle of a rectangular to query for.
|
|
|
|
|
* The form is Lat,Long,Lat,Long<br/>
|
|
|
|
|
* E.g. <code>/items?limit=10&offset=0&q=Pollution&ext_bbox=-7.535093,49.208494,3.890688,57.372349</code>
|
|
|
|
|
* returns the first 10 items with 'Pollution' having a spatial coverage in the specified bounding box.
|
|
|
|
|
* </dd>
|
|
|
|
|
* </dl>
|
|
|
|
|
*
|
|
|
|
|
* @param limit To get unlimited results the limit query parameters must be set to -1.
|
|
|
|
|
* If the results are too much the operation could fail.
|
|
|
|
|
* It is recommended to request no more than 1000 results.
|
|
|
|
|
* @param offset The offset parameter indicates the starting position of the result.
|
|
|
|
|
* @param count It indicate that the result must contains only the total number of items of the query.
|
|
|
|
|
* @return It returns an array list of string containing the ids (i.e. names) of the items.<br/>
|
|
|
|
|
* E.g.<pre>["item0","items1",...,"item10"]</pre>
|
|
|
|
|
*
|
|
|
|
|
* In the case the query parameter <code>count=true</code> it returns the total number of items of the query.
|
|
|
|
|
* E.g. <pre>{"count":148}</pre>
|
|
|
|
|
*/
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@GET
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2022-04-01 18:37:45 +02:00
|
|
|
|
/* Catalogue-Member is not added to VRE members and is assumed as the default role in the catalogue for the VRE members. So we can't enforce
|
|
|
|
|
* @AuthorizationControl(allowedRoles={Role.CATALOGUE_MEMBER, Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
|
|
|
|
*/
|
2022-07-26 12:05:37 +02:00
|
|
|
|
public String list(@QueryParam(GCatConstants.LIMIT_QUERY_PARAMETER) @DefaultValue("10") int limit,
|
|
|
|
|
@QueryParam(GCatConstants.OFFSET_QUERY_PARAMETER) @DefaultValue("0") int offset,
|
2022-09-16 11:27:06 +02:00
|
|
|
|
@QueryParam(GCatConstants.COUNT_QUERY_PARAMETER) @DefaultValue("false") Boolean count) {
|
|
|
|
|
if(count) {
|
2021-12-03 17:41:38 +01:00
|
|
|
|
CKANPackage ckan = getInstance();
|
2022-09-16 11:27:06 +02:00
|
|
|
|
int size = ckan.count();
|
|
|
|
|
return createCountJson(size);
|
2021-02-03 16:48:42 +01:00
|
|
|
|
}else {
|
2021-02-03 21:55:32 +01:00
|
|
|
|
return list(limit, offset);
|
2021-02-03 16:48:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
|
|
|
|
|
*/
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@Override
|
2022-07-26 12:05:37 +02:00
|
|
|
|
public String list(@QueryParam(GCatConstants.LIMIT_QUERY_PARAMETER) @DefaultValue("10") int limit,
|
|
|
|
|
@QueryParam(GCatConstants.OFFSET_QUERY_PARAMETER) @DefaultValue("0") int offset) {
|
2019-01-10 12:29:47 +01:00
|
|
|
|
return super.list(limit, offset);
|
2018-12-04 12:06:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public Response create(String json) {
|
|
|
|
|
return super.create(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@Override
|
2022-04-01 18:37:45 +02:00
|
|
|
|
/* Catalogue-Member is not added to VRE members and is assumed as the default role in the catalogue for the VRE members. So we can't enforce
|
|
|
|
|
* @AuthorizationControl(allowedRoles={Role.CATALOGUE_MEMBER, Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
|
|
|
|
*/
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public String read(@PathParam(ITEM_ID_PARAMETER) String id) {
|
|
|
|
|
return super.read(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public String update(@PathParam(ITEM_ID_PARAMETER) String id, String json) {
|
|
|
|
|
return super.update(id, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PATCH
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public String patch(@PathParam(ITEM_ID_PARAMETER) String id, String json) {
|
|
|
|
|
return super.patch(id, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DELETE
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public Response delete(@PathParam(ITEM_ID_PARAMETER) String id,
|
2019-01-29 17:46:22 +01:00
|
|
|
|
@QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) {
|
2018-12-04 12:06:22 +01:00
|
|
|
|
return super.delete(id, purge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PURGE
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2018-12-04 12:06:22 +01:00
|
|
|
|
public Response purge(@PathParam(ITEM_ID_PARAMETER) String id) {
|
|
|
|
|
return super.purge(id);
|
|
|
|
|
}
|
2019-09-16 14:48:18 +02:00
|
|
|
|
|
2019-01-29 17:46:22 +01:00
|
|
|
|
@Override
|
|
|
|
|
public Response delete(String name, boolean purge) throws WebServiceException {
|
|
|
|
|
return delete(name, new Boolean(purge));
|
|
|
|
|
}
|
2021-11-19 16:49:43 +01:00
|
|
|
|
|
2021-12-03 17:41:38 +01:00
|
|
|
|
protected void deleteAll(boolean purge) {
|
|
|
|
|
Thread thread = new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
CKANPackage ckan = getInstance();
|
|
|
|
|
String ret = ckan.deleteAll(purge);
|
|
|
|
|
logger.info("Result of delete all is {}", ret);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DELETE
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2021-12-03 17:41:38 +01:00
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2021-12-03 17:41:38 +01:00
|
|
|
|
public Response bulkDelete(@QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") boolean purge) {
|
|
|
|
|
deleteAll(purge);
|
|
|
|
|
return Response.status(Status.ACCEPTED).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PURGE
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2021-12-03 17:41:38 +01:00
|
|
|
|
@Override
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
|
2021-12-03 17:41:38 +01:00
|
|
|
|
public Response bulkPurge() {
|
|
|
|
|
return bulkDelete(true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 11:50:18 +01:00
|
|
|
|
@POST
|
|
|
|
|
@Path("/{" + ITEM_ID_PARAMETER + "}")
|
2022-02-21 10:34:52 +01:00
|
|
|
|
@Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
|
|
|
|
@Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8)
|
2022-04-12 11:48:12 +02:00
|
|
|
|
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER, Moderated.CATALOGUE_MODERATOR}, exception=NotAuthorizedException.class)
|
2021-12-02 11:50:18 +01:00
|
|
|
|
public Response moderate(@PathParam(ITEM_ID_PARAMETER) String id, String json) {
|
|
|
|
|
setCalledMethod("POST /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}");
|
|
|
|
|
CKANPackage ckanPackage = getInstance();
|
|
|
|
|
ckanPackage.setName(id);
|
|
|
|
|
String ret = ckanPackage.moderate(json);
|
|
|
|
|
ResponseBuilder responseBuilder = Response.status(Status.ACCEPTED);
|
|
|
|
|
if(ret!=null) {
|
2022-02-21 10:34:52 +01:00
|
|
|
|
responseBuilder.entity(ret).type(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8);
|
2021-12-02 11:50:18 +01:00
|
|
|
|
}
|
|
|
|
|
return responseBuilder.build();
|
2021-11-19 16:49:43 +01:00
|
|
|
|
}
|
2021-12-03 17:41:38 +01:00
|
|
|
|
|
2018-12-04 12:06:22 +01:00
|
|
|
|
}
|