Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions jaxrs/fileupload/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.javaee7.jaxrs</groupId>
<artifactId>jaxrs-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.javaee7.jaxrs</groupId>
<artifactId>fileupload</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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<File>() {

@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);
}

}
1 change: 1 addition & 0 deletions jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>beanvalidation</module>
<module>client-negotiation</module>
<module>dynamicfilter</module>
<module>fileupload</module>
<module>filter</module>
<module>filter-interceptor</module>
<module>interceptor</module>
Expand Down