use of javax.mail.Address 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);
}
}
}
use of javax.mail.Address in project nhin-d by DirectProject.
the class DefaultMimeXdsTransformer method getSubmissionSet.
/*
* Metadata Attribute XDS Minimal Metadata
* -----------------------------------------------------
* author R2 R
* contentTypeCode R R2
* entryUUID R R
* intendedRecipient O R
* patientId R R2
* sourceId R R
* submissionTime R R
* title O O
* uniqueId R R
*/
private DirectDocuments.SubmissionSet getSubmissionSet(String subject, Date sentDate, String auth, Address[] recipients, byte[] xdsDocument, DirectDocumentType documentType) throws Exception {
DirectDocuments.SubmissionSet submissionSet = new DirectDocuments.SubmissionSet();
// (R) Minimal Metadata Source
// TODO: format this correctly
submissionSet.setAuthorTelecommunication(auth);
// TODO: "UUID URN mapped by configuration to sending organization"
submissionSet.setSourceId("TODO");
submissionSet.setSubmissionTime(sentDate);
submissionSet.setUniqueId(UUID.randomUUID().toString());
for (Address address : recipients) {
submissionSet.getIntendedRecipient().add("||^^Internet^" + address.toString());
}
// (R2) Minimal Metadata Source
// --
// (O) Minimal Metadata Source
// TODO: title (subject)
// Additional metadata from document parsing
documentType.parse(new String(xdsDocument), submissionSet);
return submissionSet;
}
use of javax.mail.Address in project opennms by OpenNMS.
the class JavaMailAckReaderIT method workingWithMultiPartMessages.
/**
* tests the ability to create acknowledgments from an email for a multi-part text. This test
* creates a message from scratch rather than reading from an inbox. This message creation
* may not actually represent what comes from a mail server.
*/
@Test
public void workingWithMultiPartMessages() throws JavaMailerException, MessagingException {
List<Message> msgs = new ArrayList<Message>();
Properties props = new Properties();
Message msg = new MimeMessage(Session.getDefaultInstance(props));
Address[] addrs = new Address[1];
addrs[0] = new InternetAddress("david@opennms.org");
msg.addFrom(addrs);
msg.addRecipient(RecipientType.TO, new InternetAddress("david@opennms.org"));
msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
Multipart mpContent = new MimeMultipart();
BodyPart textBp = new MimeBodyPart();
BodyPart htmlBp = new MimeBodyPart();
textBp.setText("ack");
htmlBp.setContent("<html>\n" + " <head>\n" + " <title>\n" + " Acknowledge\n" + " </title>\n" + " </head>\n" + " <body>\n" + " <h1>\n" + " ack\n" + " </h1>\n" + " </body>\n" + "</html>", "text/html");
mpContent.addBodyPart(textBp);
mpContent.addBodyPart(htmlBp);
msg.setContent(mpContent);
msgs.add(msg);
List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
Assert.assertEquals(1, acks.size());
Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
use of javax.mail.Address in project opennms by OpenNMS.
the class JavaMailAckReaderIT method workingWithSimpleTextMessages.
/**
* tests the ability to create acknowledgments from an email for plain text. This test
* creates a message from scratch rather than reading from an inbox.
*/
@Test
public void workingWithSimpleTextMessages() {
Properties props = new Properties();
Message msg = new MimeMessage(Session.getDefaultInstance(props));
try {
Address[] addrs = new Address[1];
addrs[0] = new InternetAddress("david@opennms.org");
msg.addFrom(addrs);
msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, addrs[0]);
msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
msg.setText("ACK");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
List<Message> msgs = new ArrayList<Message>(1);
msgs.add(msg);
List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
Assert.assertEquals(1, acks.size());
Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
use of javax.mail.Address in project Xponents by OpenSextant.
the class MessageConverter method setMailAttributes.
/**
* Copy innate Message metadata into the ConvertedDocument properties to save that metadata in the normal place.
* This metadata will also be replicated down through children items to reflect the fact the attachment was sent via this message.
*
* @param msgdoc doc conversion
* @param message original mail message
* @throws MessagingException on err
*/
private void setMailAttributes(ConvertedDocument msgdoc, Message message) throws MessagingException {
String msg_id = getMessageID(message);
if (msg_id == null) {
return;
}
msgdoc.id = getShorterMessageID(msg_id);
String mailSubj = message.getSubject();
msgdoc.addTitle(mailSubj);
Address[] sender = message.getFrom();
String sender0 = null;
if (sender != null && sender.length > 0) {
sender0 = sender[0].toString();
msgdoc.addAuthor(sender0);
}
Date d = message.getSentDate();
String dt = (d != null ? d.toString() : "");
msgdoc.addCreateDate(d != null ? d : msgdoc.filetime);
msgdoc.addUserProperty(MAIL_KEY_PREFIX + "msgid", msg_id);
msgdoc.addUserProperty(MAIL_KEY_PREFIX + "sender", sender0);
msgdoc.addUserProperty(MAIL_KEY_PREFIX + "date", dt);
msgdoc.addUserProperty(MAIL_KEY_PREFIX + "subject", mailSubj);
}
Aggregations