use of org.nhindirect.stagent.NHINDAddressCollection in project nhin-d by DirectProject.
the class TimelyAndReliableLocalDelivery method service.
/**
* {@inheritDoc}
*/
@Override
public void service(Mail mail) throws MessagingException {
LOGGER.debug("Calling timely and reliable service method.");
boolean deliverySuccessful = false;
final MimeMessage msg = mail.getMessage();
final boolean isReliableAndTimely = TxUtil.isReliableAndTimelyRequested(msg);
final NHINDAddressCollection recipients = getMailRecipients(mail);
final NHINDAddress sender = getMailSender(mail);
try {
serviceMethod.invoke(localDeliveryMailet, mail);
deliverySuccessful = true;
} catch (Exception e) {
LOGGER.error("Failed to invoke service method.", e);
}
final Tx txToTrack = this.getTxToTrack(msg, sender, recipients);
if (deliverySuccessful) {
if (isReliableAndTimely && txToTrack.getMsgType() == TxMessageType.IMF) {
// send back an MDN dispatched message
final Collection<NotificationMessage> notifications = notificationProducer.produce(new Message(msg), recipients.toInternetAddressCollection());
if (notifications != null && notifications.size() > 0) {
LOGGER.debug("Sending MDN \"dispatched\" messages");
// create a message for each notification and put it on James "stack"
for (NotificationMessage message : notifications) {
try {
message.saveChanges();
if (dispatchedMDNDelay > 0)
Thread.sleep(dispatchedMDNDelay);
getMailetContext().sendMail(message);
}///CLOVER:OFF
catch (Throwable t) {
// don't kill the process if this fails
LOGGER.error("Error sending MDN dispatched message.", t);
}
///CLOVER:ON
}
}
}
} else {
// create a DSN message regarless if timely and reliable was requested
if (txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF)
this.sendDSN(txToTrack, recipients, false);
}
LOGGER.debug("Exiting timely and reliable service method.");
}
use of org.nhindirect.stagent.NHINDAddressCollection 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;
}
use of org.nhindirect.stagent.NHINDAddressCollection in project nhin-d by DirectProject.
the class AbstractDSNCreator method groupPostMasterAndFailedRecips.
/**
* Groups postmasters with the proper list of failed recipients
* @param sender The sender of the original message
* @param failedRecipeints List of all failed recipients
* @param useSenderAsPostmaster Indicates if the sender's domain should be used as the postmaster domain.
* @return Map grouping a collection of failed recipients with a postmaster. If the useSenderAsPostmaster flag is true, all failed recipients with be associated
* with one postmaster (using the senders domain). Otherwise, a postmaster address is created per unique failed recipient domain and failed recipients are grouped by domain.
*/
protected Map<InternetAddress, Collection<NHINDAddress>> groupPostMasterAndFailedRecips(TxDetail sender, NHINDAddressCollection failedRecipeints, boolean useSenderAsPostmaster) throws MessagingException {
final Map<InternetAddress, Collection<NHINDAddress>> postmasterToFailed = new HashMap<InternetAddress, Collection<NHINDAddress>>();
if (useSenderAsPostmaster) {
// just group all failed recipients into one collection
if (sender != null) {
final InternetAddress originalSender = new InternetAddress(sender.getDetailValue());
final InternetAddress postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
postmasterToFailed.put(postmaster, failedRecipeints);
}
} else {
// group by domain
for (NHINDAddress failedRecipeint : failedRecipeints) {
// get the postmaster for the failed recip
final InternetAddress postmaster = new InternetAddress(postmasterMailbox + "@" + failedRecipeint.getHost().toLowerCase(Locale.getDefault()));
Collection<NHINDAddress> group = postmasterToFailed.get(postmaster);
if (group == null) {
group = new ArrayList<NHINDAddress>();
postmasterToFailed.put(postmaster, group);
}
group.add(failedRecipeint);
}
}
return postmasterToFailed;
}
use of org.nhindirect.stagent.NHINDAddressCollection 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());
}
use of org.nhindirect.stagent.NHINDAddressCollection 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());
}
Aggregations