Search in sources :

Example 26 with MimeBodyPart

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

the class TestQueryTunnel method testNestedMultiPartBody.

@Test
public void testNestedMultiPartBody() throws Exception {
    // Construct a request with multi-part entity as a body and make sure it can be tunneled
    // The entity will be nested - the original multi-part body will become the 2nd part of the tunneled
    // body with the query passed as the form-encoded url
    final MimeMultipart multi = new MimeMultipart("mixed");
    MimeBodyPart partOne = new MimeBodyPart();
    partOne.setContent(new String("{\"name\":\"value\"}").getBytes(), "application/json");
    partOne.setHeader("Content-Type", "application/json");
    // Encode query params as form-urlencoded
    MimeBodyPart partTwo = new MimeBodyPart();
    partTwo.setContent("x=y&z=w&q=10", "application/x-www-form-urlencoded");
    partTwo.setHeader("Content-Type", "application /x-www-form-urlencoded");
    multi.addBodyPart(partTwo);
    multi.addBodyPart(partOne);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multi.writeTo(os);
    // Create request with multi-part body and query args
    final RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?args=xyz")).setMethod("PUT").setHeader("Content-Type", multi.getContentType()).setEntity(ByteString.copy(os.toByteArray())).build();
    // Encode and verify
    RestRequest encoded = encode(request, 0);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() > 0);
    Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
    Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
    // Decode and make sure we have the original request back
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(encoded, requestContext);
    Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?args=xyz");
    Assert.assertEquals(decoded.getMethod(), "PUT");
    Assert.assertEquals(decoded.getEntity(), request.getEntity());
    Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
    Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
    Assert.assertEquals(decoded.getHeader("Content-Length"), Integer.toString(request.getEntity().length()));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) MimeMultipart(javax.mail.internet.MimeMultipart) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestContext(com.linkedin.r2.message.RequestContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 27 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project google-app-engine-jappstart by taylorleese.

the class MailService method sendActivationEmail.

/**
     * Sends the activation e-mail to the given user.
     *
     * @param user the user
     * @param locale the locale
     * @throws MessagingException messaging exception
     */
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
    final Properties props = new Properties();
    final Session session = Session.getDefaultInstance(props, null);
    final Message message = new MimeMessage(session);
    final Multipart multipart = new MimeMultipart();
    final MimeBodyPart htmlPart = new MimeBodyPart();
    final MimeBodyPart textPart = new MimeBodyPart();
    message.setFrom(new InternetAddress(getFromAddress()));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
    message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));
    textPart.setContent(messageSource.getMessage("mail.body.txt", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");
    htmlPart.setContent(messageSource.getMessage("mail.body.html", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");
    multipart.addBodyPart(textPart);
    multipart.addBodyPart(htmlPart);
    message.setContent(multipart);
    Transport.send(message);
}
Also used : Locale(java.util.Locale) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Properties(java.util.Properties) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 28 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.

the class HumanReadableTextAssembler method makeBodyPart.

protected MimeBodyPart makeBodyPart(List<Address> rejectedRecipients, String errorMessage) throws MessagingException {
    String assembleHtmlBody;
    try {
        assembleHtmlBody = assembleHtmlBody(rejectedRecipients, errorMessage);
    } catch (IOException e) {
        throw new MessagingException("", e);
    }
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(assembleHtmlBody, "text/html");
    return mimeBodyPart;
}
Also used : MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 29 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method deserializeSignatureEnvelope.

/**
     * Extracts the ASN1 encoded signature data from the signed entity.
     * @param entity The entity containing the original signed part and the message signature.
     * @return A CMSSignedData object that contains the ASN1 encoded signature data of the message.
     */
public CMSSignedData deserializeSignatureEnvelope(SignedEntity entity) {
    if (entity == null) {
        throw new NHINDException();
    }
    CMSSignedData signed = null;
    try {
        //signed = new SMIMESigned(entity.getMimeMultipart());
        byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity.getContent());
        MimeBodyPart signedContent = null;
        signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
        signed = new CMSSignedData(new CMSProcessableBodyPart(signedContent), entity.getMimeMultipart().getBodyPart(1).getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        throw new MimeException(MimeError.Unexpected, e);
    }
    return signed;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.nhindirect.stagent.mail.MimeException) MimeBodyPart(javax.mail.internet.MimeBodyPart) NHINDException(org.nhindirect.stagent.NHINDException) CMSSignedData(org.bouncycastle.cms.CMSSignedData) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 30 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method createEncryptedEnvelope.

private MimeBodyPart createEncryptedEnvelope(MimeBodyPart bodyPart, Collection<X509Certificate> encryptingCertificates) {
    if (bodyPart == null || encryptingCertificates == null || encryptingCertificates.size() == 0) {
        throw new IllegalArgumentException();
    }
    if (LOGGER.isDebugEnabled()) {
        writePreEncypt(EntitySerializer.Default.serializeToBytes(bodyPart));
    }
    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
    for (X509Certificate cert : encryptingCertificates) gen.addKeyTransRecipient(cert);
    MimeBodyPart retVal = null;
    try {
        final String encryAlgOID = this.m_encryptionAlgorithm.getOID();
        retVal = gen.generate(bodyPart, encryAlgOID, CryptoExtensions.getJCEProviderNameForTypeAndAlgorithm("Cipher", encryAlgOID));
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return retVal;
}
Also used : SMIMEEnvelopedGenerator(org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator) MimeException(org.nhindirect.stagent.mail.MimeException) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) MimeBodyPart(javax.mail.internet.MimeBodyPart) X509Certificate(java.security.cert.X509Certificate) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException)

Aggregations

MimeBodyPart (javax.mail.internet.MimeBodyPart)172 MimeMultipart (javax.mail.internet.MimeMultipart)111 MimeMessage (javax.mail.internet.MimeMessage)65 MessagingException (javax.mail.MessagingException)64 IOException (java.io.IOException)49 DataHandler (javax.activation.DataHandler)39 ByteString (com.linkedin.data.ByteString)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 BodyPart (javax.mail.BodyPart)35 InternetAddress (javax.mail.internet.InternetAddress)31 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)30 Test (org.testng.annotations.Test)28 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)23 Multipart (javax.mail.Multipart)20 InputStream (java.io.InputStream)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)16 Session (javax.mail.Session)16 Date (java.util.Date)15 Properties (java.util.Properties)15