Search in sources :

Example 6 with FormDataBodyPart

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

the class Jersey2846ITCase method _test.

public void _test(final String path, final int status, final Object entity) throws Exception {
    final String tempDir = System.getProperty("java.io.tmpdir");
    // Get number of matching MIME*tmp files (the number should be the same at the end of the test).
    final int expectedTempFiles = matchingTempFiles(tempDir);
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), entity, MediaType.TEXT_PLAIN_TYPE);
    multipart.bodyPart(bodypart);
    final Response response = target().path(path).request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    // Get Response ...
    assertThat(response.getStatus(), is(status));
    // Wait a second to make sure the files don't exist.
    Thread.sleep(1000);
    // Make sure that the message and it's parts have been closed and temporary files deleted.
    assertThat("Temporary files were not deleted", matchingTempFiles(tempDir), is(expectedTempFiles));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart)

Example 7 with FormDataBodyPart

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

the class Jersey1667ITCase method testJersey1667Fix.

/**
     * Reproducer method for JERSEY-1667.
     */
@Test
public void testJersey1667Fix() {
    final WebTarget target = target().path("part-file-name");
    char[] chars = new char[10 * 1024];
    Arrays.fill(chars, 'a');
    final String body = new String(chars);
    final FormDataMultiPart multiPart = new FormDataMultiPart();
    final FormDataBodyPart bodyPart = new FormDataBodyPart(FormDataContentDisposition.name("part").fileName("file").build(), body);
    multiPart.bodyPart(bodyPart);
    final Response response = target.request().post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
    assertThat(response.getStatus(), equalTo(500));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 8 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart 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 9 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart 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 10 with FormDataBodyPart

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

the class MultiPartWebAppTest method testPart.

@Test
public void testPart() {
    final WebTarget target = target().path("form/part");
    final FormDataMultiPart mp = new FormDataMultiPart();
    final FormDataBodyPart p = new FormDataBodyPart(FormDataContentDisposition.name("part").build(), "CONTENT");
    mp.bodyPart(p);
    final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
    assertEquals("CONTENT", s);
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)15 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)15 Test (org.junit.Test)13 Response (javax.ws.rs.core.Response)9 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 WebTarget (javax.ws.rs.client.WebTarget)5 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)4 File (java.io.File)3 Client (javax.ws.rs.client.Client)3 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)2 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BadRequestException (javax.ws.rs.BadRequestException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Verifications (mockit.Verifications)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 ClientResponse (org.glassfish.jersey.client.ClientResponse)1 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)1