From 2a41f17dc82fc5181eba5498862f721492cb9882 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Wed, 28 May 2014 21:24:18 +0200 Subject: [PATCH] JAX-RS FileUpload Sample Project --- jaxrs/fileupload/pom.xml | 15 +++ .../jaxrs/fileupload/MyApplication.java | 12 ++ .../javaee7/jaxrs/fileupload/MyResource.java | 61 ++++++++++ .../jaxrs/fileupload/MyResourceTest.java | 108 ++++++++++++++++++ jaxrs/pom.xml | 1 + 5 files changed, 197 insertions(+) create mode 100644 jaxrs/fileupload/pom.xml create mode 100644 jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyApplication.java create mode 100644 jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyResource.java create mode 100644 jaxrs/fileupload/src/test/java/org/javaee7/jaxrs/fileupload/MyResourceTest.java diff --git a/jaxrs/fileupload/pom.xml b/jaxrs/fileupload/pom.xml new file mode 100644 index 000000000..d8e8e9967 --- /dev/null +++ b/jaxrs/fileupload/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + + org.javaee7.jaxrs + jaxrs-samples + 1.0-SNAPSHOT + ../pom.xml + + + org.javaee7.jaxrs + fileupload + 1.0-SNAPSHOT + war + diff --git a/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyApplication.java b/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyApplication.java new file mode 100644 index 000000000..cfe711afb --- /dev/null +++ b/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyApplication.java @@ -0,0 +1,12 @@ +package org.javaee7.jaxrs.fileupload; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +/** + * @author Arun Gupta + */ +@ApplicationPath("webresources") +public class MyApplication extends Application { + +} diff --git a/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyResource.java b/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyResource.java new file mode 100644 index 000000000..4959352f1 --- /dev/null +++ b/jaxrs/fileupload/src/main/java/org/javaee7/jaxrs/fileupload/MyResource.java @@ -0,0 +1,61 @@ +package org.javaee7.jaxrs.fileupload; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +/** + * @author Xavier Coulon + */ +@Path("/endpoint") +public class MyResource { + + @POST + @Path("/upload") + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + @Produces(MediaType.TEXT_PLAIN) + public Response postOctetStream(InputStream content) { + try (Reader reader = new InputStreamReader(content)) { + int totalsize = 0; + int count = 0; + final char[] buffer = new char[256]; + while((count = reader.read(buffer)) != -1) { + totalsize += count; + } + return Response.ok(totalsize).build(); + } catch (IOException e) { + e.printStackTrace(); + return Response.serverError().build(); + } + } + + @POST + @Path("/upload2") + @Consumes({MediaType.APPLICATION_OCTET_STREAM, "image/png"}) + @Produces(MediaType.TEXT_PLAIN) + public Response postImageFile(File file) { + try (Reader reader = new FileReader(file)) { + int totalsize = 0; + int count = 0; + final char[] buffer = new char[256]; + while((count = reader.read(buffer)) != -1) { + totalsize += count; + } + return Response.ok(totalsize).build(); + } catch (IOException e) { + e.printStackTrace(); + return Response.serverError().build(); + } + } + +} diff --git a/jaxrs/fileupload/src/test/java/org/javaee7/jaxrs/fileupload/MyResourceTest.java b/jaxrs/fileupload/src/test/java/org/javaee7/jaxrs/fileupload/MyResourceTest.java new file mode 100644 index 000000000..59790b4a0 --- /dev/null +++ b/jaxrs/fileupload/src/test/java/org/javaee7/jaxrs/fileupload/MyResourceTest.java @@ -0,0 +1,108 @@ +package org.javaee7.jaxrs.fileupload; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.assertj.core.api.Condition; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Arun Gupta + * @author Xavier Coulon + */ +@RunWith(Arquillian.class) +public class MyResourceTest { + + @Deployment(testable = false) + public static WebArchive createDeployment() { + return ShrinkWrap.create(WebArchive.class).addClasses(MyApplication.class, MyResource.class); + } + + private static WebTarget target; + + private static File tempFile; + @ArquillianResource + private URL base; + + @BeforeClass + public static void generateSampleFile() throws IOException { + tempFile = File.createTempFile("javaee7samples", ".png"); + // fill the file with 1KB of content + try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { + for (int i = 0; i < 1000; i++) { + outputStream.write(0); + } + } + assertThat(tempFile).canRead().has(new Condition() { + + @Override + public boolean matches(File tempFile) { + return tempFile.length() == 1000; + } + }); + } + + @Before + public void setUpClass() throws MalformedURLException { + Client client = ClientBuilder.newClient(); + target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm())); + } + + @Test + public void shouldPostOctetStreamContentAsInputStream() { + // when + Long uploadedFileSize = target.path("/upload").request() + .post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class); + // then + assertThat(uploadedFileSize).isEqualTo(1000); + } + + @Test + public void shouldNotPostImagePngContentAsInputStream() { + // when + final Response response = target.path("/upload").request().post(Entity.entity(tempFile, "image/png")); + // then + assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()); + } + + @Test + public void shouldPostOctetStreamContentAsFile() { + // when + Long uploadedFileSize = target.path("/upload2").request() + .post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class); + // then + assertThat(uploadedFileSize).isEqualTo(1000); + } + + @Test + public void shouldPostImagePngContentAsFile() { + // when + Long uploadedFileSize = target.path("/upload2").request() + .post(Entity.entity(tempFile, "image/png"), Long.class); + // then + assertThat(uploadedFileSize).isEqualTo(1000); + } + +} diff --git a/jaxrs/pom.xml b/jaxrs/pom.xml index 90e7e5acc..87e9b57f8 100644 --- a/jaxrs/pom.xml +++ b/jaxrs/pom.xml @@ -21,6 +21,7 @@ beanvalidation client-negotiation dynamicfilter + fileupload filter filter-interceptor interceptor