Search in sources :

Example 41 with TxDetail

use of org.nhindirect.common.tx.model.TxDetail in project nhin-d by DirectProject.

the class GeneralCompletionCondition method getIncompleteRecipients.

/**
	 * {@inheritDoc}
	 */
@Override
public Collection<String> getIncompleteRecipients(Collection<Tx> txs) {
    if (txs == null || txs.size() == 0)
        return Collections.emptyList();
    final Tx originalMessage = getMessageToTrack(txs);
    if (originalMessage == null)
        return Collections.emptyList();
    final TxDetail originalRecipDetail = originalMessage.getDetail(TxDetailType.RECIPIENTS.getType());
    if (originalRecipDetail == null)
        return Collections.emptyList();
    // add the original recipient list to a map of recipients to status
    final Map<String, RecipientResponseStatus> recipStatuses = new HashMap<String, RecipientResponseStatus>();
    for (String recip : originalRecipDetail.getDetailValue().split(",")) recipStatuses.put(recip, new RecipientResponseStatus(recip));
    for (Tx tx : txs) {
        final TxDetail finalRecipDetail = tx.getDetail(TxDetailType.FINAL_RECIPIENTS.getType());
        if (finalRecipDetail != null) {
            switch(tx.getMsgType()) {
                case MDN:
                    {
                        // an MDN is sent per original message recipient, so we should only be able
                        // to extract one original recipient from this message
                        final RecipientResponseStatus recipStatus = recipStatuses.get(finalRecipDetail.getDetailValue());
                        if (recipStatus != null)
                            recipStatus.addReceivedStatus(RecipientResponseStatus.MDNReceived);
                        break;
                    }
                case DSN:
                    {
                        // need to split the recipients out
                        for (String finalRecip : finalRecipDetail.getDetailValue().split(",")) {
                            final RecipientResponseStatus recipStatus = recipStatuses.get(finalRecip);
                            if (recipStatus != null)
                                recipStatus.addReceivedStatus(RecipientResponseStatus.DSNReceived);
                        }
                        break;
                    }
            }
        }
    }
    final Collection<String> retVal = new ArrayList<String>();
    // have received some type of notification
    for (RecipientResponseStatus status : recipStatuses.values()) if (status.getReceivedStatus() == 0) {
        retVal.add(status.getRecipient());
    }
    return retVal;
}
Also used : Tx(org.nhindirect.common.tx.model.Tx) HashMap(java.util.HashMap) TxDetail(org.nhindirect.common.tx.model.TxDetail) ArrayList(java.util.ArrayList)

Example 42 with TxDetail

use of org.nhindirect.common.tx.model.TxDetail 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 43 with TxDetail

use of org.nhindirect.common.tx.model.TxDetail 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)

Example 44 with TxDetail

use of org.nhindirect.common.tx.model.TxDetail in project nhin-d by DirectProject.

the class DefaultTxDetailParser method getMessageDetails.

