Search in sources :

Example 31 with MimeBodyPart

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

the class EntitySerializer method serialize.

/**
     * Serializes a collection of MimeBodyPart to an output stream with a given boundary.
     * @param entity The entities to serialize.
     * @param boundary The boundary string that will separate each entity.
     * @param stream The output stream that the entities will be serialized to.
     */
public void serialize(Collection<MimeBodyPart> parts, String boundary, OutputStream stream) {
    if (parts == null || parts.size() == 0) {
        throw new IllegalArgumentException();
    }
    try {
        MimeMultipart mm = new MimeMultipart();
        for (MimeBodyPart part : parts) {
            mm.addBodyPart(part);
        }
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        mm.writeTo(oStream);
        oStream.flush();
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 32 with MimeBodyPart

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

the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_safeNetHSMSignatureProvider_assertGenerated.

public void testGenerate_safeNetHSMSignatureProvider_assertGenerated() throws Exception {
    /**
         * This test is only run if a specific SafeNet eToken Pro HSM is connected to the testing 
         * system.  This can be modified for another specific machine and/or token.
         */
    pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        // get a certificate from the key store
        final KeyStore ks = KeyStore.getInstance("PKCS11");
        ks.load(null, "1Kingpuff".toCharArray());
        final Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            final String alias = aliases.nextElement();
            final KeyStore.Entry entry = ks.getEntry(alias, null);
            if (entry instanceof KeyStore.PrivateKeyEntry) {
                KeyStore.PrivateKeyEntry ent = (KeyStore.PrivateKeyEntry) entry;
                signerCert = X509CertificateEx.fromX509Certificate((X509Certificate) ent.getCertificate(), ent.getPrivateKey());
                break;
            }
        }
        final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator(pkcs11ProvName, "BC");
        setupSigningInfo(gen);
        // create the content 
        final MimeBodyPart signedContent = new MimeBodyPart();
        signedContent.addHeader("To:", "me@you.com");
        signedContent.addHeader("From", "test.test.com");
        signedContent.setText("Some Text To Sign");
        final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
        final CMSSignedData signedData = gen.generate(content);
        validateSignature(signedData);
    }
}
Also used : SplitProviderDirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator) MimeBodyPart(javax.mail.internet.MimeBodyPart) KeyStore(java.security.KeyStore) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 33 with MimeBodyPart

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

the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_differentDefaultSigAndDigestProvider_assertGenerated.

public void testGenerate_differentDefaultSigAndDigestProvider_assertGenerated() throws Exception {
    final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator("SunRsaSign", "BC");
    setupSigningInfo(gen);
    // create the content 
    final MimeBodyPart signedContent = new MimeBodyPart();
    signedContent.addHeader("To:", "me@you.com");
    signedContent.addHeader("From", "test.test.com");
    signedContent.setText("Some Text To Sign");
    final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
    final CMSSignedData signedData = gen.generate(content);
    validateSignature(signedData);
}
Also used : SplitProviderDirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator) MimeBodyPart(javax.mail.internet.MimeBodyPart) CMSSignedData(org.bouncycastle.cms.CMSSignedData) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 34 with MimeBodyPart

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

the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_sameDefaultSigAndDigestProvider_assertGenerated.

public void testGenerate_sameDefaultSigAndDigestProvider_assertGenerated() throws Exception {
    final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator("", "");
    setupSigningInfo(gen);
    // create the content 
    final MimeBodyPart signedContent = new MimeBodyPart();
    signedContent.addHeader("To:", "me@you.com");
    signedContent.addHeader("From", "test.test.com");
    signedContent.setText("Some Text To Sign");
    final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
    final CMSSignedData signedData = gen.generate(content);
    validateSignature(signedData);
}
Also used : SplitProviderDirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator) MimeBodyPart(javax.mail.internet.MimeBodyPart) CMSSignedData(org.bouncycastle.cms.CMSSignedData) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 35 with MimeBodyPart

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

the class DSNMessageGenerator method generateDSNFailureMessage.

/**
	 * Generates the DSN message a replacing the existing exchange in body with the DSN message as a MimeMessage object.
	 * @param txs Collection of correlated Tx objects.
	 * @param ex The message exchange.
	 * @throws Exception
	 */
@Handler
public void generateDSNFailureMessage(Collection<Tx> txs, Exchange ex) throws Exception {
    // change the inbound message body to null
    ex.getIn().setBody(null);
    // get the message that is being tracked so we can generate an error message for it
    Tx messageToTrack = AbstractCompletionCondition.getMessageToTrack(txs);
    if (messageToTrack != null) {
        // make sure we have incomplete recipients
        final Collection<String> incompleteRecips = conditionChecker.getIncompleteRecipients(txs);
        if (incompleteRecips != null && !incompleteRecips.isEmpty()) {
            InternetAddress originalSender = null;
            String originalSubject = "";
            InternetAddress postmaster = null;
            String originalMessageId = "";
            Enumeration<Header> fullMessageHeaders = null;
            final List<DSNRecipientHeaders> recipientDSNHeaders = new ArrayList<DSNRecipientHeaders>();
            final List<Address> failedRecipAddresses = new ArrayList<Address>();
            final TxDetail sender = messageToTrack.getDetail(TxDetailType.FROM);
            if (sender != null) {
                originalSender = new InternetAddress(sender.getDetailValue());
                postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
            }
            final TxDetail subject = messageToTrack.getDetail(TxDetailType.SUBJECT);
            if (subject != null)
                originalSubject = subject.getDetailValue();
            for (String incompleteRecip : incompleteRecips) {
                final Address failedRecipAddress = new InternetAddress(incompleteRecip);
                DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS), failedRecipAddress);
                recipientDSNHeaders.add(dsnRecipHeaders);
                failedRecipAddresses.add(failedRecipAddress);
            }
            ///CLOVER:OFF
            final TxDetail origMessId = messageToTrack.getDetail(TxDetailType.MSG_ID);
            if (origMessId != null)
                originalMessageId = origMessId.getDetailValue();
            ///CLOVER:ON
            final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
            final TxDetail fullHeaders = messageToTrack.getDetail(TxDetailType.MSG_FULL_HEADERS);
            if (fullHeaders != null)
                fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
            final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
            final MimeMessage dnsMessage = generator.createDSNMessage(originalSender, originalSubject, postmaster, recipientDSNHeaders, messageDSNHeaders, textBodyPart);
            ex.getIn().setBody(dnsMessage);
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Tx(org.nhindirect.common.tx.model.Tx) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) DSNRecipientHeaders(org.nhindirect.common.mail.dsn.DSNRecipientHeaders) ArrayList(java.util.ArrayList) TxDetail(org.nhindirect.common.tx.model.TxDetail) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) DSNMessageHeaders(org.nhindirect.common.mail.dsn.DSNMessageHeaders) MimeBodyPart(javax.mail.internet.MimeBodyPart) Handler(org.apache.camel.Handler)

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