Search in sources :

Example 1 with MessageProcessResult

use of org.nhindirect.gateway.smtp.MessageProcessResult in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_UseRcpt_AssertRecipientsUsed.

public void testService_UseRcpt_AssertRecipientsUsed() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            usedSender = (NHINDAddress) invocation.getArguments()[2];
            return new MessageProcessResult(new DefaultMessageEnvelope(new Message(mimeMsg), usedRecipients, usedSender), null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(Arrays.asList(new MailAddress("you@cerner.com")));
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertNotNull(usedRecipients);
    assertEquals(1, usedRecipients.size());
    assertEquals("you@cerner.com", usedRecipients.iterator().next().toString());
}
Also used : MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Mail(org.apache.mailet.Mail) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 2 with MessageProcessResult

use of org.nhindirect.gateway.smtp.MessageProcessResult in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet method service.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public void service(Mail mail) throws MessagingException {
    GatewayState.getInstance().lockForProcessing();
    try {
        Tx txToMonitor = null;
        LOGGER.trace("Entering service(Mail mail)");
        onPreprocessMessage(mail);
        final MimeMessage msg = mail.getMessage();
        final NHINDAddressCollection recipients = getMailRecipients(mail);
        // get the sender
        final NHINDAddress sender = getMailSender(mail);
        LOGGER.info("Proccessing incoming message from sender " + sender.toString());
        MessageProcessResult result = null;
        final boolean isOutgoing = this.isOutgoing(msg, sender);
        // gathered now before the message is transformed
        if (isOutgoing)
            txToMonitor = getTxToTrack(msg, sender, recipients);
        // recipients can get modified by the security and trust agent, so make a local copy
        // before processing
        final NHINDAddressCollection originalRecipList = NHINDAddressCollection.create(recipients);
        try {
            // process the message with the agent stack
            LOGGER.trace("Calling agent.processMessage");
            result = agent.processMessage(msg, recipients, sender);
            LOGGER.trace("Finished calling agent.processMessage");
            if (result == null) {
                LOGGER.error("Failed to process message.  processMessage returned null.");
                onMessageRejected(mail, originalRecipList, sender, isOutgoing, txToMonitor, null);
                mail.setState(Mail.GHOST);
                LOGGER.trace("Exiting service(Mail mail)");
                return;
            }
        } catch (Exception e) {
            // catch all
            LOGGER.error("Failed to process message: " + e.getMessage(), e);
            onMessageRejected(mail, originalRecipList, sender, isOutgoing, txToMonitor, e);
            mail.setState(Mail.GHOST);
            LOGGER.trace("Exiting service(Mail mail)");
            return;
        }
        if (result.getProcessedMessage() != null) {
            mail.setMessage(result.getProcessedMessage().getMessage());
        } else {
            /*
				 * TODO: Handle exception... GHOST the message for now and eat it
				 */
            LOGGER.debug("Processed message is null.  GHOST and eat the message.");
            onMessageRejected(mail, recipients, sender, null);
            mail.setState(Mail.GHOST);
            return;
        }
        // remove reject recipients from the RCTP headers
        if (result.getProcessedMessage().getRejectedRecipients() != null && result.getProcessedMessage().getRejectedRecipients().size() > 0 && mail.getRecipients() != null && mail.getRecipients().size() > 0) {
            final Collection<MailAddress> newRCPTList = new ArrayList<MailAddress>();
            for (MailAddress rctpAdd : (Collection<MailAddress>) mail.getRecipients()) {
                if (!isRcptRejected(rctpAdd, result.getProcessedMessage().getRejectedRecipients())) {
                    newRCPTList.add(rctpAdd);
                }
            }
            mail.setRecipients(newRCPTList);
        }
        /*
			 * Handle sending MDN messages
			 */
        final Collection<NotificationMessage> notifications = result.getNotificationMessages();
        if (notifications != null && notifications.size() > 0) {
            LOGGER.info("MDN messages requested.  Sending MDN \"processed\" messages");
            // create a message for each notification and put it on James "stack"
            for (NotificationMessage message : notifications) {
                try {
                    this.getMailetContext().sendMail(message);
                } catch (Throwable t) {
                    // don't kill the process if this fails
                    LOGGER.error("Error sending MDN message.", t);
                }
            }
        }
        // track message
        trackMessage(txToMonitor, isOutgoing);
        onPostprocessMessage(mail, result, isOutgoing, txToMonitor);
        LOGGER.trace("Exiting service(Mail mail)");
    } finally {
        GatewayState.getInstance().unlockFromProcessing();
    }
}
Also used : MailAddress(org.apache.mailet.MailAddress) Tx(org.nhindirect.common.tx.model.Tx) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) ArrayList(java.util.ArrayList) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) MessagingException(javax.mail.MessagingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SmtpAgentException(org.nhindirect.gateway.smtp.SmtpAgentException) MalformedURLException(java.net.MalformedURLException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection)

Example 3 with MessageProcessResult

use of org.nhindirect.gateway.smtp.MessageProcessResult in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_NullProcessedMessage_GhostState.

public void testService_NullProcessedMessage_GhostState() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            return new MessageProcessResult(null, null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(Arrays.asList(new MailAddress("you@cerner.com")));
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertEquals(Mail.GHOST, mockMail.getState());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult)

Example 4 with MessageProcessResult

use of org.nhindirect.gateway.smtp.MessageProcessResult in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_RejectRecipients_AssertRejectedList.

@SuppressWarnings("unused")
public void testService_RejectRecipients_AssertRejectedList() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            usedRecipients.get(0).setStatus(TrustEnforcementStatus.Failed);
            usedRecipients.get(1).setStatus(TrustEnforcementStatus.Success);
            usedSender = (NHINDAddress) invocation.getArguments()[2];
            MyMessageEnvelope env = new MyMessageEnvelope(new Message(mimeMsg), usedRecipients, usedSender);
            env.setAgent(new MockNHINDAgent(Arrays.asList("cerner.com")));
            env.categorizeRecipients(TrustEnforcementStatus.Success);
            NHINDAddressCollection rejectedRecips = env.getRejectedRecipients();
            return new MessageProcessResult(env, null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    mockMail.setRecipients(Arrays.asList(new MailAddress("you@cerner.com"), new MailAddress("they@cerner.com")));
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertEquals(1, mockMail.getRecipients().size());
}
Also used : MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) NHINDAddress(org.nhindirect.stagent.NHINDAddress) MockNHINDAgent(org.nhindirect.stagent.MockNHINDAgent) Mail(org.apache.mailet.Mail) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 5 with MessageProcessResult

