Skip to content

Commit 489a5ae

Browse files
committed
Merge pull request #225 from xcoulon/jaxrs-fileupload
JAX-RS FileUpload Sample Project
2 parents 34233c3 + 2a41f17 commit 489a5ae

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

jaxrs/fileupload/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jaxrs</groupId>
6+
<artifactId>jaxrs-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.jaxrs</groupId>
12+
<artifactId>fileupload</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.jaxrs.fileupload;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* @author Arun Gupta
8+
*/
9+
@ApplicationPath("webresources")
10+
public class MyApplication extends Application {
11+
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.javaee7.jaxrs.fileupload;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.io.Reader;
9+
10+
import javax.ws.rs.Consumes;
11+
import javax.ws.rs.POST;
12+
import javax.ws.rs.Path;
13+
import javax.ws.rs.Produces;
14+
import javax.ws.rs.core.MediaType;
15+
import javax.ws.rs.core.Response;
16+
17+
/**
18+
* @author Xavier Coulon
19+
*/
20+
@Path("/endpoint")
21+
public class MyResource {
22+
23+
@POST
24+
@Path("/upload")
25+
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
26+
@Produces(MediaType.TEXT_PLAIN)
27+
public Response postOctetStream(InputStream content) {
28+
try (Reader reader = new InputStreamReader(content)) {
29+
int totalsize = 0;
30+
int count = 0;
31+
final char[] buffer = new char[256];
32+
while((count = reader.read(buffer)) != -1) {
33+
totalsize += count;
34+
}
35+
return Response.ok(totalsize).build();
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
return Response.serverError().build();
39+
}
40+
}
41+
42+
@POST
43+
@Path("/upload2")
44+
@Consumes({MediaType.APPLICATION_OCTET_STREAM, "image/png"})
45+
@Produces(MediaType.TEXT_PLAIN)
46+
public Response postImageFile(File file) {
47+
try (Reader reader = new FileReader(file)) {
48+
int totalsize = 0;
49+
int count = 0;
50+
final char[] buffer = new char[256];
51+
while((count = reader.read(buffer)) != -1) {
52+
totalsize += count;
53+
}
54+
return Response.ok(totalsize).build();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
return Response.serverError().build();
58+
}
59+
}
60+
61+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.javaee7.jaxrs.fileupload;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.net.MalformedURLException;
9+
import java.net.URI;
10+
import java.net.URL;
11+
12+
import javax.ws.rs.client.Client;
13+
import javax.ws.rs.client.ClientBuilder;
14+
import javax.ws.rs.client.Entity;
15+
import javax.ws.rs.client.WebTarget;
16+
import javax.ws.rs.core.MediaType;
17+
import javax.ws.rs.core.Response;
18+
import javax.ws.rs.core.Response.Status;
19+
20+
import org.assertj.core.api.Condition;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.arquillian.test.api.ArquillianResource;
24+
import org.jboss.shrinkwrap.api.ShrinkWrap;
25+
import org.jboss.shrinkwrap.api.spec.WebArchive;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
31+
/**
32+
* @author Arun Gupta
33+
* @author Xavier Coulon
34+
*/
35+
@RunWith(Arquillian.class)
36+
public class MyResourceTest {
37+
38+
@Deployment(testable = false)
39+
public static WebArchive createDeployment() {
40+
return ShrinkWrap.create(WebArchive.class).addClasses(MyApplication.class, MyResource.class);
41+
}
42+
43+
private static WebTarget target;
44+
45+
private static File tempFile;
46+
@ArquillianResource
47+
private URL base;
48+
49+
@BeforeClass
50+
public static void generateSampleFile() throws IOException {
51+
tempFile = File.createTempFile("javaee7samples", ".png");
52+
// fill the file with 1KB of content
53+
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
54+
for (int i = 0; i < 1000; i++) {
55+
outputStream.write(0);
56+
}
57+
}
58+
assertThat(tempFile).canRead().has(new Condition<File>() {
59+
60+
@Override
61+
public boolean matches(File tempFile) {
62+
return tempFile.length() == 1000;
63+
}
64+
});
65+
}
66+
67+
@Before
68+
public void setUpClass() throws MalformedURLException {
69+
Client client = ClientBuilder.newClient();
70+
target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm()));
71+
}
72+
73+
@Test
74+
public void shouldPostOctetStreamContentAsInputStream() {
75+
// when
76+
Long uploadedFileSize = target.path("/upload").request()
77+
.post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class);
78+
// then
79+
assertThat(uploadedFileSize).isEqualTo(1000);
80+
}
81+
82+
@Test
83+
public void shouldNotPostImagePngContentAsInputStream() {
84+
// when
85+
final Response response = target.path("/upload").request().post(Entity.entity(tempFile, "image/png"));
86+
// then
87+
assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode());
88+
}
89+
90+
@Test
91+
public void shouldPostOctetStreamContentAsFile() {
92+
// when
93+
Long uploadedFileSize = target.path("/upload2").request()
94+
.post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class);
95+
// then
96+
assertThat(uploadedFileSize).isEqualTo(1000);
97+
}
98+
99+
@Test
100+
public void shouldPostImagePngContentAsFile() {
101+
// when
102+
Long uploadedFileSize = target.path("/upload2").request()
103+
.post(Entity.entity(tempFile, "image/png"), Long.class);
104+
// then
105+
assertThat(uploadedFileSize).isEqualTo(1000);
106+
}
107+
108+
}

jaxrs/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<module>beanvalidation</module>
2222
<module>client-negotiation</module>
2323
<module>dynamicfilter</module>
24+
<module>fileupload</module>
2425
<module>filter</module>
2526
<module>filter-interceptor</module>
2627
<module>interceptor</module>

0 commit comments

Comments
 (0)