Search in sources :

Example 21 with FormDataBodyPart

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

the class FormDataMultiPartReaderWriterTest method testFileSize.

/**
 * JERSEY-2862 reproducer. Make sure that mimepull is able to move it's temporary file to the one created by Jersey.
 * Reproducible only on Windows. Entity size has to be bigger than 8192 to make sure mimepull creates temporary file.
 */
@Test
public void testFileSize() throws Exception {
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final byte[] content = new byte[2 * 8192];
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), content, MediaType.TEXT_PLAIN_TYPE);
    multipart.bodyPart(bodypart);
    final Response response = target().path("FileResource").path("FileSize").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    assertThat("Temporary file has wrong size.", response.readEntity(int.class), is(content.length));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Test(org.junit.Test)

Example 22 with FormDataBodyPart

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

the class FormDataMultiPartReaderWriterTest method tempFileDeletedAfterExceptionInMethod.

/**
 * JERSEY-2846 reproducer. Make sure that temporary file created by MIMEPull deleted after an unsuccessful request.
 */
@Test
public void tempFileDeletedAfterExceptionInMethod() throws Exception {
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), "CONTENT");
    multipart.bodyPart(bodypart);
    final Response response = target().path("FileResource").path("ExceptionInMethod").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    // Make sure that the temp file has been removed.
    final String pathname = response.readEntity(String.class);
    // Wait a second to make sure the file doesn't exist.
    Thread.sleep(1000);
    assertThat("Temporary file, " + pathname + ", on the server has not been removed", new File(pathname).exists(), is(false));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) File(java.io.File) Test(org.junit.Test)

Example 23 with FormDataBodyPart

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

the class MultiPartWebAppTest method testPartWithFileName.

@Test
public void testPartWithFileName() {
    final WebTarget target = target().path("form/part-file-name");
    final FormDataMultiPart mp = new FormDataMultiPart();
    final FormDataBodyPart p = new FormDataBodyPart(FormDataContentDisposition.name("part").fileName("file").build(), "CONTENT");
    mp.bodyPart(p);
    final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
    assertEquals("CONTENT:file", 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)

Example 24 with FormDataBodyPart

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

the class MultiPartWebAppTest method testXmlJAXBPart.

@Test
public void testXmlJAXBPart() {
    final WebTarget target = target().path("form/xml-jaxb-part");
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("bean").fileName("bean").build(), new Bean("BEAN"), MediaType.APPLICATION_XML_TYPE));
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("string").fileName("string").build(), "STRING"));
    final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
    assertEquals("STRING:string,BEAN:bean", 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)

Example 25 with FormDataBodyPart

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

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)

Aggregations

FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)109 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)66 File (java.io.File)35 WebTarget (javax.ws.rs.client.WebTarget)33 Test (org.junit.Test)29 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)28 Response (javax.ws.rs.core.Response)27 InputStream (java.io.InputStream)17 JerseyTest (org.glassfish.jersey.test.JerseyTest)17 Gson (com.google.gson.Gson)16 CompletableFuture (java.util.concurrent.CompletableFuture)16 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)15 IOException (java.io.IOException)14 Map (java.util.Map)14 FormDataContentDisposition (org.glassfish.jersey.media.multipart.FormDataContentDisposition)14 HashMap (java.util.HashMap)12 List (java.util.List)12 Consumes (javax.ws.rs.Consumes)12 POST (javax.ws.rs.POST)11 Form (javax.ws.rs.core.Form)11