use of org.nhindirect.gateway.smtp.MessageProcessResult in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_UseToHeader_AssertRecipientsUsed.

public void testService_UseToHeader_AssertRecipientsUsed() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            return new MessageProcessResult(new DefaultMessageEnvelope(new Message(mimeMsg)), null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(null);
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertNotNull(usedRecipients);
    assertEquals(1, usedRecipients.size());
    assertEquals("externUser1@starugh-stateline.com", usedRecipients.iterator().next().toString());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)5 MailAddress (org.apache.mailet.MailAddress)5 MessageProcessResult (org.nhindirect.gateway.smtp.MessageProcessResult)5 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)5 Mail (org.apache.mailet.Mail)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 SmtpAgent (org.nhindirect.gateway.smtp.SmtpAgent)4 NHINDAddress (org.nhindirect.stagent.NHINDAddress)3 Message (org.nhindirect.stagent.mail.Message)3 DefaultMessageEnvelope (org.nhindirect.stagent.DefaultMessageEnvelope)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 MessagingException (javax.mail.MessagingException)1 ServiceException (org.nhindirect.common.rest.exceptions.ServiceException)1 Tx (org.nhindirect.common.tx.model.Tx)1 SmtpAgentException (org.nhindirect.gateway.smtp.SmtpAgentException)1 MockNHINDAgent (org.nhindirect.stagent.MockNHINDAgent)1 NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)1