use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class TimelyAndReliableLocalDelivery method init.
/**
* {@inheritDoc}
*/
public void init() throws MessagingException {
super.init();
try {
final String sDispatchedDelay = GatewayConfiguration.getConfigurationParam(DISPATCHED_MDN_DELAY, this, "0");
try {
dispatchedMDNDelay = Integer.valueOf(sDispatchedDelay).intValue();
} catch (NumberFormatException e) {
// in case of parsing exceptions
dispatchedMDNDelay = 0;
}
// create an instance of the local delivery if we can
localDeliveryMailet = createLocalDeliveryClass();
final Method initMethod = Mailet.class.getDeclaredMethod("init", MailetConfig.class);
serviceMethod = Mailet.class.getDeclaredMethod("service", Mail.class);
// set private objects if they exist
final Class<?> localDeliveryMailetClass = localDeliveryMailet.getClass();
Field field = getDeclaredFieldQuietly(localDeliveryMailetClass, "rrt");
if (field != null) {
field.setAccessible(true);
field.set(localDeliveryMailet, rrt);
}
field = getDeclaredFieldQuietly(localDeliveryMailetClass, "usersRepository");
if (field != null) {
field.setAccessible(true);
field.set(localDeliveryMailet, usersRepository);
}
field = getDeclaredFieldQuietly(localDeliveryMailetClass, "mailboxManager");
if (field != null) {
field.setAccessible(true);
field.set(localDeliveryMailet, mailboxManager);
}
field = getDeclaredFieldQuietly(localDeliveryMailetClass, "domainList");
if (field != null) {
field.setAccessible(true);
field.set(localDeliveryMailet, domainList);
}
field = getDeclaredFieldQuietly(localDeliveryMailetClass, "fileSystem");
if (field != null) {
field.setAccessible(true);
field.set(localDeliveryMailet, fileSystem);
}
initMethod.invoke(localDeliveryMailet, this.getMailetConfig());
} catch (Exception e) {
throw new MessagingException("Failed to initialize TimelyAndReliableLocalDelivery.", e);
}
notificationProducer = new ReliableDispatchedNotificationProducer(new NotificationSettings(true, "Local Direct Delivery Agent", "Your message was successfully dispatched."));
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class NHINDSecurityAndTrustMailet method getSender.
public static InternetAddress getSender(Mail mail) {
InternetAddress retVal = null;
if (mail.getSender() != null)
retVal = mail.getSender().toInternetAddress();
else {
// try to get the sender from the message
Address[] senderAddr = null;
try {
if (mail.getMessage() == null)
return null;
senderAddr = mail.getMessage().getFrom();
if (senderAddr == null || senderAddr.length == 0)
return null;
} catch (MessagingException e) {
return null;
}
// not the best way to do this
retVal = (InternetAddress) senderAddr[0];
}
return retVal;
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class AbstractNotificationAwareMailet method getSender.
/**
* Gets the sender attribute of a Mail message
* @param mail The message to retrive the sender from
* @return The message sender.
*/
public static InternetAddress getSender(Mail mail) {
InternetAddress retVal = null;
if (mail.getSender() != null)
retVal = mail.getSender().toInternetAddress();
else {
// try to get the sender from the message
Address[] senderAddr = null;
try {
if (mail.getMessage() == null)
return null;
senderAddr = mail.getMessage().getFrom();
if (senderAddr == null || senderAddr.length == 0)
return null;
} catch (MessagingException e) {
return null;
}
// not the best way to do this
retVal = (InternetAddress) senderAddr[0];
}
return retVal;
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class NHINDSecurityAndTrustMailet_service_Test method testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState.
public void testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState() throws Exception {
final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
final SmtpAgent mockAgent = mock(SmtpAgent.class);
final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
when(mockMail.getRecipients()).thenReturn(null);
when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
doThrow(new RuntimeException("Just Passing Through")).when(mockAgent).processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any());
mockMail.setMessage(mimeMsg);
NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
mailet.agent = mockAgent;
boolean exceptionOccured = false;
try {
mailet.service(mockMail);
} catch (MessagingException e) {
assertEquals("Failed to process message: Just Passing Through", e.getMessage());
exceptionOccured = true;
}
assertFalse(exceptionOccured);
assertEquals(Mail.GHOST, mockMail.getState());
}
use of javax.mail.MessagingException 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);
}
}
}
Aggregations