Example usage for javax.mail.internet MimeMultipart getContentType

List of usage examples for javax.mail.internet MimeMultipart getContentType

Introduction

In this page you can find the example usage for javax.mail.internet MimeMultipart getContentType.

Prototype

public synchronized String getContentType() 

Source Link

Document

Return the content-type of this Multipart.

Usage

From source file:com.ctriposs.r2.message.rest.QueryTunnelUtil.java

/**
 * @param request   a RestRequest object to be encoded as a tunneled POST
 * @param threshold the size of the query params above which the request will be encoded
 *
 * @return an encoded RestRequest/*from   ww w  . j  a  v a  2s.  c  o m*/
 */
public static RestRequest encode(final RestRequest request, int threshold)
        throws URISyntaxException, MessagingException, IOException {
    URI uri = request.getURI();

    // Check to see if we should tunnel this request by testing the length of the query
    // if the query is NULL, we won't bother to encode.
    // 0 length is a special case that could occur with a url like http://www.foo.com?
    // which we don't want to encode, because we'll lose the "?" in the process
    // Otherwise only encode queries whose length is greater than or equal to the
    // threshold value.

    String query = uri.getRawQuery();

    if (query == null || query.length() == 0 || query.length() < threshold) {
        return request;
    }

    RestRequestBuilder requestBuilder = new RestRequestBuilder(request);

    // reconstruct URI without query
    uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
            uri.getFragment());

    // If there's no existing body, just pass the request as x-www-form-urlencoded
    ByteString entity = request.getEntity();
    if (entity == null || entity.length() == 0) {
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED);
        requestBuilder.setEntity(ByteString.copyString(query, Data.UTF_8_CHARSET));
    } else {
        // If we have a body, we must preserve it, so use multipart/mixed encoding

        MimeMultipart multi = createMultiPartEntity(entity, request.getHeader(HEADER_CONTENT_TYPE), query);
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, multi.getContentType());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        multi.writeTo(os);
        requestBuilder.setEntity(ByteString.copy(os.toByteArray()));
    }

    // Set the base uri, supply the original method in the override header, and change method to POST
    requestBuilder.setURI(uri);
    requestBuilder.setHeader(HEADER_METHOD_OVERRIDE, requestBuilder.getMethod());
    requestBuilder.setMethod(RestMethod.POST);

    return requestBuilder.build();
}

From source file:com.linkedin.r2.message.rest.QueryTunnelUtil.java

/**
 * @param request   a RestRequest object to be encoded as a tunneled POST
 * @param requestContext a RequestContext object associated with the request
 * @param threshold the size of the query params above which the request will be encoded
 *
 * @return an encoded RestRequest//w  ww.  ja  v  a  2s.com
 */
public static RestRequest encode(final RestRequest request, RequestContext requestContext, int threshold)
        throws URISyntaxException, MessagingException, IOException {
    URI uri = request.getURI();

    // Check to see if we should tunnel this request by testing the length of the query
    // if the query is NULL, we won't bother to encode.
    // 0 length is a special case that could occur with a url like http://www.foo.com?
    // which we don't want to encode, because we'll lose the "?" in the process
    // Otherwise only encode queries whose length is greater than or equal to the
    // threshold value.

    String query = uri.getRawQuery();

    boolean forceQueryTunnel = requestContext.getLocalAttr(R2Constants.FORCE_QUERY_TUNNEL) != null
            && (Boolean) requestContext.getLocalAttr(R2Constants.FORCE_QUERY_TUNNEL);

    if (query == null || query.length() == 0 || (query.length() < threshold && !forceQueryTunnel)) {
        return request;
    }

    RestRequestBuilder requestBuilder = new RestRequestBuilder(request);

    // reconstruct URI without query
    uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
            uri.getFragment());

    // If there's no existing body, just pass the request as x-www-form-urlencoded
    ByteString entity = request.getEntity();
    if (entity == null || entity.length() == 0) {
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED);
        requestBuilder.setEntity(ByteString.copyString(query, Data.UTF_8_CHARSET));
    } else {
        // If we have a body, we must preserve it, so use multipart/mixed encoding

        MimeMultipart multi = createMultiPartEntity(entity, request.getHeader(HEADER_CONTENT_TYPE), query);
        requestBuilder.setHeader(HEADER_CONTENT_TYPE, multi.getContentType());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        multi.writeTo(os);
        requestBuilder.setEntity(ByteString.copy(os.toByteArray()));
    }

    // Set the base uri, supply the original method in the override header, and change method to POST
    requestBuilder.setURI(uri);
    requestBuilder.setHeader(HEADER_METHOD_OVERRIDE, requestBuilder.getMethod());
    requestBuilder.setMethod(RestMethod.POST);

    return requestBuilder.build();
}

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.MultipartRequestEntity.java

