Search in sources :

Example 6 with BodyPart

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);
    }
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Invocation(javax.ws.rs.client.Invocation) MediaType(javax.ws.rs.core.MediaType) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 7 with BodyPart

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);
    }
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 8 with BodyPart

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);
    }
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 9 with BodyPart

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();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 10 with BodyPart

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();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

BodyPart (org.glassfish.jersey.media.multipart.BodyPart)10 MediaType (javax.ws.rs.core.MediaType)9 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)8 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 Test (org.junit.Test)5 WebTarget (javax.ws.rs.client.WebTarget)4 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 BodyPartEntity (org.glassfish.jersey.media.multipart.BodyPartEntity)3 List (java.util.List)2 Map (java.util.Map)2 GET (javax.ws.rs.GET)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)2 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)2 BufferedWriter (java.io.BufferedWriter)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1