mirror of
https://github.com/sismics/docs.git
synced 2025-12-27 00:22:33 +00:00
Name on share, share document (not file)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user