Search in sources :

Example 1 with NotificationMessage

use of org.nhindirect.stagent.mail.notifications.NotificationMessage in project nhin-d by DirectProject.

the class DefaultSmtpAgent method postProcessIncomingMessage.

private void postProcessIncomingMessage(MessageProcessResult result) {
    this.copyMessage(result.getProcessedMessage().getMessage(), settings.getIncomingMessageSettings());
    // check if we need to create notification messages
    try {
        if (settings.getNotificationProducer() != null) {
            result.setNotificationMessages(settings.getNotificationProducer().produce((IncomingMessage) result.getProcessedMessage()));
            if (result.getNotificationMessages() != null && auditor != null) {
                for (NotificationMessage noteMsg : result.getNotificationMessages()) {
                    Collection<AuditContext> contexts = createContextCollectionFromMessage(noteMsg, Arrays.asList(AuditEvents.MDN_HEADER_CONTEXT));
                    auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.PRODUCE_MDN_NAME, AuditEvents.EVENT_TYPE), contexts);
                }
            }
        }
    } catch (Exception e) {
        // don't bail on the whole process if we can't create notifications messages
        LOGGER.error("Failed to create notification messages.", e);
    }
    // check if this is an incoming MDN message... is so, audit it
    if (NotificationHelper.isMDN(result.getProcessedMessage().getMessage())) {
        Collection<AuditContext> contexts = createContextCollectionFromMessage(result.getProcessedMessage(), Arrays.asList(AuditEvents.MDN_RECEIVED_CONTEXT));
        auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.MDN_RECEIVED_NAME, AuditEvents.EVENT_TYPE), contexts);
    }
}
Also used : NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage) AuditEvent(org.nhindirect.common.audit.AuditEvent) AuditContext(org.nhindirect.common.audit.AuditContext) DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) MessagingException(javax.mail.MessagingException) AgentException(org.nhindirect.stagent.AgentException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 2 with NotificationMessage

use of org.nhindirect.stagent.mail.notifications.NotificationMessage in project nhin-d by DirectProject.

the class NotificationProducerTest method testProduceMDN_MultipleRecipients.

public void testProduceMDN_MultipleRecipients() throws Exception {
    NotificationSettings setting = new NotificationSettings(true, "", "");
    NotificationProducer prod = new NotificationProducer(setting);
    IncomingMessage msg = getMessageFromFile("MultipleRecipientsIncomingMessage.txt", Arrays.asList("cerner.com", "securehealthemail.com"));
    Collection<NotificationMessage> notes = prod.produce(msg);
    assertNotNull(notes);
    assertEquals(2, notes.size());
    boolean foundCernerCom = false;
    boolean foundSecureHealth = false;
    for (NHINDAddress noteMsg : msg.getDomainRecipients()) {
        if (noteMsg.toString().contains("cerner.com"))
            foundCernerCom = true;
        else if (noteMsg.toString().contains("securehealthemail.com"))
            foundSecureHealth = true;
    }
    assertTrue(foundCernerCom);
    assertTrue(foundSecureHealth);
}
Also used : NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage)

Example 3 with NotificationMessage

use of org.nhindirect.stagent.mail.notifications.NotificationMessage in project nhin-d by DirectProject.

the class NotificationProducerTest method testProduce_NoDomainRecipients_AssertEmptyList.

public void testProduce_NoDomainRecipients_AssertEmptyList() throws Exception {
    NotificationSettings setting = new NotificationSettings(true, "", "");
    NotificationProducer prod = new NotificationProducer(setting);
    IncomingMessage msg = getMessageFromFile("PlainIncomingMessage.txt", Arrays.asList("otherdomain.com"));
    Collection<NotificationMessage> notes = prod.produce(msg);
    assertNotNull(notes);
    assertEquals(0, notes.size());
}
Also used : NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage)

Example 4 with NotificationMessage

use of org.nhindirect.stagent.mail.notifications.NotificationMessage in project nhin-d by DirectProject.

the class NotificationProducerTest method testProduce_AuthResponseFalse_AssertEmptyList.

public void testProduce_AuthResponseFalse_AssertEmptyList() throws Exception {
    NotificationSettings setting = new NotificationSettings(false, "", "");
    NotificationProducer prod = new NotificationProducer(setting);
    IncomingMessage msg = getMessageFromFile("PlainIncomingMessage.txt", Arrays.asList("cerner.com"));
    Collection<NotificationMessage> notes = prod.produce(msg);
    assertNotNull(notes);
    assertEquals(0, notes.size());
}
Also used : NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage)

Example 5 with NotificationMessage

use of org.nhindirect.stagent.mail.notifications.NotificationMessage in project nhin-d by DirectProject.

