Search in sources :

Example 6 with TxMessageType

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

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

the class IsNotSMIMEEncrypted method match.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("rawtypes")
public Collection match(Mail mail) throws MessagingException {
    if (mail == null)
        return null;
    MimeMessage message = mail.getMessage();
    if (message == null)
        return null;
    TxMessageType type = TxUtil.getMessageType(message);
    if (type != TxMessageType.SMIME) {
        return mail.getRecipients();
    } else
        return null;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) TxMessageType(org.nhindirect.common.tx.model.TxMessageType)

Example 8 with TxMessageType

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

the class IsNotification method match.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("rawtypes")
public Collection match(Mail mail) throws MessagingException {
    if (mail == null)
        return null;
    MimeMessage message = mail.getMessage();
    if (message == null)
        return null;
    TxMessageType type = TxUtil.getMessageType(message);
    if (type == TxMessageType.DSN || type == TxMessageType.MDN) {
        return mail.getRecipients();
    } else
        return null;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) TxMessageType(org.nhindirect.common.tx.model.TxMessageType)

Example 9 with TxMessageType

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

the class DefaultNHINDAgent method processMessage.

/*
     * Process the incoming message by apply the security and trust algorithms. 
     *
     */
protected void processMessage(IncomingMessage message) {
    ///CLOVER:OFF
    if (message.getSender() == null) {
        throw new TrustException(TrustError.UntrustedSender);
    }
    ///CLOVER:ON
    message.categorizeRecipients(this.getDomains());
    if (!message.hasDomainRecipients()) {
        throw new AgentException(AgentError.NoTrustedRecipients);
    }
    //
    // Map each address to its certificates/trust settings
    //
    this.bindAddresses(message);
    //
    // Extract signed content from the message
    //
    this.decryptSignedContent(message);
    message.setMessage(this.unwrapMessage(message.getMessage()));
    // Enforce trust requirements, including checking signatures
    //
    // need to decide if this message is a notification and message and 
    // if outgoing policy can be used for trust enforcement
    final boolean allowOutgoingPolicyForIncomingNotifications = OptionsParameter.getParamValueAsBoolean(OptionsManager.getInstance().getParameter(OptionsParameter.USE_OUTGOING_POLICY_FOR_INCOMING_NOTIFICATIONS), false);
    if (allowOutgoingPolicyForIncomingNotifications) {
        final TxMessageType msgType = TxUtil.getMessageType(message.getMessage());
        // determine if this message is a notification message
        if (msgType.equals(TxMessageType.DSN) || msgType.equals(TxMessageType.MDN)) {
            // need to apply outgoing anchor policy to each recipient
            for (NHINDAddress recipient : message.getDomainRecipients()) {
                try {
                    final Collection<X509Certificate> anchors = new ArrayList<X509Certificate>(trustAnchors.getIncomingAnchors().getCertificates(recipient));
                    anchors.addAll(trustAnchors.getOutgoingAnchors().getCertificates(recipient));
                    recipient.setTrustAnchors(anchors);
                } catch (Exception e) {
                    LOGGER.warn("Exception getting anchors for inbound notification policy.", e);
                }
            }
        }
    }
    // for incoming messages, headers may have been re-written
    // by a man in the middle attack.  The authoritative headers are in the unwrapped message, so they need to be compared to the header in 
    // Enveloped message to ensure that they have not been tampered
    // Policy will dictate what the STA will do with the message if a tamper has been detected
    enforceTamperPolicy(message);
    this.trustModel.enforce(message);
    //
    if (message.hasDomainRecipients()) {
        message.categorizeRecipients(this.minTrustRequirement);
    }
    if (!message.hasDomainRecipients()) {
        throw new TrustException(TrustError.NoTrustedRecipients);
    }
    message.updateRoutingHeaders();
}
Also used : TrustException(org.nhindirect.stagent.trust.TrustException) ArrayList(java.util.ArrayList) X509Certificate(java.security.cert.X509Certificate) MessagingException(javax.mail.MessagingException) TrustException(org.nhindirect.stagent.trust.TrustException) MimeException(org.nhindirect.stagent.mail.MimeException) PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) PolicyParseException(org.nhindirect.policy.PolicyParseException) IOException(java.io.IOException) PolicyProcessException(org.nhindirect.policy.PolicyProcessException) TxMessageType(org.nhindirect.common.tx.model.TxMessageType)

Aggregations

TxMessageType (org.nhindirect.common.tx.model.TxMessageType)9 TxDetail (org.nhindirect.common.tx.model.TxDetail)6 NotificationDAOException (org.nhindirect.monitor.dao.NotificationDAOException)3 DuplicateNotificationStateManagerException (org.nhindirect.monitor.processor.DuplicateNotificationStateManagerException)3 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 DeliveryStatus (com.sun.mail.dsn.DeliveryStatus)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Address (javax.mail.Address)1 InternetAddress (javax.mail.internet.InternetAddress)1 InternetHeaders (javax.mail.internet.InternetHeaders)1 Tx (org.nhindirect.common.tx.model.Tx)1 PolicyParseException (org.nhindirect.policy.PolicyParseException)1 PolicyProcessException (org.nhindirect.policy.PolicyProcessException)1 PolicyRequiredException (org.nhindirect.policy.PolicyRequiredException)1 MimeException (org.nhindirect.stagent.mail.MimeException)1