Search in sources :

Example 6 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project jersey by jersey.

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)

Example 7 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartEncoderTest method testMultipartWriterForNewSubTypes.

@Test(description = "Test whether the body parts get correctly encoded for any new multipart sub type")
public void testMultipartWriterForNewSubTypes() {
    BStruct multipartEntity = Util.getMultipartEntity(result);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
    MultipartDataSource multipartDataSource = new MultipartDataSource(multipartEntity, multipartDataBoundary);
    multipartDataSource.serializeData(outputStream);
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    try {
        List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/new-sub-type; boundary=" + multipartDataBoundary, inputStream);
        Assert.assertEquals(mimeParts.size(), 4);
        BStruct bodyPart = Util.getEntityStruct(result);
        validateBodyPartContent(mimeParts, bodyPart);
    } catch (MimeTypeParseException e) {
        log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
    } catch (IOException e) {
        log.error("Error occurred while decoding binary part", e.getMessage());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 8 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartEncoderTest method testMultipartWriterForMixed.

@Test(description = "Test whether the body parts get correctly encoded for multipart/mixed")
public void testMultipartWriterForMixed() {
    BStruct multipartEntity = Util.getMultipartEntity(result);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
    MultipartDataSource multipartDataSource = new MultipartDataSource(multipartEntity, multipartDataBoundary);
    multipartDataSource.serializeData(outputStream);
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    try {
        List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + multipartDataBoundary, inputStream);
        Assert.assertEquals(mimeParts.size(), 4);
        BStruct bodyPart = Util.getEntityStruct(result);
        validateBodyPartContent(mimeParts, bodyPart);
    } catch (MimeTypeParseException e) {
        log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
    } catch (IOException e) {
        log.error("Error occurred while decoding binary part", e.getMessage());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 9 with MIMEPart

use of org.jvnet.mimepull.MIMEPart in project ballerina by ballerina-lang.

the class MultipartEncoderTest method testNestedPartContent.

/**
 * When nested parts have been properly encoded, decoding should work as it should.
 *
 * @param mimePart MIMEPart that contains nested parts
 * @throws MimeTypeParseException When an error occurs while parsing body content
 * @throws IOException            When an error occurs while validating body content
 */
private void testNestedPartContent(MIMEPart mimePart) throws MimeTypeParseException, IOException {
    List<MIMEPart> nestedParts = MultipartDecoder.decodeBodyParts(mimePart.getContentType(), mimePart.readOnce());
    Assert.assertEquals(nestedParts.size(), 4);
    BStruct ballerinaBodyPart = Util.getEntityStruct(result);
    validateBodyPartContent(nestedParts, ballerinaBodyPart);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MIMEPart(org.jvnet.mimepull.MIMEPart)

Aggregations

MIMEPart (org.jvnet.mimepull.MIMEPart)9 BStruct (org.ballerinalang.model.values.BStruct)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 MimeTypeParseException (javax.activation.MimeTypeParseException)5 Test (org.testng.annotations.Test)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MultipartDataSource (org.ballerinalang.mime.util.MultipartDataSource)3 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2 MIMEMessage (org.jvnet.mimepull.MIMEMessage)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)2 ParamsWithPayload (com.sun.enterprise.admin.remote.ParamsWithPayload)1 RestPayloadImpl (com.sun.enterprise.admin.remote.RestPayloadImpl)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1