Search in sources :

Example 21 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createBytesBody.

private static final MimeBodyPart createBytesBody() {
    try {
        //Bytes body. A body that uses a content type different then just text/plain.
        final byte[] body = new byte[20];
        for (int i = 0; i < body.length; i++) {
            body[i] = (byte) i;
        }
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(BINARY_CONTENT_TYPE);
        dataPart.setContent(body, contentType.getBaseType());
        dataPart.setHeader(HEADER_CONTENT_TYPE, contentType.toString());
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Example 22 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createPurelyEmptyBody.

private static final MimeBodyPart createPurelyEmptyBody() {
    try {
        //Purely empty body. This has no body or headers.
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
        //Mail requires content so we do a bit of a hack here.
        dataPart.setContent("", contentType.getBaseType());
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Example 23 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createLargeDataSource.

private static final MimeBodyPart createLargeDataSource() {
    try {
        //Large body. Something bigger then the size of the boundary with folded headers.
        final String body = "Has at possim tritani laoreet, vis te meis verear. Vel no vero quando oblique, eu blandit placerat nec, vide facilisi recusabo nec te. Veri labitur sensibus eum id. Quo omnis " + "putant erroribus ad, nonumes copiosae percipit in qui, id cibo meis clita pri. An brute mundi quaerendum duo, eu aliquip facilisis sea, eruditi invidunt dissentiunt eos ea.";
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
        dataPart.setContent(body, contentType.getBaseType());
        //Modify the content type header to use folding. We will also use multiple headers that use folding to verify
        //the integrity of the reader. Note that the Content-Type header uses parameters which are key/value pairs
        //separated by '='. Note that we do not use two consecutive CRLFs anywhere since our implementation
        //does not support this.
        final StringBuffer contentTypeBuffer = new StringBuffer(contentType.toString());
        contentTypeBuffer.append(";\r\n\t\t\t");
        contentTypeBuffer.append("parameter1= value1");
        contentTypeBuffer.append(";\r\n   \t");
        contentTypeBuffer.append("parameter2= value2");
        //This is a custom header which is folded. It does not use parameters so it's values are separated by commas.
        final StringBuffer customHeaderBuffer = new StringBuffer();
        customHeaderBuffer.append("CustomValue1");
        customHeaderBuffer.append(",\r\n\t  \t");
        customHeaderBuffer.append("CustomValue2");
        customHeaderBuffer.append(",\r\n ");
        customHeaderBuffer.append("CustomValue3");
        dataPart.setHeader(HEADER_CONTENT_TYPE, contentTypeBuffer.toString());
        dataPart.setHeader("AnotherCustomHeader", "AnotherCustomValue");
        dataPart.setHeader("FoldedHeader", customHeaderBuffer.toString());
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Example 24 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createHeaderLessBody.

private static final MimeBodyPart createHeaderLessBody() {
    try {
        //Header-less body. This has a body but no headers.
        final String body = "A body without any headers.";
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
        dataPart.setContent(body, contentType.getBaseType());
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Example 25 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class QueryTunnelUtil method doDecode.

private static RestRequest doDecode(final RestRequest request, RequestContext requestContext) throws MessagingException, IOException, URISyntaxException {
    String query = null;
    byte[] entity = new byte[0];
    // All encoded requests must have a content type. If the header is missing, ContentType throws an exception
    ContentType contentType = new ContentType(request.getHeader(HEADER_CONTENT_TYPE));
    RestRequestBuilder requestBuilder = request.builder();
    // Get copy of headers and remove the override
    Map<String, String> h = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    h.putAll(request.getHeaders());
    h.remove(HEADER_METHOD_OVERRIDE);
    // Simple case, just extract query params from entity, append to query, and clear entity
    if (contentType.getBaseType().equals(FORM_URL_ENCODED)) {
        query = request.getEntity().asString(Data.UTF_8_CHARSET);
        h.remove(HEADER_CONTENT_TYPE);
        h.remove(CONTENT_LENGTH);
    } else if (contentType.getBaseType().equals(MULTIPART)) {
        // Clear these in case there is no body part
        h.remove(HEADER_CONTENT_TYPE);
        h.remove(CONTENT_LENGTH);
        MimeMultipart multi = new MimeMultipart(new DataSource() {

            @Override
            public InputStream getInputStream() throws IOException {
                return request.getEntity().asInputStream();
            }

            @Override
            public OutputStream getOutputStream() throws IOException {
                return null;
            }

            @Override
            public String getContentType() {
                return request.getHeader(HEADER_CONTENT_TYPE);
            }

            @Override
            public String getName() {
                return null;
            }
        });
        for (int i = 0; i < multi.getCount(); i++) {
            MimeBodyPart part = (MimeBodyPart) multi.getBodyPart(i);
            if (part.isMimeType(FORM_URL_ENCODED) && query == null) {
                // Assume the first segment we come to that is urlencoded is the tunneled query params
                query = IOUtil.toString((InputStream) part.getContent(), UTF8);
            } else if (entity.length <= 0) {
                // Assume the first non-urlencoded content we come to is the intended entity.
                Object content = part.getContent();
                if (content instanceof MimeMultipart) {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ((MimeMultipart) content).writeTo(os);
                    entity = os.toByteArray();
                } else {
                    entity = IOUtil.toByteArray((InputStream) content);
                }
                h.put(CONTENT_LENGTH, Integer.toString(entity.length));
                h.put(HEADER_CONTENT_TYPE, part.getContentType());
            } else {
                // If it's not form-urlencoded and we've already found another section,
                // this has to be be an extra body section, which we have no way to handle.
                // Proceed with the request as if the 1st part we found was the expected body,
                // but log a warning in case some client is constructing a request that doesn't
                // follow the rules.
                String unexpectedContentType = part.getContentType();
                LOG.warn("Unexpected body part in X-HTTP-Method-Override request, type=" + unexpectedContentType);
            }
        }
    }
    // we have to check and append the original query correctly.
    if (query != null && query.length() > 0) {
        String separator = "&";
        String existingQuery = request.getURI().getRawQuery();
        if (existingQuery == null) {
            separator = "?";
        } else if (existingQuery.isEmpty()) {
            // This would mean someone has appended a "?" with no args to the url underneath us
            separator = "";
        }
        requestBuilder.setURI(new URI(request.getURI().toString() + separator + query));
    }
    requestBuilder.setEntity(entity);
    requestBuilder.setHeaders(h);
    requestBuilder.setMethod(request.getHeader(HEADER_METHOD_OVERRIDE));
    requestContext.putLocalAttr(R2Constants.IS_QUERY_TUNNELED, true);
    return requestBuilder.build();
}
Also used : ContentType(javax.mail.internet.ContentType) InputStream(java.io.InputStream) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TreeMap(java.util.TreeMap) URI(java.net.URI) DataSource(javax.activation.DataSource) MimeMultipart(javax.mail.internet.MimeMultipart) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

ContentType (javax.mail.internet.ContentType)40 IOException (java.io.IOException)17 MessagingException (javax.mail.MessagingException)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 ByteString (com.linkedin.data.ByteString)10 MimeMessage (javax.mail.internet.MimeMessage)9 MimeMultipart (javax.mail.internet.MimeMultipart)9 InputStream (java.io.InputStream)8 ParseException (javax.mail.internet.ParseException)8 MimePart (javax.mail.internet.MimePart)6 PackageException (com.axway.ats.action.objects.model.PackageException)5 DataHandler (javax.activation.DataHandler)5 BodyPart (javax.mail.BodyPart)5 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)4 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URI (java.net.URI)3