From c9fdb6def5d733080f5c80638f8a9fe936bfd087 Mon Sep 17 00:00:00 2001 From: jendib Date: Sun, 28 Jul 2013 01:55:32 +0200 Subject: [PATCH] Pass file stream directly to GET /file/:id/data --- .../docs/rest/resource/FileResource.java | 18 +++---------- .../sismics/docs/rest/TestFileResource.java | 25 ++++++++++++------- 2 files changed, 19 insertions(+), 24 deletions(-) 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 e75f4683..02366947 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 @@ -1,11 +1,8 @@ package com.sismics.docs.rest.resource; import java.io.BufferedInputStream; -import java.io.FileInputStream; import java.io.FilenameFilter; -import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.text.MessageFormat; @@ -23,12 +20,10 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.StreamingOutput; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import com.google.common.io.ByteStreams; import com.sismics.docs.core.dao.jpa.DocumentDao; import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.model.jpa.Document; @@ -57,9 +52,10 @@ public class FileResource extends BaseResource { * @throws JSONException */ @GET + @Path("{id: [a-z0-9\\-]+}") @Produces(MediaType.APPLICATION_JSON) public Response get( - @QueryParam("id") String id) throws JSONException { + @PathParam("id") String id) throws JSONException { if (!authenticate()) { throw new ForbiddenClientException(); } @@ -241,15 +237,7 @@ public class FileResource extends BaseResource { }); final java.io.File storageFile = matchingFiles[0]; - // Stream the file to the response - StreamingOutput stream = new StreamingOutput() { - @Override - public void write(OutputStream os) throws IOException { - ByteStreams.copy(new FileInputStream(storageFile), os); - } - }; - return Response.ok(stream) - .header("Content-Disposition", MessageFormat.format("attachment; filename=\"{0}\"", storageFile.getName())) + return Response.ok(storageFile) .build(); } } 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 10b25a1e..2d943c0c 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 @@ -8,10 +8,10 @@ import javax.ws.rs.core.MediaType; import junit.framework.Assert; import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; 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; @@ -30,10 +30,10 @@ public class TestFileResource extends BaseJerseyTest { /** * Test the document resource. * - * @throws JSONException + * @throws Exception */ @Test - public void testFileResource() throws JSONException { + public void testFileResource() throws Exception { // Login admin String adminAuthenticationToken = clientUtil.login("admin", "admin", false); @@ -63,21 +63,28 @@ public class TestFileResource extends BaseJerseyTest { json = response.getEntity(JSONObject.class); String file1Id = json.getString("id"); - // Get a file - fileResource = resource().path("/file"); + // Get the file + fileResource = resource().path("/file/" + file1Id); fileResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken)); - MultivaluedMapImpl getParams = new MultivaluedMapImpl(); - getParams.putSingle("id", file1Id); - response = fileResource.queryParams(getParams).get(ClientResponse.class); + 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(adminAuthenticationToken)); + response = fileResource.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 all files from a document fileResource = resource().path("/file/list"); fileResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken)); - getParams = new MultivaluedMapImpl(); + MultivaluedMapImpl getParams = new MultivaluedMapImpl(); getParams.putSingle("id", document1Id); response = fileResource.queryParams(getParams).get(ClientResponse.class); json = response.getEntity(JSONObject.class);