Search in sources :

Example 31 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project dropwizard by dropwizard.

the class FormsAppTest method canSubmitFormAndReceiveResponse.

@Test
void canSubmitFormAndReceiveResponse() throws IOException {
    config.setChunkedEncodingEnabled(false);
    final Client client = new JerseyClientBuilder(RULE.getEnvironment()).using(config).build("test client 1");
    try (final FormDataMultiPart fdmp = new FormDataMultiPart()) {
        final MultiPart mp = fdmp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("fileName").build(), "CONTENT"));
        final String url = String.format("http://localhost:%d/uploadFile", RULE.getLocalPort());
        final String response = client.target(url).register(MultiPartFeature.class).request().post(Entity.entity(mp, mp.getMediaType()), String.class);
        assertThat(response).isEqualTo("fileName:\nCONTENT");
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Client(javax.ws.rs.client.Client) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.jupiter.api.Test)

Example 32 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project dropwizard by dropwizard.

the class FormsAppTest method failOnNoChunkedEncoding.

/**
 * Test confirms that chunked encoding has to be disabled in order for
 * sending forms to work. Maybe someday this requirement will be relaxed and
 * this test can be updated for the new behavior. For more info, see issues
 * #1013 and #1094
 */
@Test
void failOnNoChunkedEncoding() throws IOException {
    final Client client = new JerseyClientBuilder(RULE.getEnvironment()).using(config).build("test client 2");
    try (final FormDataMultiPart fdmp = new FormDataMultiPart()) {
        final MultiPart mp = fdmp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("fileName").build(), "CONTENT"));
        final String url = String.format("http://localhost:%d/uploadFile", RULE.getLocalPort());
        final Response response = client.target(url).register(MultiPartFeature.class).request().post(Entity.entity(mp, mp.getMediaType()));
        assertThat(response.getStatus()).isEqualTo(400);
        assertThat(response.readEntity(ErrorMessage.class)).isEqualTo(new ErrorMessage(400, "HTTP 400 Bad Request"));
    }
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Client(javax.ws.rs.client.Client) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.jupiter.api.Test)

Example 33 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project jersey by jersey.

the class MultiPartReaderClientSide method readMultiPart.

protected MultiPart readMultiPart(final Class<MultiPart> type, final Type genericType, final Annotation[] annotations, MediaType mediaType, final MultivaluedMap<String, String> headers, final InputStream stream) throws IOException, MIMEParsingException {
    mediaType = unquoteMediaTypeParameters(mediaType, "boundary");
    final MIMEMessage mimeMessage = new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);
    final boolean formData = MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE);
    final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();
    final MessageBodyWorkers workers = messageBodyWorkers.get();
    multiPart.setMessageBodyWorkers(workers);
    final MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders();
    for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
        final List<String> values = entry.getValue();
        for (final String value : values) {
            multiPartHeaders.add(entry.getKey(), value);
        }
    }
    final boolean fileNameFix;
    if (!formData) {
        multiPart.setMediaType(mediaType);
        fileNameFix = false;
    } else {
        // see if the User-Agent header corresponds to some version of MS Internet Explorer
        // if so, need to set fileNameFix to true to handle issue http://java.net/jira/browse/JERSEY-759
        final String userAgent = headers.getFirst(HttpHeaders.USER_AGENT);
        fileNameFix = userAgent != null && userAgent.contains(" MSIE ");
    }
    for (final MIMEPart mimePart : getMimeParts(mimeMessage)) {
        final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
        // Configure providers.
        bodyPart.setMessageBodyWorkers(workers);
        // Copy headers.
        for (final Header header : mimePart.getAllHeaders()) {
            bodyPart.getHeaders().add(header.getName(), header.getValue());
        }
        try {
            final String contentType = bodyPart.getHeaders().getFirst("Content-Type");
            if (contentType != null) {
                bodyPart.setMediaType(MediaType.valueOf(contentType));
            }
            bodyPart.getContentDisposition();
        } catch (final IllegalArgumentException ex) {
            throw new BadRequestException(ex);
        }
        // Copy data into a BodyPartEntity structure.
        bodyPart.setEntity(new BodyPartEntity(mimePart));
        // Add this BodyPart to our MultiPart.
        multiPart.getBodyParts().add(bodyPart);
    }
    return multiPart;
}
Also used : MessageBodyWorkers(org.glassfish.jersey.message.MessageBodyWorkers) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Header(org.jvnet.mimepull.Header) MIMEMessage(org.jvnet.mimepull.MIMEMessage) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MIMEPart(org.jvnet.mimepull.MIMEPart)

Example 34 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project jersey by jersey.

the class Jersey2421Test method testMultiPartFeatureOnClient.

/**
     * Test that multipart feature works on the client-side - custom connector checks presence of {@code boundary} parameter in
     * the {@code Content-Type} header (the header is added to the request in MBW).
     */
@Test
public void testMultiPartFeatureOnClient() throws Exception {
    final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector())).register(MultiPartFeature.class);
    final MultiPart entity = new FormDataMultiPart().bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("part").build(), "CONTENT"));
    final Response response = client.target("http://localhost").request().post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
    assertThat(response.getStatus(), is(200));
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 35 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart 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)

Aggregations

MultiPart (org.glassfish.jersey.media.multipart.MultiPart)63 WebTarget (javax.ws.rs.client.WebTarget)42 ProcessingException (javax.ws.rs.ProcessingException)28 BatfishException (org.batfish.common.BatfishException)28 JSONObject (org.codehaus.jettison.json.JSONObject)20 Test (org.junit.Test)16 Nullable (javax.annotation.Nullable)15 MediaType (javax.ws.rs.core.MediaType)15 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)11 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)11 Response (javax.ws.rs.core.Response)10 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)10 Client (javax.ws.rs.client.Client)7 StreamDataBodyPart (org.glassfish.jersey.media.multipart.file.StreamDataBodyPart)7 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)5 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)3 File (java.io.File)3 Path (javax.ws.rs.Path)3