public MultipartRequestEntity(MimeMultipart mp) {
    this.mp = mp;
    setContentType(/* ww w .  j a v  a 2s .c  o m*/
            mp.getContentType() + "; type=\"" + Constants.CTYPE_REQUEST_NOCHARSET + "\"; start=\"request\"");
}

From source file:edu.wisc.bnsemail.dao.SmtpBusinessEmailUpdateNotifier.java

@Override
public void notifyEmailUpdated(String oldAddress, String newAddress) {
    try {/*from   www. j  a va  2  s  .  co m*/
        //Create the message body
        final MimeBodyPart msg = new MimeBodyPart();
        msg.setContent(
                "Your Business Email Address has changed\n" + "\n" + "Old Email Address: " + oldAddress + "\n"
                        + "New Email Address: " + newAddress + "\n" + "\n"
                        + "If you have any questions, please contact your Human Resources department.",
                "text/plain");

        final MimeMessage message = this.javaMailSender.createMimeMessage();
        final Address[] recipients;
        if (StringUtils.isNotEmpty(oldAddress)) {
            recipients = new Address[] { new InternetAddress(oldAddress), new InternetAddress(newAddress) };
        } else {
            recipients = new Address[] { new InternetAddress(newAddress) };
        }
        message.setRecipients(RecipientType.TO, recipients);
        message.setFrom(new InternetAddress("payroll@ohr.wisc.edu"));
        message.setSubject("Business Email Address Change");

        // sign the message body
        if (this.smimeSignedGenerator != null) {
            final MimeMultipart mm = this.smimeSignedGenerator.generate(msg, "BC");
            message.setContent(mm, mm.getContentType());
        }
        // no signing keystore configured, send the message unsigned
        else {
            message.setContent(msg.getContent(), msg.getContentType());
        }

        message.saveChanges();

        this.javaMailSender.send(message);

        this.logger.info("Sent notification of email address change from {} to {}", oldAddress, newAddress);
    } catch (Exception e) {
        this.logger.error(
                "Failed to send notification email for change from " + oldAddress + " to " + newAddress, e);
    }
}

From source file:eu.peppol.as2.MdnMimeMessageInspector.java

/**
 * The multipart/report should contain both a text/plain part with textual information and
 * a message/disposition-notification part that should be examined for error/failure/warning.
 */// w  ww.ja  v  a  2s.  c om
public MimeMultipart getMultipartReport() {
    try {
        BodyPart bodyPart = getSignedMultiPart().getBodyPart(0);
        Object content = bodyPart.getContent();
        MimeMultipart multipartReport = (MimeMultipart) content;
        if (!multipartReport.getContentType().contains("multipart/report")) {
            throw new IllegalStateException(
                    "The first body part of the first part of the signed message is not a multipart/report");
        }
        return multipartReport;
    } catch (Exception e) {
        throw new IllegalStateException("Unable to retrieve the multipart/report : " + e.getMessage(), e);
    }
}

From source file:com.xebialabs.xlt.ci.server.XLTestServerImplTest.java