@SuppressWarnings("incomplete-switch")
@Override
public Map<String, TxDetail> getMessageDetails(MimeMessage msg) {
    Map<String, TxDetail> retVal = new HashMap<String, TxDetail>();
    // get the message id
    final String msgId = MailStandard.getHeader(msg, MailStandard.Headers.MessageID);
    if (!msgId.isEmpty())
        retVal.put(TxDetailType.MSG_ID.getType(), new TxDetail(TxDetailType.MSG_ID.getType(), msgId));
    // get the subject
    final String subject = MailStandard.getHeader(msg, MailStandard.Headers.Subject);
    if (!subject.isEmpty())
        retVal.put(TxDetailType.SUBJECT.getType(), new TxDetail(TxDetailType.SUBJECT.getType(), subject));
    // get the full headers as a string
    final String fullHeaders = getHeadersAsStringInternal(msg);
    if (!fullHeaders.isEmpty())
        retVal.put(TxDetailType.MSG_FULL_HEADERS.getType(), new TxDetail(TxDetailType.MSG_FULL_HEADERS.getType(), fullHeaders));
    // get the from addresses
    try {
        final String from = MailStandard.getHeader(msg, MailStandard.Headers.From);
        if (!from.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            int cnt = 0;
            for (InternetAddress addr : (InternetAddress[]) msg.getFrom()) {
                // comma delimit multiple addresses
                if (cnt > 0)
                    builder.append(",");
                builder.append(addr.getAddress().toLowerCase(Locale.getDefault()));
                ++cnt;
            }
            retVal.put(TxDetailType.FROM.getType(), new TxDetail(TxDetailType.FROM.getType(), builder.toString()));
        }
    }/// CLOVER:OFF
     catch (MessagingException e) {
        LOGGER.warn("Failed to retrieve message sender list.", e);
    }
    // get the sender if it exists
    try {
        final InternetAddress sender = (InternetAddress) msg.getSender();
        if (sender != null)
            retVal.put(TxDetailType.SENDER.getType(), new TxDetail(TxDetailType.SENDER.toString(), sender.getAddress().toLowerCase(Locale.getDefault())));
    }/// CLOVER:OFF
     catch (MessagingException e) {
        LOGGER.warn("Failed to retrieve message sender", e);
    }
    // get the recipient addresses
    try {
        if (msg.getAllRecipients() != null) {
            StringBuilder builder = new StringBuilder();
            int cnt = 0;
            for (Address addr : msg.getAllRecipients()) {
                // comma delimit multiple addresses
                if (cnt > 0)
                    builder.append(",");
                if (addr instanceof InternetAddress)
                    builder.append(((InternetAddress) addr).getAddress().toLowerCase(Locale.getDefault()));
                ++cnt;
            }
            retVal.put(TxDetailType.RECIPIENTS.getType(), new TxDetail(TxDetailType.RECIPIENTS.getType(), builder.toString()));
        }
    }/// CLOVER:OFF
     catch (MessagingException e) {
        LOGGER.warn("Failed to retrieve message recipient list.", e);
    }
    /// CLOVER:ON
    // get the message type
    final TxMessageType messageType = TxUtil.getMessageType(msg);
    if (messageType != TxMessageType.UNKNOWN) {
        switch(messageType) {
            case MDN:
                {
                    // the disposition if a field in the second part of the MDN message
                    final String disposition = MDNStandard.getMDNField(msg, MDNStandard.Headers.Disposition);
                    if (!disposition.isEmpty())
                        retVal.put(TxDetailType.DISPOSITION.getType(), new TxDetail(TxDetailType.DISPOSITION.getType(), disposition.toLowerCase(Locale.getDefault())));
                    // the final recipients is a field in the second part of the MDN message
                    final String finalRecipient = MDNStandard.getMDNField(msg, MDNStandard.Headers.FinalRecipient);
                    if (!finalRecipient.isEmpty())
                        retVal.put(TxDetailType.FINAL_RECIPIENTS.getType(), new TxDetail(TxDetailType.FINAL_RECIPIENTS.getType(), finalRecipient.toLowerCase(Locale.getDefault())));
                    // the original message id if a field in the second part of the MDN message
                    String origMsgId = MDNStandard.getMDNField(msg, MDNStandard.Headers.OriginalMessageID);
                    if (origMsgId.isEmpty()) {
                        // it might be in a reply to header
                        origMsgId = MailStandard.getHeader(msg, MailStandard.Headers.InReplyTo);
                    }
                    if (!origMsgId.isEmpty())
                        retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID.getType(), origMsgId));
                    // check for X-DIRECT-FINAL-DESTINATION-DELIVER extension
                    try {
                        final InternetHeaders mdnHeaders = MDNStandard.getNotificationFieldsAsHeaders(msg);
                        if (mdnHeaders.getHeader(MDNStandard.DispositionOption_TimelyAndReliable, ",") != null) {
                            retVal.put(TxDetailType.DISPOSITION_OPTIONS.getType(), new TxDetail(TxDetailType.DISPOSITION_OPTIONS.getType(), MDNStandard.DispositionOption_TimelyAndReliable));
                        }
                    }// CLOVER:OFF
                     catch (Exception e) {
                        LOGGER.warn("Failed to retrieve MDN headers from message.  Message may not be an MDN message.", e);
                    }
                    // CLOVER:ON
                    break;
                }
            case DSN:
                {
                    // the Original-Envelope-ID header does not reflect the message id
                    try {
                        final DeliveryStatus status = new DeliveryStatus(new ByteArrayInputStream(MailUtil.serializeToBytes(msg)));
                        retVal.put(TxDetailType.FINAL_RECIPIENTS.getType(), new TxDetail(TxDetailType.FINAL_RECIPIENTS.getType(), DSNStandard.getFinalRecipients(status).toLowerCase(Locale.getDefault())));
                        // check at the message level
                        boolean parentFound = false;
                        final String origMsgId = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.OriginalMessageID);
                        if (!origMsgId.isEmpty()) {
                            parentFound = true;
                            retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID, origMsgId));
                        }
                        if (!parentFound) {
                            // it might be in a reply to header
                            final String parentMsgId = MailStandard.getHeader(msg, MailStandard.Headers.InReplyTo);
                            if (!parentMsgId.isEmpty())
                                retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID.getType(), parentMsgId));
                        }
                        // get the action
                        final String action = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.Action);
                        if (!action.isEmpty())
                            retVal.put(TxDetailType.DSN_ACTION.getType(), new TxDetail(TxDetailType.DSN_ACTION.getType(), action.toLowerCase(Locale.getDefault())));
                        // get the status
                        final String dsnStatus = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.Status);
                        if (!dsnStatus.isEmpty())
                            retVal.put(TxDetailType.DSN_STATUS.getType(), new TxDetail(TxDetailType.DSN_STATUS.getType(), dsnStatus.toLowerCase(Locale.getDefault())));
                    }///CLOVER:OFF
                     catch (Exception e) {
                        LOGGER.warn("Could not get a requested field from the DSN message", e);
                    }
                    ///CLOVER:ON
                    break;
                }
        }
    }
    // check for the existence of disposition request options
    final String dispOption = MailStandard.getHeader(msg, MDNStandard.Headers.DispositionNotificationOptions);
    if (!dispOption.isEmpty())
        retVal.put(TxDetailType.DISPOSITION_OPTIONS.getType(), new TxDetail(TxDetailType.DISPOSITION_OPTIONS.getType(), dispOption.toLowerCase(Locale.getDefault())));
    return retVal;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) InternetHeaders(javax.mail.internet.InternetHeaders) HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) TxDetail(org.nhindirect.common.tx.model.TxDetail) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) DeliveryStatus(com.sun.mail.dsn.DeliveryStatus) TxMessageType(org.nhindirect.common.tx.model.TxMessageType)

