1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 10:16:21 +00:00

Name on share, share document (not file)

This commit is contained in:
jendib
2013-08-14 20:51:08 +02:00
parent 4cb5b7fdbb
commit 200d7a0001
14 changed files with 430 additions and 359 deletions

View File

@@ -1,12 +1,38 @@
package com.sismics.docs.rest.resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.NoResultException;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
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.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.google.common.base.Strings;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.ShareDao;
import com.sismics.docs.core.dao.jpa.TagDao;
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import com.sismics.docs.core.dao.jpa.dto.TagDto;
import com.sismics.docs.core.model.jpa.Document;
import com.sismics.docs.core.model.jpa.Share;
import com.sismics.docs.core.model.jpa.Tag;
import com.sismics.docs.core.util.jpa.PaginatedList;
import com.sismics.docs.core.util.jpa.PaginatedLists;
@@ -14,16 +40,6 @@ import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import javax.persistence.NoResultException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.text.MessageFormat;
import java.util.*;
/**
* Document REST resources.
@@ -43,15 +59,20 @@ public class DocumentResource extends BaseResource {
@Path("{id: [a-z0-9\\-]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(
@PathParam("id") String id) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
@PathParam("id") String id,
@QueryParam("share") String shareId) throws JSONException {
authenticate();
DocumentDao documentDao = new DocumentDao();
ShareDao shareDao = new ShareDao();
Document documentDb;
try {
documentDb = documentDao.getDocument(id, principal.getId());
documentDb = documentDao.getDocument(id);
// Check document visibility
if (!shareDao.checkVisibility(documentDb, principal.getId(), shareId)) {
throw new ForbiddenClientException();
}
} catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", id));
}
@@ -62,7 +83,7 @@ public class DocumentResource extends BaseResource {
document.put("description", documentDb.getDescription());
document.put("create_date", documentDb.getCreateDate().getTime());
// Get tags
// Add tags
TagDao tagDao = new TagDao();
List<TagDto> tagDtoList = tagDao.getByDocumentId(id);
List<JSONObject> tags = new ArrayList<>();
@@ -75,6 +96,17 @@ public class DocumentResource extends BaseResource {
}
document.put("tags", tags);
// Add shares
List<Share> shareDbList = shareDao.getByDocumentId(id);
List<JSONObject> shareList = new ArrayList<>();
for (Share shareDb : shareDbList) {
JSONObject share = new JSONObject();
share.put("id", shareDb.getId());
share.put("name", shareDb.getName());
shareList.add(share);
}
document.put("shares", shareList);
return Response.ok().entity(document).build();
}

View File

@@ -28,10 +28,9 @@ import org.codehaus.jettison.json.JSONObject;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.FileShareDao;
import com.sismics.docs.core.dao.jpa.ShareDao;
import com.sismics.docs.core.model.jpa.Document;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.FileShare;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
@@ -50,52 +49,6 @@ import com.sun.jersey.multipart.FormDataParam;
*/
@Path("/file")
public class FileResource extends BaseResource {
/**
* Returns a file.
*
* @param id Document ID
* @return Response
* @throws JSONException
*/
@GET
@Path("{id: [a-z0-9\\-]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(
@PathParam("id") String id) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
FileDao fileDao = new FileDao();
FileShareDao fileShareDao = new FileShareDao();
DocumentDao documentDao = new DocumentDao();
File fileDb;
try {
fileDb = fileDao.getFile(id);
documentDao.getDocument(fileDb.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id));
}
JSONObject file = new JSONObject();
file.put("id", fileDb.getId());
file.put("mimetype", fileDb.getMimeType());
file.put("document_id", fileDb.getDocumentId());
file.put("create_date", fileDb.getCreateDate().getTime());
// Add file shares
List<FileShare> fileShareDbList = fileShareDao.getByFileId(id);
List<JSONObject> fileShareList = new ArrayList<>();
for (FileShare fileShareDb : fileShareDbList) {
JSONObject fileShare = new JSONObject();
fileShare.put("id", fileShareDb.getId());
fileShareList.add(fileShare);
}
file.put("shares", fileShareList);
return Response.ok().entity(file).build();
}
/**
* Add a file to a document.
*
@@ -224,8 +177,15 @@ public class FileResource extends BaseResource {
@Path("list")
@Produces(MediaType.APPLICATION_JSON)
public Response list(
@QueryParam("id") String documentId) throws JSONException {
if (!authenticate()) {
@QueryParam("id") String documentId,
@QueryParam("share") String shareId) throws JSONException {
authenticate();
// Check document visibility
DocumentDao documentDao = new DocumentDao();
Document document = documentDao.getDocument(documentId);
ShareDao shareDao = new ShareDao();
if (!shareDao.checkVisibility(document, principal.getId(), shareId)) {
throw new ForbiddenClientException();
}
@@ -297,21 +257,9 @@ public class FileResource extends BaseResource {
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response data(
@PathParam("id") final String fileId,
@QueryParam("share") String fileShareId,
@QueryParam("share") String shareId,
@QueryParam("thumbnail") boolean thumbnail) throws JSONException {
// Handle file sharing
FileShareDao fileShareDao = new FileShareDao();
if (fileShareId != null) {
FileShare fileShare = fileShareDao.getFileShare(fileShareId);
if (fileShare == null) {
throw new ClientException("FileShareNotFound", "File share not found");
}
if (!fileShare.getFileId().equals(fileId)) {
throw new ClientException("InvalidFile", "This file share is not linked to this file");
}
} else if (!authenticate()) {
throw new ForbiddenClientException();
}
authenticate();
// Get the file
FileDao fileDao = new FileDao();
@@ -319,8 +267,12 @@ public class FileResource extends BaseResource {
File file;
try {
file = fileDao.getFile(fileId);
if (fileShareId == null) {
documentDao.getDocument(file.getDocumentId(), principal.getId());
Document document = documentDao.getDocument(file.getDocumentId());
// Check document visibility
ShareDao shareDao = new ShareDao();
if (!shareDao.checkVisibility(document, principal.getId(), shareId)) {
throw new ForbiddenClientException();
}
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", fileId));

View File

@@ -17,67 +17,65 @@ import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.FileShareDao;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.FileShare;
import com.sismics.docs.core.dao.jpa.ShareDao;
import com.sismics.docs.core.model.jpa.Share;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
/**
* File share REST resources.
* Share REST resources.
*
* @author bgamard
*/
@Path("/fileshare")
public class FileShareResource extends BaseResource {
@Path("/share")
public class ShareResource extends BaseResource {
/**
* Add a file share to a file.
* Add a share to a document.
*
* @param fileId File ID
* @param fileBodyPart File to add
* @param documentId Document ID
* @return Response
* @throws JSONException
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
public Response add(
@FormParam("id") String fileId) throws JSONException {
@FormParam("id") String documentId,
@FormParam("name") String name) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Validate input data
ValidationUtil.validateRequired(fileId, "id");
ValidationUtil.validateRequired(documentId, "id");
name = ValidationUtil.validateLength(name, "name", 1, 36, true);
// Get the file
FileDao fileDao = new FileDao();
// Get the document
DocumentDao documentDao = new DocumentDao();
try {
File file = fileDao.getFile(fileId);
documentDao.getDocument(file.getDocumentId(), principal.getId());
documentDao.getDocument(documentId, principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", fileId));
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId));
}
// Create the file share
FileShareDao fileShareDao = new FileShareDao();
FileShare fileShare = new FileShare();
fileShare.setFileId(fileId);
fileShareDao.create(fileShare);
// Create the share
ShareDao shareDao = new ShareDao();
Share share = new Share();
share.setDocumentId(documentId);
share.setName(name);
shareDao.create(share);
// Always return ok
JSONObject response = new JSONObject();
response.put("status", "ok");
response.put("id", fileShare.getId());
response.put("id", share.getId());
return Response.ok().entity(response).build();
}
/**
* Deletes a file share.
* Deletes a share.
*
* @param id File share ID
* @param id Share ID
* @return Response
* @throws JSONException
*/
@@ -90,21 +88,19 @@ public class FileShareResource extends BaseResource {
throw new ForbiddenClientException();
}
// Get the file share
FileShareDao fileShareDao = new FileShareDao();
FileDao fileDao = new FileDao();
// Get the share
ShareDao shareDao = new ShareDao();
DocumentDao documentDao = new DocumentDao();
FileShare fileShare;
Share share;
try {
fileShare = fileShareDao.getFileShare(id);
File file = fileDao.getFile(fileShare.getFileId());
documentDao.getDocument(file.getDocumentId(), principal.getId());
share = shareDao.getShare(id);
documentDao.getDocument(share.getDocumentId(), principal.getId());
} catch (NoResultException e) {
throw new ClientException("FileShareNotFound", MessageFormat.format("File share not found: {0}", id));
throw new ClientException("ShareNotFound", MessageFormat.format("Share not found: {0}", id));
}
// Delete the file share
fileShareDao.delete(fileShare.getId());
// Delete the share
shareDao.delete(share.getId());
// Always return ok
JSONObject response = new JSONObject();

View File

@@ -1,22 +1,24 @@
package com.sismics.docs.rest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.ws.rs.core.MediaType;
import junit.framework.Assert;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import com.google.common.io.ByteStreams;
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
import com.sismics.util.mime.MimeType;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import junit.framework.Assert;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import javax.ws.rs.core.MediaType;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Exhaustive test of the file resource.
@@ -76,15 +78,6 @@ public class TestFileResource extends BaseJerseyTest {
json = response.getEntity(JSONObject.class);
String file2Id = json.getString("id");
// Get the file
fileResource = resource().path("/file/" + file1Id);
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
response = fileResource.get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
Assert.assertEquals(MimeType.IMAGE_JPEG, json.getString("mimetype"));
Assert.assertEquals(file1Id, json.getString("id"));
// Get the file data
fileResource = resource().path("/file/" + file1Id + "/data");
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));

View File

@@ -7,6 +7,7 @@ import javax.ws.rs.core.MediaType;
import junit.framework.Assert;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
@@ -20,25 +21,25 @@ import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
/**
* Exhaustive test of the file share resource.
* Exhaustive test of the share resource.
*
* @author bgamard
*/
public class TestFileShareResource extends BaseJerseyTest {
public class TestShareResource extends BaseJerseyTest {
/**
* Test the file share resource.
* Test the share resource.
*
* @throws Exception
*/
@Test
public void testFileShareResource() throws Exception {
// Login fileshare1
clientUtil.createUser("fileshare1");
String fileShare1AuthenticationToken = clientUtil.login("fileshare1");
public void testShareResource() throws Exception {
// Login share1
clientUtil.createUser("share1");
String share1AuthenticationToken = clientUtil.login("share1");
// Create a document
WebResource documentResource = resource().path("/document");
documentResource.addFilter(new CookieAuthenticationFilter(fileShare1AuthenticationToken));
documentResource.addFilter(new CookieAuthenticationFilter(share1AuthenticationToken));
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
postParams.add("title", "File test document 1");
ClientResponse response = documentResource.put(ClientResponse.class, postParams);
@@ -49,7 +50,7 @@ public class TestFileShareResource extends BaseJerseyTest {
// Add a file
WebResource fileResource = resource().path("/file");
fileResource.addFilter(new CookieAuthenticationFilter(fileShare1AuthenticationToken));
fileResource.addFilter(new CookieAuthenticationFilter(share1AuthenticationToken));
FormDataMultiPart form = new FormDataMultiPart();
InputStream file = this.getClass().getResourceAsStream("/file/PIA00452.jpg");
FormDataBodyPart fdp = new FormDataBodyPart("file",
@@ -62,40 +63,54 @@ public class TestFileShareResource extends BaseJerseyTest {
json = response.getEntity(JSONObject.class);
String file1Id = json.getString("id");
// Share this file
WebResource fileShareResource = resource().path("/fileshare");
fileShareResource.addFilter(new CookieAuthenticationFilter(fileShare1AuthenticationToken));
// Share this document
WebResource fileShareResource = resource().path("/share");
fileShareResource.addFilter(new CookieAuthenticationFilter(share1AuthenticationToken));
postParams = new MultivaluedMapImpl();
postParams.add("id", file1Id);
postParams.add("id", document1Id);
postParams.add("name", "4 All");
response = fileShareResource.put(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
String fileShare1Id = json.getString("id");
String share1Id = json.getString("id");
// Get the document anonymously
documentResource = resource().path("/document/" + document1Id);
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
getParams.putSingle("share", share1Id);
response = documentResource.queryParams(getParams).get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
Assert.assertEquals(document1Id, json.getString("id"));
Assert.assertEquals(1, json.getJSONArray("shares").length());
Assert.assertEquals(share1Id, json.getJSONArray("shares").getJSONObject(0).getString("id"));
Assert.assertEquals("4 All", json.getJSONArray("shares").getJSONObject(0).getString("name"));
// Get all files from this document anonymously
fileResource = resource().path("/file/list");
getParams = new MultivaluedMapImpl();
getParams.putSingle("id", document1Id);
getParams.putSingle("share", share1Id);
response = fileResource.queryParams(getParams).get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
JSONArray files = json.getJSONArray("files");
Assert.assertEquals(1, files.length());
// Get the file data anonymously
fileResource = resource().path("/file/" + file1Id + "/data");
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
getParams = new MultivaluedMapImpl();
getParams.putSingle("thumbnail", false);
getParams.putSingle("share", fileShare1Id);
getParams.putSingle("share", share1Id);
response = fileResource.queryParams(getParams).get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
InputStream is = response.getEntityInputStream();
byte[] fileBytes = ByteStreams.toByteArray(is);
Assert.assertEquals(163510, fileBytes.length);
// Get the file
fileResource = resource().path("/file/" + file1Id);
fileResource.addFilter(new CookieAuthenticationFilter(fileShare1AuthenticationToken));
response = fileResource.get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
Assert.assertEquals(file1Id, json.getString("id"));
Assert.assertEquals(1, json.getJSONArray("shares").length());
Assert.assertEquals(fileShare1Id, json.getJSONArray("shares").getJSONObject(0).getString("id"));
// Deletes the share
fileShareResource = resource().path("/fileshare/" + fileShare1Id);
fileShareResource.addFilter(new CookieAuthenticationFilter(fileShare1AuthenticationToken));
fileShareResource = resource().path("/share/" + share1Id);
fileShareResource.addFilter(new CookieAuthenticationFilter(share1AuthenticationToken));
response = fileShareResource.delete(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);