private void verifyUploadRequest(final RecordedRequest request) throws IOException, MessagingException {
    assertEquals(request.getRequestLine(), "POST /api/internal/import/testspecid HTTP/1.1");
    assertEquals(request.getHeader("accept"), "application/json; charset=utf-8");
    assertEquals(request.getHeader("authorization"), "Basic YWRtaW46YWRtaW4=");
    assertThat(request.getHeader("Content-Length"), is(nullValue()));
    assertThat(request.getHeader("Transfer-Encoding"), is("chunked"));
    assertThat(request.getChunkSizes().get(0), greaterThan(0));
    assertThat(request.getChunkSizes().size(), greaterThan(0));

    assertTrue(request.getBodySize() > 0);

    ByteArrayDataSource bads = new ByteArrayDataSource(request.getBody().inputStream(), "multipart/mixed");
    MimeMultipart mp = new MimeMultipart(bads);
    assertTrue(request.getBodySize() > 0);

    assertEquals(mp.getCount(), 2);/*from   www  .  ja  v  a2  s  .  c om*/
    assertEquals(mp.getContentType(), "multipart/mixed");

    // TODO could do additional checks on metadata content
    BodyPart bodyPart1 = mp.getBodyPart(0);
    assertEquals(bodyPart1.getContentType(), "application/json; charset=utf-8");

    BodyPart bodyPart2 = mp.getBodyPart(1);
    assertEquals(bodyPart2.getContentType(), "application/zip");
}

From source file:com.adaptris.util.text.mime.MultiPartOutput.java

private void writeViaTempfile(OutputStream out, File tempFile) throws MessagingException, IOException {
    try (FileOutputStream fileOut = new FileOutputStream(tempFile);
            CountingOutputStream counter = new CountingOutputStream(fileOut)) {
        MimeMultipart multipart = new MimeMultipart();
        mimeHeader.setHeader(HEADER_CONTENT_TYPE, multipart.getContentType());
        // Write the part out to the stream first.
        for (KeyedBodyPart kbp : parts) {
            multipart.addBodyPart(kbp.getData());
        }//from  ww w.  ja  va2 s .c  o  m
        multipart.writeTo(counter);
        counter.flush();
        mimeHeader.setHeader(HEADER_CONTENT_LENGTH, String.valueOf(counter.count()));
    }
    writeHeaders(mimeHeader, out);
    try (InputStream in = new FileInputStream(tempFile)) {
        IOUtils.copy(in, out);
    }
}

From source file:com.adaptris.util.text.mime.MultiPartOutput.java

private void inMemoryWrite(OutputStream out) throws MessagingException, IOException {
    MimeMultipart multipart = new MimeMultipart();
    ByteArrayOutputStream partOut = new ByteArrayOutputStream();
    mimeHeader.setHeader(HEADER_CONTENT_TYPE, multipart.getContentType());
    // Write the part out to the stream first.
    for (KeyedBodyPart kbp : parts) {
        multipart.addBodyPart(kbp.getData());
    }//from   w w w .  ja  va 2 s  .  c  om
    multipart.writeTo(partOut);
    mimeHeader.setHeader(HEADER_CONTENT_LENGTH, String.valueOf(partOut.size()));
    writeHeaders(mimeHeader, out);
    out.write(partOut.toByteArray());
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

MimeMultipart initMultiPartFormContent(MockHttpServletRequest request) throws Exception {
    MimeMultipart body = new MimeMultipart();
    request.setContentType(body.getContentType());

    return body;/*from w  w w. ja v  a2s.c  o  m*/
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

void createMultiPartFormContent(MockHttpServletRequest request, String contentDisposition, String contentType,
        byte[] content) throws Exception {
    MimeMultipart body = new MimeMultipart();
    request.setContentType(body.getContentType());
    InternetHeaders headers = new InternetHeaders();
    headers.setHeader("Content-Disposition", contentDisposition);
    headers.setHeader("Content-Type", contentType);
    body.addBodyPart(new MimeBodyPart(headers, content));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    body.writeTo(bout);//  w  w w  . j  a va  2 s.  c om
    request.setContent(bout.toByteArray());
}