Search in sources :

Example 26 with FormDataMultiPart

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

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 27 with FormDataMultiPart

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

the class FormDataMultiPartReaderWriterTest method tempFileDeletedAfterSuccessfulProcessing.

/**
     * JERSEY-2846 reproducer. Make sure that temporary file created by MIMEPull deleted after a successful request.
     */
@Test
public void tempFileDeletedAfterSuccessfulProcessing() 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("SuccessfulMethod").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 28 with FormDataMultiPart

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

the class MultipartTest method testSpecificListAsParameter.

/**
     * Test that injection of a list (specific type) works.
     */
@Test
public void testSpecificListAsParameter() throws Exception {
    final MyObject object = new MyObject("object");
    final List<MyObject> list = Arrays.asList(new MyObject("list1"), new MyObject("list2"));
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("object").fileName("object").build(), object, MediaType.APPLICATION_JSON_TYPE));
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("list").fileName("list").build(), list, MediaType.APPLICATION_JSON_TYPE));
    final Response response = target("listAsParameter").request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE));
    assertThat(response.readEntity(String.class), is("object_list1_list2"));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 29 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project ice by JBEI.

the class IceRestClient method postSequenceFile.

public Response postSequenceFile(String url, String recordId, EntryType entryType, String sequence) {
    try {
        WebTarget target = client.target("https://" + url).path("/rest/file/sequence");
        Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
        final FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.field("file", IOUtils.toInputStream(sequence, "UTF-8"), MediaType.TEXT_PLAIN_TYPE);
        multiPart.field("entryRecordId", recordId);
        multiPart.field("entryType", entryType.name());
        return invocationBuilder.post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
    } catch (Exception e) {
        Logger.error(e);
        return null;
    }
}
Also used : FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart)

Aggregations

FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)29 Test (org.junit.Test)25 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)15 Response (javax.ws.rs.core.Response)10 Invocation (javax.ws.rs.client.Invocation)9 WebTarget (javax.ws.rs.client.WebTarget)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 MediaType (javax.ws.rs.core.MediaType)6 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)5 File (java.io.File)4 Client (javax.ws.rs.client.Client)3 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)2 HashMap (java.util.HashMap)2 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 FileOutputStream (java.io.FileOutputStream)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