use of com.sun.mail.dsn.DeliveryStatus in project nhin-d by DirectProject.
the class DSNGenerator method createDSNMessage.
/**
* Creates a DSN message message.
* @param originalSender The original sender of the message
* @param originalSubject The subject of the original message
* @param postmaster The postmaster address that the DSN message will be from
* @param recipientDSNHeaders A list of recipient DSN headers to populate the delivery status part of the DSN message
* @param messageDSNHeaders The message DSN headers to populate the delivery status part of the DSN message
* @param humanReadableText The human readable part (the first part) or the DSN message
* @return A mime message containing the full DSN message
* @throws MessagingException
*/
public MimeMessage createDSNMessage(InternetAddress originalSender, String originalSubject, InternetAddress postmaster, List<DSNRecipientHeaders> recipientDSNHeaders, DSNMessageHeaders messageDSNHeaders, MimeBodyPart humanReadableText) throws MessagingException {
final DeliveryStatus deliveryStatus = createDeliveryStatus(recipientDSNHeaders, messageDSNHeaders);
// assemble multipart report
final MultipartReport multipartReport = new MultipartReport("", deliveryStatus);
// set name of the delivery status file
multipartReport.getBodyPart(DELIVERY_STATUS_MULTIPART_INDEX).setFileName("status.dat");
// set text body part
multipartReport.setTextBodyPart(humanReadableText);
// create mime message to send from the MultipartReport
final Properties properties = new Properties();
properties.setProperty("mail.from", postmaster.getAddress());
final Session session = Session.getInstance(properties);
final MimeMessage destinationMessage = new MimeMessage(session);
destinationMessage.setSentDate(Calendar.getInstance().getTime());
destinationMessage.setContent(multipartReport);
destinationMessage.setFrom(postmaster);
destinationMessage.addRecipient(RecipientType.TO, originalSender);
destinationMessage.setSubject(subjectPrefix + originalSubject);
destinationMessage.setHeader(MailStandard.Headers.InReplyTo, messageDSNHeaders.getOriginalMessageId());
destinationMessage.saveChanges();
return destinationMessage;
}
use of com.sun.mail.dsn.DeliveryStatus in project nhin-d by DirectProject.
the class DefaultTxDetailParser method getMessageDetails.
@SuppressWarnings("incomplete-switch")
@Override
public Map<String, TxDetail> getMessageDetails(MimeMessage msg) {
Map<String, TxDetail> retVal = new HashMap<String, TxDetail>();
// get the message id
final String msgId = MailStandard.getHeader(msg, MailStandard.Headers.MessageID);
if (!msgId.isEmpty())
retVal.put(TxDetailType.MSG_ID.getType(), new TxDetail(TxDetailType.MSG_ID.getType(), msgId));
// get the subject
final String subject = MailStandard.getHeader(msg, MailStandard.Headers.Subject);
if (!subject.isEmpty())
retVal.put(TxDetailType.SUBJECT.getType(), new TxDetail(TxDetailType.SUBJECT.getType(), subject));
// get the full headers as a string
final String fullHeaders = getHeadersAsStringInternal(msg);
if (!fullHeaders.isEmpty())
retVal.put(TxDetailType.MSG_FULL_HEADERS.getType(), new TxDetail(TxDetailType.MSG_FULL_HEADERS.getType(), fullHeaders));
// get the from addresses
try {
final String from = MailStandard.getHeader(msg, MailStandard.Headers.From);
if (!from.isEmpty()) {
StringBuilder builder = new StringBuilder();
int cnt = 0;
for (InternetAddress addr : (InternetAddress[]) msg.getFrom()) {
// comma delimit multiple addresses
if (cnt > 0)
builder.append(",");
builder.append(addr.getAddress().toLowerCase(Locale.getDefault()));
++cnt;
}
retVal.put(TxDetailType.FROM.getType(), new TxDetail(TxDetailType.FROM.getType(), builder.toString()));
}
}/// CLOVER:OFF
catch (MessagingException e) {
LOGGER.warn("Failed to retrieve message sender list.", e);
}
// get the sender if it exists
try {
final InternetAddress sender = (InternetAddress) msg.getSender();
if (sender != null)
retVal.put(TxDetailType.SENDER.getType(), new TxDetail(TxDetailType.SENDER.toString(), sender.getAddress().toLowerCase(Locale.getDefault())));
}/// CLOVER:OFF
catch (MessagingException e) {
LOGGER.warn("Failed to retrieve message sender", e);
}
// get the recipient addresses
try {
if (msg.getAllRecipients() != null) {
StringBuilder builder = new StringBuilder();
int cnt = 0;
for (Address addr : msg.getAllRecipients()) {
// comma delimit multiple addresses
if (cnt > 0)
builder.append(",");
if (addr instanceof InternetAddress)
builder.append(((InternetAddress) addr).getAddress().toLowerCase(Locale.getDefault()));
++cnt;
}
retVal.put(TxDetailType.RECIPIENTS.getType(), new TxDetail(TxDetailType.RECIPIENTS.getType(), builder.toString()));
}
}/// CLOVER:OFF
catch (MessagingException e) {
LOGGER.warn("Failed to retrieve message recipient list.", e);
}
/// CLOVER:ON
// get the message type
final TxMessageType messageType = TxUtil.getMessageType(msg);
if (messageType != TxMessageType.UNKNOWN) {
switch(messageType) {
case MDN:
{
// the disposition if a field in the second part of the MDN message
final String disposition = MDNStandard.getMDNField(msg, MDNStandard.Headers.Disposition);
if (!disposition.isEmpty())
retVal.put(TxDetailType.DISPOSITION.getType(), new TxDetail(TxDetailType.DISPOSITION.getType(), disposition.toLowerCase(Locale.getDefault())));
// the final recipients is a field in the second part of the MDN message
final String finalRecipient = MDNStandard.getMDNField(msg, MDNStandard.Headers.FinalRecipient);
if (!finalRecipient.isEmpty())
retVal.put(TxDetailType.FINAL_RECIPIENTS.getType(), new TxDetail(TxDetailType.FINAL_RECIPIENTS.getType(), finalRecipient.toLowerCase(Locale.getDefault())));
// the original message id if a field in the second part of the MDN message
String origMsgId = MDNStandard.getMDNField(msg, MDNStandard.Headers.OriginalMessageID);
if (origMsgId.isEmpty()) {
// it might be in a reply to header
origMsgId = MailStandard.getHeader(msg, MailStandard.Headers.InReplyTo);
}
if (!origMsgId.isEmpty())
retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID.getType(), origMsgId));
// check for X-DIRECT-FINAL-DESTINATION-DELIVER extension
try {
final InternetHeaders mdnHeaders = MDNStandard.getNotificationFieldsAsHeaders(msg);
if (mdnHeaders.getHeader(MDNStandard.DispositionOption_TimelyAndReliable, ",") != null) {
retVal.put(TxDetailType.DISPOSITION_OPTIONS.getType(), new TxDetail(TxDetailType.DISPOSITION_OPTIONS.getType(), MDNStandard.DispositionOption_TimelyAndReliable));
}
}// CLOVER:OFF
catch (Exception e) {
LOGGER.warn("Failed to retrieve MDN headers from message. Message may not be an MDN message.", e);
}
// CLOVER:ON
break;
}
case DSN:
{
// the Original-Envelope-ID header does not reflect the message id
try {
final DeliveryStatus status = new DeliveryStatus(new ByteArrayInputStream(MailUtil.serializeToBytes(msg)));
retVal.put(TxDetailType.FINAL_RECIPIENTS.getType(), new TxDetail(TxDetailType.FINAL_RECIPIENTS.getType(), DSNStandard.getFinalRecipients(status).toLowerCase(Locale.getDefault())));
// check at the message level
boolean parentFound = false;
final String origMsgId = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.OriginalMessageID);
if (!origMsgId.isEmpty()) {
parentFound = true;
retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID, origMsgId));
}
if (!parentFound) {
// it might be in a reply to header
final String parentMsgId = MailStandard.getHeader(msg, MailStandard.Headers.InReplyTo);
if (!parentMsgId.isEmpty())
retVal.put(TxDetailType.PARENT_MSG_ID.getType(), new TxDetail(TxDetailType.PARENT_MSG_ID.getType(), parentMsgId));
}
// get the action
final String action = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.Action);
if (!action.isEmpty())
retVal.put(TxDetailType.DSN_ACTION.getType(), new TxDetail(TxDetailType.DSN_ACTION.getType(), action.toLowerCase(Locale.getDefault())));
// get the status
final String dsnStatus = DSNStandard.getHeaderValueFromDeliveryStatus(status, DSNStandard.Headers.Status);
if (!dsnStatus.isEmpty())
retVal.put(TxDetailType.DSN_STATUS.getType(), new TxDetail(TxDetailType.DSN_STATUS.getType(), dsnStatus.toLowerCase(Locale.getDefault())));
}///CLOVER:OFF
catch (Exception e) {
LOGGER.warn("Could not get a requested field from the DSN message", e);
}
///CLOVER:ON
break;
}
}
}
// check for the existence of disposition request options
final String dispOption = MailStandard.getHeader(msg, MDNStandard.Headers.DispositionNotificationOptions);
if (!dispOption.isEmpty())
retVal.put(TxDetailType.DISPOSITION_OPTIONS.getType(), new TxDetail(TxDetailType.DISPOSITION_OPTIONS.getType(), dispOption.toLowerCase(Locale.getDefault())));
return retVal;
}
use of com.sun.mail.dsn.DeliveryStatus in project nhin-d by DirectProject.
the class DSNGenerator method createDeliveryStatus.
/**
* Creates the deliver status section of the message
* @param recipientDSNHeaders
* @param messageDSNHeaders
* @return
* @throws MessagingException
*/
protected DeliveryStatus createDeliveryStatus(List<DSNRecipientHeaders> recipientDSNHeaders, DSNMessageHeaders messageDSNHeaders) throws MessagingException {
final DeliveryStatus deliveryStatus = new DeliveryStatus();
for (DSNRecipientHeaders dsnHeaders : recipientDSNHeaders) {
final InternetHeaders recipHeaders = new InternetHeaders();
final DSNAction dsnAction = dsnHeaders.getAction();
recipHeaders.addHeader(DSNStandard.Headers.Action, "" + dsnAction);
final Address finalRecipient = dsnHeaders.getFinalRecipient();
if (finalRecipient != null) {
recipHeaders.addHeader(DSNStandard.Headers.FinalRecipient, finalRecipient.getType() + ";" + finalRecipient.toString());
}
String status = dsnHeaders.getStatus();
recipHeaders.addHeader(DSNStandard.Headers.Status, status);
deliveryStatus.addRecipientDSN(recipHeaders);
}
final InternetHeaders messageHeaders = new InternetHeaders();
final MtaNameType mtaNameType = messageDSNHeaders.getMtaNameType();
final String reportingMta = messageDSNHeaders.getReportingMta();
messageHeaders.addHeader(DSNStandard.Headers.ReportingMTA, mtaNameType + ";" + reportingMta);
// add a custom header with original message id
final String originalMessageId = messageDSNHeaders.getOriginalMessageId();
messageHeaders.addHeader(DSNStandard.Headers.OriginalMessageID, originalMessageId);
deliveryStatus.setMessageDSN(messageHeaders);
return deliveryStatus;
}
Aggregations