use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class FormDataMultiPartReaderWriterTest method testProducesFormDataUsingMultiPart.
// Test a response of type "multipart/form-data". The example comes from
// Section 6 of RFC 1867.
@Test
public void testProducesFormDataUsingMultiPart() {
final Invocation.Builder request = target().path("ProducesFormDataUsingMultiPart").request("multipart/form-data");
try {
final MultiPart result = request.get(MultiPart.class);
checkMediaType(new MediaType("multipart", "form-data"), result.getMediaType());
assertEquals(2, result.getBodyParts().size());
final BodyPart part1 = result.getBodyParts().get(0);
checkMediaType(new MediaType("text", "plain"), part1.getMediaType());
checkEntity("Joe Blow\r\n", (BodyPartEntity) part1.getEntity());
final String value1 = part1.getHeaders().getFirst("Content-Disposition");
assertEquals("form-data; name=\"field1\"", value1);
final BodyPart part2 = result.getBodyParts().get(1);
checkMediaType(new MediaType("text", "plain"), part2.getMediaType());
checkEntity("... contents of file1.txt ...\r\n", (BodyPartEntity) part2.getEntity());
final String value2 = part2.getHeaders().getFirst("Content-Disposition");
assertEquals("form-data; name=\"pics\"; filename=\"file1.txt\"", value2);
result.getParameterizedHeaders();
result.cleanup();
} catch (final IOException | ParseException e) {
e.printStackTrace(System.out);
fail("Caught exception: " + e);
}
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartReaderWriterTest method testETag.
@Test
public void testETag() {
final WebTarget target = target().path("multipart/etag");
try {
final MultiPart result = target.request("multipart/mixed").get(MultiPart.class);
checkMediaType(new MediaType("multipart", "mixed"), result.getMediaType());
assertEquals(1, result.getBodyParts().size());
final BodyPart part = result.getBodyParts().get(0);
checkMediaType(new MediaType("text", "plain"), part.getMediaType());
checkEntity("This is the only segment", (BodyPartEntity) part.getEntity());
assertEquals("\"value\"", part.getHeaders().getFirst("ETag"));
result.getParameterizedHeaders();
result.cleanup();
} catch (final IOException | ParseException e) {
e.printStackTrace(System.out);
fail("Caught exception: " + e);
}
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartReaderWriterTest method testThree.
@Test
public void testThree() {
final WebTarget target = target().path("multipart/three");
try {
final MultiPart result = target.request("multipart/mixed").get(MultiPart.class);
checkMediaType(new MediaType("multipart", "mixed"), result.getMediaType());
assertEquals(2, result.getBodyParts().size());
final BodyPart part1 = result.getBodyParts().get(0);
checkMediaType(new MediaType("text", "plain"), part1.getMediaType());
checkEntity("This is the first segment", (BodyPartEntity) part1.getEntity());
final BodyPart part2 = result.getBodyParts().get(1);
checkMediaType(new MediaType("x-application", "x-format"), part2.getMediaType());
final MultiPartBean entity = part2.getEntityAs(MultiPartBean.class);
assertEquals("myname", entity.getName());
assertEquals("myvalue", entity.getValue());
result.getParameterizedHeaders();
result.cleanup();
} catch (final IOException | ParseException e) {
e.printStackTrace(System.out);
fail("Caught exception: " + e);
}
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartResource method four.
@Path("four")
@PUT
@Produces("text/plain")
public Response four(MultiPart multiPart) throws IOException {
if (!(multiPart.getBodyParts().size() == 2)) {
return Response.ok("FAILED: Number of body parts is " + multiPart.getBodyParts().size() + " instead of 2").build();
}
BodyPart part0 = multiPart.getBodyParts().get(0);
if (!(part0.getMediaType().equals(new MediaType("text", "plain")))) {
return Response.ok("FAILED: First media type is " + part0.getMediaType()).build();
}
BodyPart part1 = multiPart.getBodyParts().get(1);
if (!(part1.getMediaType().equals(new MediaType("x-application", "x-format")))) {
return Response.ok("FAILED: Second media type is " + part1.getMediaType()).build();
}
BodyPartEntity bpe = (BodyPartEntity) part0.getEntity();
StringBuilder sb = new StringBuilder();
InputStream stream = bpe.getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
char[] buffer = new char[2048];
while (true) {
int n = reader.read(buffer);
if (n < 0) {
break;
}
sb.append(buffer, 0, n);
}
if (!sb.toString().equals("This is the first segment")) {
return Response.ok("FAILED: First part name = " + sb.toString()).build();
}
MultiPartBean bean = part1.getEntityAs(MultiPartBean.class);
if (!bean.getName().equals("myname")) {
return Response.ok("FAILED: Second part name = " + bean.getName()).build();
}
if (!bean.getValue().equals("myvalue")) {
return Response.ok("FAILED: Second part value = " + bean.getValue()).build();
}
return Response.ok("SUCCESS: All tests passed").build();
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartResource method one.
@Path("one")
@GET
@Produces("multipart/mixed")
public Response one() {
MultiPart entity = new MultiPart();
// Exercise manually adding part(s) to the bodyParts property
BodyPart part = new BodyPart("This is the only segment", new MediaType("text", "plain"));
entity.getBodyParts().add(part);
return Response.ok(entity).type("multipart/mixed").build();
}
Aggregations