mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 01:36:18 +00:00
#300: custom metadata fields: API admin
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=23
|
||||
db.version=24
|
||||
@@ -0,0 +1,208 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import com.sismics.docs.core.constant.MetadataType;
|
||||
import com.sismics.docs.core.dao.MetadataDao;
|
||||
import com.sismics.docs.core.dao.criteria.MetadataCriteria;
|
||||
import com.sismics.docs.core.dao.dto.MetadataDto;
|
||||
import com.sismics.docs.core.model.jpa.Metadata;
|
||||
import com.sismics.docs.core.util.jpa.SortCriteria;
|
||||
import com.sismics.docs.rest.constant.BaseFunction;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArrayBuilder;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Metadata REST resources.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
@Path("/metadata")
|
||||
public class MetadataResource extends BaseResource {
|
||||
/**
|
||||
* Returns the list of all configured metadata.
|
||||
*
|
||||
* @api {get} /metadata Get configured metadata
|
||||
* @apiName GetMetadata
|
||||
* @apiGroup Metadata
|
||||
* @apiParam {Number} sort_column Column index to sort on
|
||||
* @apiParam {Boolean} asc If true, sort in ascending order
|
||||
* @apiSuccess {Object[]} metadata List of metadata
|
||||
* @apiSuccess {String} metadata.id ID
|
||||
* @apiSuccess {String} metadata.name Name
|
||||
* @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} metadata.type Type
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.7.0
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@GET
|
||||
public Response list(
|
||||
@QueryParam("sort_column") Integer sortColumn,
|
||||
@QueryParam("asc") Boolean asc) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
JsonArrayBuilder metadata = Json.createArrayBuilder();
|
||||
SortCriteria sortCriteria = new SortCriteria(sortColumn, asc);
|
||||
|
||||
MetadataDao metadataDao = new MetadataDao();
|
||||
List<MetadataDto> metadataDtoList = metadataDao.findByCriteria(new MetadataCriteria(), sortCriteria);
|
||||
for (MetadataDto metadataDto : metadataDtoList) {
|
||||
metadata.add(Json.createObjectBuilder()
|
||||
.add("id", metadataDto.getId())
|
||||
.add("name", metadataDto.getName())
|
||||
.add("type", metadataDto.getType().name()));
|
||||
}
|
||||
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("metadata", metadata);
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a metadata.
|
||||
*
|
||||
* @api {put} /metadata Add a custom metadata
|
||||
* @apiName PutMetadata
|
||||
* @apiGroup Metadata
|
||||
* @apiParam {String{1..50}} name Name
|
||||
* @apiParam {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type
|
||||
* @apiSuccess {String} id ID
|
||||
* @apiSuccess {String} name Name
|
||||
* @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) ValidationError Validation error
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.7.0
|
||||
*
|
||||
* @param name Name
|
||||
* @param typeStr Type
|
||||
* @return Response
|
||||
*/
|
||||
@PUT
|
||||
public Response add(@FormParam("name") String name,
|
||||
@FormParam("type") String typeStr) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Validate input data
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
|
||||
MetadataType type = MetadataType.valueOf(ValidationUtil.validateLength(typeStr, "type", 1, 20, false));
|
||||
|
||||
// Create the metadata
|
||||
MetadataDao metadataDao = new MetadataDao();
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setName(name);
|
||||
metadata.setType(type);
|
||||
metadataDao.create(metadata, principal.getId());
|
||||
|
||||
// Returns the metadata
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", metadata.getId())
|
||||
.add("name", metadata.getName())
|
||||
.add("type", metadata.getType().name());
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a metadata.
|
||||
*
|
||||
* @api {post} /metadata/:id Update a custom metadata
|
||||
* @apiName PostMetadataId
|
||||
* @apiGroup Metadata
|
||||
* @apiParam {String} id Metadata ID
|
||||
* @apiParam {String{1..50}} name Name
|
||||
* @apiSuccess {String} id ID
|
||||
* @apiSuccess {String} name Name
|
||||
* @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) ValidationError Validation error
|
||||
* @apiError (client) NotFound Metadata not found
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.7.0
|
||||
*
|
||||
* @param id ID
|
||||
* @param name Name
|
||||
* @return Response
|
||||
*/
|
||||
@POST
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response update(@PathParam("id") String id,
|
||||
@FormParam("name") String name) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Validate input data
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
|
||||
|
||||
// Get the metadata
|
||||
MetadataDao metadataDao = new MetadataDao();
|
||||
Metadata metadata = metadataDao.getActiveById(id);
|
||||
if (metadata == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Update the metadata
|
||||
metadata.setName(name);
|
||||
metadataDao.update(metadata, principal.getId());
|
||||
|
||||
// Returns the metadata
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", metadata.getId())
|
||||
.add("name", metadata.getName())
|
||||
.add("type", metadata.getType().name());
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a metadata.
|
||||
*
|
||||
* @api {delete} /metadata/:id Delete a custom metadata
|
||||
* @apiName DeleteMetadataId
|
||||
* @apiGroup Metadata
|
||||
* @apiParam {String} id Metadata ID
|
||||
* @apiSuccess {String} status Status OK
|
||||
* @apiError (client) ForbiddenError Access denied
|
||||
* @apiError (client) NotFound Metadata not found
|
||||
* @apiPermission admin
|
||||
* @apiVersion 1.7.0
|
||||
*
|
||||
* @param id ID
|
||||
* @return Response
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response delete(@PathParam("id") String id) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
checkBaseFunction(BaseFunction.ADMIN);
|
||||
|
||||
// Get the metadata
|
||||
MetadataDao metadataDao = new MetadataDao();
|
||||
Metadata metadata = metadataDao.getActiveById(id);
|
||||
if (metadata == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Delete the metadata
|
||||
metadataDao.delete(id, principal.getId());
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("status", "ok");
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
}
|
||||
@@ -198,7 +198,7 @@ public class VocabularyResource extends BaseResource {
|
||||
/**
|
||||
* Delete a vocabulary entry.
|
||||
*
|
||||
* @api {delete} /vocabulary/:id Delete vocabulary entry
|
||||
* @api {delete} /vocabulary/:id Delete a vocabulary entry
|
||||
* @apiName DeleteVocabularyId
|
||||
* @apiGroup Vocabulary
|
||||
* @apiParam {String} id Entry ID
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=23
|
||||
db.version=24
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=23
|
||||
db.version=24
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
|
||||
/**
|
||||
* Test the metadata resource.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class TestMetadataResource extends BaseJerseyTest {
|
||||
/**
|
||||
* Test the metadata resource.
|
||||
*/
|
||||
@Test
|
||||
public void testMetadataResource() {
|
||||
// Login admin
|
||||
String adminToken = clientUtil.login("admin", "admin", false);
|
||||
|
||||
// Get all metadata with admin
|
||||
JsonObject json = target().path("/metadata")
|
||||
.queryParam("sort_column", "2")
|
||||
.queryParam("asc", "false")
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
JsonArray metadata = json.getJsonArray("metadata");
|
||||
Assert.assertEquals(0, metadata.size());
|
||||
|
||||
// Create a metadata with admin
|
||||
json = target().path("/metadata").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "ISBN 13")
|
||||
.param("type", "STRING")), JsonObject.class);
|
||||
String metadataIsbnId = json.getString("id");
|
||||
Assert.assertNotNull(metadataIsbnId);
|
||||
Assert.assertEquals("ISBN 13", json.getString("name"));
|
||||
Assert.assertEquals("STRING", json.getString("type"));
|
||||
|
||||
// Get all metadata with admin
|
||||
json = target().path("/metadata")
|
||||
.queryParam("sort_column", "2")
|
||||
.queryParam("asc", "false")
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
metadata = json.getJsonArray("metadata");
|
||||
Assert.assertEquals(1, metadata.size());
|
||||
|
||||
// Update a metadata with admin
|
||||
json = target().path("/metadata/" + metadataIsbnId).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.post(Entity.form(new Form()
|
||||
.param("name", "ISBN 10")), JsonObject.class);
|
||||
Assert.assertEquals(metadataIsbnId, json.getString("id"));
|
||||
Assert.assertEquals("ISBN 10", json.getString("name"));
|
||||
Assert.assertEquals("STRING", json.getString("type"));
|
||||
|
||||
// Delete a metadata with admin
|
||||
target().path("/metadata/" + metadataIsbnId).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.delete(JsonObject.class);
|
||||
|
||||
// Get all metadata with admin
|
||||
json = target().path("/metadata")
|
||||
.queryParam("sort_column", "2")
|
||||
.queryParam("asc", "false")
|
||||
.request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.get(JsonObject.class);
|
||||
metadata = json.getJsonArray("metadata");
|
||||
Assert.assertEquals(0, metadata.size());
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
|
||||
/**
|
||||
* Exhaustive test of the vocabulary resource.
|
||||
*
|
||||
@@ -100,7 +99,7 @@ public class TestVocabularyResource extends BaseJerseyTest {
|
||||
Assert.assertEquals(1, entry.getJsonNumber("order").intValue());
|
||||
|
||||
// Delete a vocabulary entry with admin
|
||||
json = target().path("/vocabulary/" + vocabulary1Id).request()
|
||||
target().path("/vocabulary/" + vocabulary1Id).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.delete(JsonObject.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user