the class DirectXdMailet method service.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
     */
@Override
public void service(Mail mail) throws MessagingException {
    LOGGER.info("Servicing DirectXdMailet");
    if (StringUtils.isBlank(endpointUrl)) {
        LOGGER.error("DirectXdMailet endpoint URL cannot be empty or null.");
        throw new MessagingException("DirectXdMailet endpoint URL cannot be empty or null.");
    }
    boolean successfulTransaction = false;
    final MimeMessage msg = mail.getMessage();
    final boolean isReliableAndTimely = TxUtil.isReliableAndTimelyRequested(msg);
    final NHINDAddressCollection initialRecipients = getMailRecipients(mail);
    final NHINDAddressCollection xdRecipients = new NHINDAddressCollection();
    final NHINDAddress sender = getMailSender(mail);
    Tx txToTrack = null;
    // Get recipients and create a collection of Strings
    final List<String> recipAddresses = new ArrayList<String>();
    for (NHINDAddress addr : initialRecipients) {
        recipAddresses.add(addr.getAddress());
    }
    // Service XD* addresses
    if (getResolver().hasXdEndpoints(recipAddresses)) {
        LOGGER.info("Recipients include XD endpoints");
        try {
            //List<Address> xdAddresses = new ArrayList<Address>();
            for (String s : getResolver().getXdEndpoints(recipAddresses)) {
                //xdAddresses.add((new MailAddress(s)).toInternetAddress());
                xdRecipients.add(new NHINDAddress(s));
            }
            txToTrack = this.getTxToTrack(msg, sender, xdRecipients);
            // Replace recipients with only XD* addresses
            //msg.setRecipients(RecipientType.TO, xdAddresses.toArray(new Address[0]));
            msg.setRecipients(RecipientType.TO, xdRecipients.toArray(new Address[0]));
            // Transform MimeMessage into ProvideAndRegisterDocumentSetRequestType object
            ProvideAndRegisterDocumentSetRequestType request = getMimeXDSTransformer().transform(msg);
            for (String directTo : recipAddresses) {
                String response = getDocumentRepository().forwardRequest(endpointUrl, request, directTo, sender.toString());
                if (!isSuccessful(response)) {
                    LOGGER.error("DirectXdMailet failed to deliver XD message.");
                    LOGGER.error(response);
                } else {
                    successfulTransaction = true;
                    if (isReliableAndTimely && txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
                        // send MDN dispatch for messages the recipients that were successful
                        final Collection<NotificationMessage> notifications = notificationProducer.produce(new Message(msg), xdRecipients.toInternetAddressCollection());
                        if (notifications != null && notifications.size() > 0) {
                            LOGGER.debug("Sending MDN \"dispathed\" messages");
                            // create a message for each notification and put it on James "stack"
                            for (NotificationMessage message : notifications) {
                                try {
                                    getMailetContext().sendMail(message);
                                } catch (Throwable t) {
                                    // don't kill the process if this fails
                                    LOGGER.error("Error sending MDN dispatched message.", t);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            LOGGER.error("DirectXdMailet delivery failure", e);
        }
    }
    // this basically sets the message back to it's original state with SMTP addresses only
    if (getResolver().hasSmtpEndpoints(recipAddresses)) {
        LOGGER.info("Recipients include SMTP endpoints");
        mail.setRecipients(getSmtpRecips(recipAddresses));
    } else {
        LOGGER.info("Recipients do not include SMTP endpoints");
        // No SMTP addresses, ghost it
        mail.setState(Mail.GHOST);
    }
    if (!successfulTransaction) {
        if (txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
            // for good measure, send DSN messages back to the original sender on failure
            // create a DSN message
            this.sendDSN(txToTrack, xdRecipients, false);
        }
    }
}
Also used : Tx(org.nhindirect.common.tx.model.Tx) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) ArrayList(java.util.ArrayList) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Aggregations

NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)11 IncomingMessage (org.nhindirect.stagent.IncomingMessage)7 MessagingException (javax.mail.MessagingException)4 MimeMessage (javax.mail.internet.MimeMessage)4 NHINDAddress (org.nhindirect.stagent.NHINDAddress)4 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)4 Tx (org.nhindirect.common.tx.model.Tx)3 Message (org.nhindirect.stagent.mail.Message)3 ArrayList (java.util.ArrayList)2 MailAddress (org.apache.mailet.MailAddress)2 ProvideAndRegisterDocumentSetRequestType (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 Collection (java.util.Collection)1 Address (javax.mail.Address)1 InternetHeaders (javax.mail.internet.InternetHeaders)1 AuditContext (org.nhindirect.common.audit.AuditContext)1 AuditEvent (org.nhindirect.common.audit.AuditEvent)1