Example 45 with TxDetail

use of org.nhindirect.common.tx.model.TxDetail in project nhin-d by DirectProject.

the class DefaultTxDetailParser method getMessageDetails.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public Map<String, TxDetail> getMessageDetails(InternetHeaders headers) {
    Map<String, TxDetail> retVal = null;
    try {
        // convert into a MimeMessage with only the headers
        final MimeMessage msg = new MimeMessage((Session) null);
        final Enumeration<String> henum = headers.getAllHeaderLines();
        while (henum.hasMoreElements()) msg.addHeaderLine(henum.nextElement());
        retVal = getMessageDetails(msg);
    }///CLOVER:OFF		
     catch (MessagingException e) {
        LOGGER.warn("Failed to translate headers to MimeMessage.", e);
    }
    return retVal;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TxDetail(org.nhindirect.common.tx.model.TxDetail)

Aggregations

TxDetail (org.nhindirect.common.tx.model.TxDetail)70 Test (org.junit.Test)50 Tx (org.nhindirect.common.tx.model.Tx)35 MimeMessage (javax.mail.internet.MimeMessage)32 HashMap (java.util.HashMap)20 Exchange (org.apache.camel.Exchange)10 CamelContext (org.apache.camel.CamelContext)9 DefaultExchange (org.apache.camel.impl.DefaultExchange)9 ArrayList (java.util.ArrayList)7 TxMessageType (org.nhindirect.common.tx.model.TxMessageType)6 NotificationDAOException (org.nhindirect.monitor.dao.NotificationDAOException)5 InputStream (java.io.InputStream)4 Address (javax.mail.Address)4 InternetAddress (javax.mail.internet.InternetAddress)4 DefaultTxDetailParser (org.nhindirect.common.tx.impl.DefaultTxDetailParser)4 TxCompletionCondition (org.nhindirect.monitor.condition.TxCompletionCondition)4 Header (javax.mail.Header)3 MessagingException (javax.mail.MessagingException)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 DSNMessageHeaders (org.nhindirect.common.mail.dsn.DSNMessageHeaders)3