Search in sources :

Example 1 with FormDataBodyPart

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

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 2 with FormDataBodyPart

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

the class MultiPartWebAppTest method testFieldInjectedXmlJAXBPart.

@Test
public void testFieldInjectedXmlJAXBPart() {
    final WebTarget target = target().path("form-field-injected/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 3 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 4 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 5 with FormDataBodyPart

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

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)

Aggregations

FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)39 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)28 Response (javax.ws.rs.core.Response)19 Test (org.junit.Test)16 File (java.io.File)10 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)10 InputStream (java.io.InputStream)8 Map (java.util.Map)8 List (java.util.List)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 MediaType (javax.ws.rs.core.MediaType)6 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Produces (javax.ws.rs.Produces)5 WebTarget (javax.ws.rs.client.WebTarget)5 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)5 Path (javax.ws.rs.Path)4