Search in sources :

Example 41 with MultiPart

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

the class MultiPartReaderWriterTest method checkEleven.

private void checkEleven(final String seed, final int multiplier) throws Exception {
    final StringBuilder sb = new StringBuilder(seed.length() * multiplier);
    for (int i = 0; i < multiplier; i++) {
        sb.append(seed);
    }
    final String expected = sb.toString();
    final WebTarget target = target().path("multipart/eleven");
    final MultiPart entity = new MultiPart().bodyPart(expected, MediaType.TEXT_PLAIN_TYPE);
    final MultiPart response = target.request("multipart/mixed").put(Entity.entity(entity, "multipart/mixed"), MultiPart.class);
    final String actual = response.getBodyParts().get(0).getEntityAs(String.class);
    assertEquals("Length for multiplier " + multiplier, expected.length(), actual.length());
    assertEquals("Content for multiplier " + multiplier, expected, actual);
    response.cleanup();
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) WebTarget(javax.ws.rs.client.WebTarget)

Example 42 with MultiPart

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

the class MultiPartResource method one.

@Path("one")
@GET
@Produces("multipart/mixed")
public Response one() {
    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"));
    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 43 with MultiPart

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

the class MultiPartResource method eleven.

// Echo back a body part whose content may or may not exceed the size
// of the buffer threshold
@Path("eleven")
@PUT
@Consumes("multipart/mixed")
@Produces("multipart/mixed")
public Response eleven(MultiPart multiPart) throws IOException {
    BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity();
    StringBuilder sb = new StringBuilder();
    InputStream stream = bpe.getInputStream();
    InputStreamReader reader = new InputStreamReader(stream);
    char[] buffer = new char[2048];
    while (true) {
        int n = reader.read(buffer);
        if (n < 0) {
            break;
        }
        sb.append(buffer, 0, n);
    }
    return Response.ok(new MultiPart().bodyPart(sb.toString(), MediaType.TEXT_PLAIN_TYPE)).type(new MediaType("multipart", "mixed")).build();
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 44 with MultiPart

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

the class AbstractNiFiTemplatesRestClient method create.

@Nonnull
@Override
public TemplateDTO create(@Nullable final String name, @Nonnull final String xml) {
    // Build template body part
    final FormDataBodyPart templatePart = new FormDataBodyPart("template", xml, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    FormDataContentDisposition.FormDataContentDispositionBuilder disposition = FormDataContentDisposition.name(templatePart.getName());
    disposition.fileName((name == null) ? "import_template_" + System.currentTimeMillis() : name);
    templatePart.setFormDataContentDisposition(disposition.build());
    // Combine parts
    MultiPart multiPart = new MultiPart();
    multiPart.bodyPart(templatePart);
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
    // Upload template
    return upload(multiPart);
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) Nonnull(javax.annotation.Nonnull)

Example 45 with MultiPart

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

the class RestIntegrationTest method getMultiPart.

private MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) {
    MultiPart multiPart = new MultiPart();
    BodyPart filePart = new FileDataBodyPart(resourceToTest.fileNameHeader, resourceToTest.fileToUpload);
    BodyPart entityPart = new FormDataBodyPart(resourceToTest.entityNameHeader, entity, MediaType.APPLICATION_JSON_TYPE);
    multiPart.bodyPart(filePart).bodyPart(entityPart);
    return multiPart;
}
Also used : FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

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