Search in sources :

Example 1 with DSNMessageHeaders

use of org.nhindirect.common.mail.dsn.DSNMessageHeaders 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)

Example 2 with DSNMessageHeaders

use of org.nhindirect.common.mail.dsn.DSNMessageHeaders in project nhin-d by DirectProject.

the class AbstractDSNCreator method createDSNFailure.

@Override
public MimeMessage createDSNFailure(Tx tx, NHINDAddressCollection failedRecipeints) throws MessagingException {
    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 = tx.getDetail(TxDetailType.FROM);
    if (sender != null) {
        originalSender = new InternetAddress(sender.getDetailValue());
        postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
    }
    final TxDetail subject = tx.getDetail(TxDetailType.SUBJECT);
    if (subject != null)
        originalSubject = subject.getDetailValue();
    for (NHINDAddress incompleteRecip : failedRecipeints) {
        DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, dsnStatus), incompleteRecip);
        recipientDSNHeaders.add(dsnRecipHeaders);
        failedRecipAddresses.add(incompleteRecip);
    }
    ///CLOVER:OFF
    final TxDetail origMessId = tx.getDetail(TxDetailType.MSG_ID);
    if (origMessId != null)
        originalMessageId = origMessId.getDetailValue();
    ///CLOVER:ON
    final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
    final TxDetail fullHeaders = tx.getDetail(TxDetailType.MSG_FULL_HEADERS);
    if (fullHeaders != null)
        fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
    final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
    return generator.createDSNMessage(originalSender, originalSubject, postmaster, recipientDSNHeaders, messageDSNHeaders, textBodyPart);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) DSNRecipientHeaders(org.nhindirect.common.mail.dsn.DSNRecipientHeaders) ArrayList(java.util.ArrayList) TxDetail(org.nhindirect.common.tx.model.TxDetail) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Header(javax.mail.Header) DSNMessageHeaders(org.nhindirect.common.mail.dsn.DSNMessageHeaders) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 3 with DSNMessageHeaders

use of org.nhindirect.common.mail.dsn.DSNMessageHeaders in project nhin-d by DirectProject.

the class AbstractDSNCreator method createDSNFailure.

@Override
public Collection<MimeMessage> createDSNFailure(Tx tx, NHINDAddressCollection failedRecipeints, boolean useSenderDomainForPostmaster) throws MessagingException {
    Collection<MimeMessage> retVal = new ArrayList<MimeMessage>();
    InternetAddress originalSender = null;
    String originalSubject = "";
    String originalMessageId = "";
    Enumeration<Header> fullMessageHeaders = null;
    final List<Address> failedRecipAddresses = new ArrayList<Address>();
    final TxDetail subject = tx.getDetail(TxDetailType.SUBJECT);
    if (subject != null)
        originalSubject = subject.getDetailValue();
    ///CLOVER:OFF
    final TxDetail origMessId = tx.getDetail(TxDetailType.MSG_ID);
    if (origMessId != null)
        originalMessageId = origMessId.getDetailValue();
    ///CLOVER:ON
    final TxDetail fullHeaders = tx.getDetail(TxDetailType.MSG_FULL_HEADERS);
    if (fullHeaders != null)
        fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
    final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
    final TxDetail sender = tx.getDetail(TxDetailType.FROM);
    if (sender != null)
        originalSender = new InternetAddress(sender.getDetailValue());
    final Map<InternetAddress, Collection<NHINDAddress>> dsnMessagePostmasterToFailedRecipMap = groupPostMasterAndFailedRecips(sender, failedRecipeints, useSenderDomainForPostmaster);
    if (dsnMessagePostmasterToFailedRecipMap.size() > 0) {
        for (Entry<InternetAddress, Collection<NHINDAddress>> entry : dsnMessagePostmasterToFailedRecipMap.entrySet()) {
            final List<DSNRecipientHeaders> recipientDSNHeaders = new ArrayList<DSNRecipientHeaders>();
            for (NHINDAddress incompleteRecip : entry.getValue()) {
                final DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, dsnStatus), incompleteRecip);
                recipientDSNHeaders.add(dsnRecipHeaders);
                failedRecipAddresses.add(incompleteRecip);
            }
            final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
            final MimeMessage dsnMessage = generator.createDSNMessage(originalSender, originalSubject, entry.getKey(), recipientDSNHeaders, messageDSNHeaders, textBodyPart);
            retVal.add(dsnMessage);
        }
    }
    return retVal;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) DSNRecipientHeaders(org.nhindirect.common.mail.dsn.DSNRecipientHeaders) ArrayList(java.util.ArrayList) TxDetail(org.nhindirect.common.tx.model.TxDetail) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) DSNMessageHeaders(org.nhindirect.common.mail.dsn.DSNMessageHeaders) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) Collection(java.util.Collection) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

ArrayList (java.util.ArrayList)3 Address (javax.mail.Address)3 Header (javax.mail.Header)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 DSNMessageHeaders (org.nhindirect.common.mail.dsn.DSNMessageHeaders)3 DSNRecipientHeaders (org.nhindirect.common.mail.dsn.DSNRecipientHeaders)3 TxDetail (org.nhindirect.common.tx.model.TxDetail)3 MimeMessage (javax.mail.internet.MimeMessage)2 NHINDAddress (org.nhindirect.stagent.NHINDAddress)2 Collection (java.util.Collection)1 Handler (org.apache.camel.Handler)1 Tx (org.nhindirect.common.tx.model.Tx)1 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)1