Search in sources :

Example 6 with MultiPart

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

the class MultiPartReaderWriterTest method testTen.

/**
     * Zero length body part.
     */
@Test
public void testTen() {
    final WebTarget target = target().path("multipart/ten");
    final MultiPartBean bean = new MultiPartBean("myname", "myvalue");
    final MultiPart entity = new MultiPart().bodyPart(bean, new MediaType("x-application", "x-format")).bodyPart("", MediaType.APPLICATION_OCTET_STREAM_TYPE);
    final String response = target.request("text/plain").put(Entity.entity(entity, "multipart/mixed"), String.class);
    if (!response.startsWith("SUCCESS:")) {
        fail("Response is '" + response + "'");
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 7 with MultiPart

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

the class MultiPartReaderWriterTest method testTwo.

@Test
public void testTwo() {
    final WebTarget target = target().path("multipart/two");
    try {
        final MultiPart result = target.request("multipart/mixed").get(MultiPart.class);
        checkMediaType(new MediaType("multipart", "mixed"), result.getMediaType());
        assertEquals(2, result.getBodyParts().size());
        final BodyPart part1 = result.getBodyParts().get(0);
        checkMediaType(new MediaType("text", "plain"), part1.getMediaType());
        checkEntity("This is the first segment", (BodyPartEntity) part1.getEntity());
        final BodyPart part2 = result.getBodyParts().get(1);
        checkMediaType(new MediaType("text", "xml"), part2.getMediaType());
        checkEntity("<outer><inner>value</inner></outer>", (BodyPartEntity) part2.getEntity());
        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) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 8 with MultiPart

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

the class MultiPartResource method etag.

@GET
@Path("etag")
@Produces("multipart/mixed")
public Response etag() {
    MultiPart entity = new MultiPart();
    // Exercise manually adding part(s) to the bodyParts property
    BodyPart part = new BodyPart("This is the only segment", new MediaType("text", "plain"));
    part.getHeaders().add("ETag", "\"value\"");
    entity.getBodyParts().add(part);
    return Response.ok(entity).type("multipart/mixed").build();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with MultiPart

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

the class BonitaAPIUtil method uploadFile.

public UploadFileResponse uploadFile(ProcessDefinitionResponse processDefinition, FileInput file) throws Exception {
    WebTarget resource = webTarget.path("portal/resource/process/{processName}/{processVersion}/API/formFileUpload").resolveTemplate("processName", processDefinition.getName()).resolveTemplate("processVersion", processDefinition.getVersion());
    File tempFile = File.createTempFile("tempFile", ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(file.getContent());
    fos.close();
    final FileDataBodyPart filePart = new FileDataBodyPart("file", tempFile, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    final MultiPart multipart = new FormDataMultiPart().bodyPart(filePart);
    return resource.request().accept(MediaType.APPLICATION_JSON).post(entity(multipart, MediaType.MULTIPART_FORM_DATA), UploadFileResponse.class);
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FileOutputStream(java.io.FileOutputStream) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) File(java.io.File) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 10 with MultiPart

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

the class JerseyRestClient method postMultiPartStream.

/**
 * POST a multipart object streaming
 *
 * @param path       the path to access
 * @param name       the name of the param the endpoint is expecting
 * @param fileName   the name of the file
 * @param stream     the stream itself
 * @param returnType the type to return from the post
 * @return the response of type T
 */
public <T> T postMultiPartStream(String path, String name, String fileName, InputStream stream, Class<T> returnType) {
    WebTarget target = buildTarget(path, null);
    MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart(name, stream, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    multiPart.getBodyParts().add(streamDataBodyPart);
    MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
    contentType = Boundary.addBoundary(contentType);
    return target.request().post(Entity.entity(multiPart, contentType), returnType);
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget)

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