use of org.nhindirect.stagent.DefaultMessageEnvelope in project nhin-d by DirectProject.
the class DefaultSmtpAgent method processMessage.
/**
* Processes an message from an SMTP stack. The bridge component between the SMTP stack and the SMTP agent is responsible for
* extracting the message, the recipient list, and the sender. In some cases, the routing headers may have different information than
* what is populated in the SMTP MAIL FROM and RCTP TO headers. In these cases, the SMTP headers should be favored over the routing
* headers in the message and passed as the recipient collection and sender to this method.
* @param message The message in the SMTP envelope.
* @param recipients The recipients of the message. The RCTP TO headers should be used over the message routing headers.
* @param sender The send of the message. The MAIL FROM header should be used over the From: routing header in the message.
*/
public MessageProcessResult processMessage(MimeMessage message, NHINDAddressCollection recipients, NHINDAddress sender) {
GatewayState.getInstance().lockForProcessing();
try {
LOGGER.trace("Entering processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
MessageProcessResult retVal = null;
verifyInitialized();
preProcessMessage(message, sender);
Collection<NHINDAddress> originalRecipList = new ArrayList<NHINDAddress>(recipients);
DefaultMessageEnvelope envelopeToProcess = null;
try {
envelopeToProcess = new DefaultMessageEnvelope(new Message(message), recipients, sender);
envelopeToProcess.setAgent(agent);
// should always result in either a non null object or an exception
MessageEnvelope processEvn = processEnvelope(envelopeToProcess);
retVal = new MessageProcessResult(processEvn, null);
if (retVal.getProcessedMessage() != null)
postProcessMessage(retVal);
} catch (SmtpAgentException e) {
// rethrow
LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
throw e;
} catch (Exception e) {
// audit the message rejection
if (envelopeToProcess != null) {
Collection<AuditContext> contexts = createContextCollectionFromMessage(envelopeToProcess, Arrays.asList(AuditEvents.DEFAULT_HEADER_CONTEXT));
if (e instanceof NHINDException) {
NHINDException exception = (NHINDException) e;
if (exception.getError() != null) {
contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_MESSAGE_REASON_CONTEXT, exception.getError().toString()));
if (exception.getError() != null && exception.getError() instanceof AgentException && ((AgentException) exception.getError()).getError() == AgentError.NoTrustedRecipients) {
StringBuilder rejectedRecips = new StringBuilder();
int cnt = 0;
for (NHINDAddress address : originalRecipList) {
rejectedRecips.append(address.getAddress());
if (++cnt < originalRecipList.size())
rejectedRecips.append(", ");
}
contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_RECIPIENTS_CONTEXT, rejectedRecips.toString()));
}
}
}
auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.REJECTED_MESSAGE_NAME, AuditEvents.EVENT_TYPE), contexts);
}
LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
throw new SmtpAgentException(SmtpAgentError.Unknown, e);
}
LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
return retVal;
} finally {
GatewayState.getInstance().unlockFromProcessing();
}
}
use of org.nhindirect.stagent.DefaultMessageEnvelope 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());
}
use of org.nhindirect.stagent.DefaultMessageEnvelope 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());
}
Aggregations