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; import javax.ws.rs.NotAuthorizedException; 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; import javax.xml.ws.WebServiceException; import org.gcube.common.authorization.control.annotations.AuthorizationControl; import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.roles.Role; import org.gcube.gcat.persistence.ckan.CKANOrganization; /** * @author Luca Frosini (ISTI - CNR) */ @Path(Organization.ORGANIZATIONS) public class Organization extends REST implements org.gcube.gcat.api.interfaces.Organization { public static final String ORGANIZATION_ID_PARAMETER = "ORGANIZATION_ID"; public Organization() { super(ORGANIZATIONS, ORGANIZATION_ID_PARAMETER, CKANOrganization.class); } /* * Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface */ @Override public int count() throws WebServiceException { CKANOrganization ckan = getInstance(); return ckan.count(); } @GET @Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit, @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset, @QueryParam(GCatConstants.COUNT_PARAMETER) @DefaultValue("false") Boolean countOnly) { if(countOnly) { int count = count(); return createCountJson(count); }else { return list(limit, offset); } } /* * Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface */ @Override public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit, @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) { return super.list(limit, offset); } @POST @Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Override @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public Response create(String json) { return super.create(json); } @GET @Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Override @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public String read(@PathParam(ORGANIZATION_ID_PARAMETER) String id) { return super.read(id); } @PUT @Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Override @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public String update(@PathParam(ORGANIZATION_ID_PARAMETER) String id, String json) { return super.update(id, json); } @PATCH @Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @Consumes(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Produces(GCatConstants.APPLICATION_JSON_CHARSET_UTF_8) @Override @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public String patch(@PathParam(ORGANIZATION_ID_PARAMETER) String id, String json) { return super.patch(id, json); } @DELETE @Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @Override @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public Response delete(@PathParam(ORGANIZATION_ID_PARAMETER) String id, @QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) { return super.delete(id, purge); } @PURGE @Path("/{" + ORGANIZATION_ID_PARAMETER + "}") @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class) public Response purge(@PathParam(ORGANIZATION_ID_PARAMETER) String id) { return super.purge(id); } }