From d0c259ead2cb3707845149de9438c6afd0a5fe48 Mon Sep 17 00:00:00 2001 From: jendib Date: Fri, 6 Mar 2015 21:23:50 +0100 Subject: [PATCH] List orphan files --- .../sismics/docs/core/dao/jpa/FileDao.java | 4 ++++ .../docs/rest/resource/FileResource.java | 22 +++++++++++-------- .../sismics/docs/rest/TestFileResource.java | 14 ++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/FileDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/FileDao.java index bbc5d2aa..e8af4e83 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/FileDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/FileDao.java @@ -125,6 +125,10 @@ public class FileDao { @SuppressWarnings("unchecked") public List getByDocumentId(String documentId) { EntityManager em = ThreadLocalContext.get().getEntityManager(); + if (documentId == null) { + Query q = em.createQuery("select f from File f where f.documentId is null and f.deleteDate is null order by f.createDate asc"); + return q.getResultList(); + } Query q = em.createQuery("select f from File f where f.documentId = :documentId and f.deleteDate is null order by f.order asc"); q.setParameter("documentId", documentId); return q.getResultList(); diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java index fb10dab4..2700a7f2 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java @@ -286,18 +286,22 @@ public class FileResource extends BaseResource { public Response list( @QueryParam("id") String documentId, @QueryParam("share") String shareId) throws JSONException { - authenticate(); + boolean authenticated = authenticate(); // Check document visibility - try { - DocumentDao documentDao = new DocumentDao(); - Document document = documentDao.getDocument(documentId); - ShareDao shareDao = new ShareDao(); - if (!shareDao.checkVisibility(document, principal.getId(), shareId)) { - throw new ForbiddenClientException(); + if (documentId != null) { + try { + DocumentDao documentDao = new DocumentDao(); + Document document = documentDao.getDocument(documentId); + ShareDao shareDao = new ShareDao(); + if (!shareDao.checkVisibility(document, principal.getId(), shareId)) { + throw new ForbiddenClientException(); + } + } catch (NoResultException e) { + throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); } - } catch (NoResultException e) { - throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); + } else if (!authenticated) { + throw new ForbiddenClientException(); } FileDao fileDao = new FileDao(); diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java index 78f347e5..2db66c69 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java @@ -218,6 +218,16 @@ public class TestFileResource extends BaseJerseyTest { JSONObject json = response.getEntity(JSONObject.class); String file1Id = json.getString("id"); + // Get all orphan files + fileResource = resource().path("/file/list"); + fileResource.addFilter(new CookieAuthenticationFilter(file2AuthenticationToken)); + MultivaluedMapImpl getParams = new MultivaluedMapImpl(); + 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()); + // Create a document WebResource documentResource = resource().path("/document"); documentResource.addFilter(new CookieAuthenticationFilter(file2AuthenticationToken)); @@ -242,12 +252,12 @@ public class TestFileResource extends BaseJerseyTest { // Get all files from a document fileResource = resource().path("/file/list"); fileResource.addFilter(new CookieAuthenticationFilter(file2AuthenticationToken)); - MultivaluedMapImpl getParams = new MultivaluedMapImpl(); + getParams = new MultivaluedMapImpl(); getParams.putSingle("id", document1Id); 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"); + files = json.getJSONArray("files"); Assert.assertEquals(1, files.length()); } } \ No newline at end of file