docs/docs-web/src/main/java/com/sismics/docs/rest/resource/ShareResource.java

115 lines
3.5 KiB
Java
Raw Normal View History

2013-08-13 21:05:14 +02:00
package com.sismics.docs.rest.resource;
2013-08-13 23:15:58 +02:00
import java.text.MessageFormat;
import javax.persistence.NoResultException;
2013-08-13 21:05:14 +02:00
import javax.ws.rs.DELETE;
2013-08-13 23:15:58 +02:00
import javax.ws.rs.FormParam;
2013-08-13 21:05:14 +02:00
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
2013-08-13 23:15:58 +02:00
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.ShareDao;
import com.sismics.docs.core.model.jpa.Share;
2013-08-13 23:15:58 +02:00
import com.sismics.rest.exception.ClientException;
2013-08-13 21:05:14 +02:00
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
/**
* Share REST resources.
2013-08-13 21:05:14 +02:00
*
* @author bgamard
*/
@Path("/share")
public class ShareResource extends BaseResource {
2013-08-13 21:05:14 +02:00
/**
* Add a share to a document.
2013-08-13 21:05:14 +02:00
*
* @param documentId Document ID
2013-08-13 21:05:14 +02:00
* @return Response
* @throws JSONException
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
public Response add(
@FormParam("id") String documentId,
@FormParam("name") String name) throws JSONException {
2013-08-13 21:05:14 +02:00
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Validate input data
ValidationUtil.validateRequired(documentId, "id");
name = ValidationUtil.validateLength(name, "name", 1, 36, true);
2013-08-13 21:05:14 +02:00
// Get the document
2013-08-13 23:15:58 +02:00
DocumentDao documentDao = new DocumentDao();
try {
documentDao.getDocument(documentId, principal.getId());
2013-08-13 23:15:58 +02:00
} catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId));
2013-08-13 23:15:58 +02:00
}
// Create the share
ShareDao shareDao = new ShareDao();
Share share = new Share();
share.setDocumentId(documentId);
share.setName(name);
shareDao.create(share);
2013-08-13 21:05:14 +02:00
// Always return ok
JSONObject response = new JSONObject();
response.put("status", "ok");
response.put("id", share.getId());
2013-08-13 21:05:14 +02:00
return Response.ok().entity(response).build();
}
/**
* Deletes a share.
2013-08-13 21:05:14 +02:00
*
* @param id Share ID
2013-08-13 21:05:14 +02:00
* @return Response
* @throws JSONException
*/
@DELETE
@Path("{id: [a-z0-9\\-]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response delete(
@PathParam("id") String id) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Get the share
ShareDao shareDao = new ShareDao();
2013-08-13 23:15:58 +02:00
DocumentDao documentDao = new DocumentDao();
2014-12-01 01:21:38 +01:00
Share share = shareDao.getShare(id);
if (share == null) {
throw new ClientException("ShareNotFound", MessageFormat.format("Share not found: {0}", id));
}
// Check that the user is the owner of the linked document
2013-08-13 23:15:58 +02:00
try {
documentDao.getDocument(share.getDocumentId(), principal.getId());
2013-08-13 23:15:58 +02:00
} catch (NoResultException e) {
2014-12-01 01:21:38 +01:00
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", share.getDocumentId()));
2013-08-13 23:15:58 +02:00
}
2013-08-13 21:05:14 +02:00
// Delete the share
shareDao.delete(share.getId());
2013-08-13 23:15:58 +02:00
2013-08-13 21:05:14 +02:00
// Always return ok
JSONObject response = new JSONObject();
response.put("status", "ok");
return Response.ok().entity(response).build();
}
}