use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class NotificationTest method testFailedNotification.
public void testFailedNotification() throws Exception {
Notification noti = new Notification(NotificationType.Failed);
System.out.println(noti);
MimeMultipart mm = noti.getAsMultipart();
assertNotNull(mm);
assertEquals(2, mm.getCount());
BodyPart part = mm.getBodyPart(0);
assertTrue(part.getContentType().startsWith("text/plain"));
assertEquals(Notification.DefaultExplanationFailed, part.getContent().toString());
part = mm.getBodyPart(1);
assertTrue(part.getContentType().startsWith("message/disposition-notification"));
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
part.writeTo(outStream);
String content = new String(outStream.toByteArray());
assertTrue(content.contains("automatic-action/MDN-sent-automatically;failed"));
}
use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class MDNStandard method getNotificationFieldsAsHeaders.
/**
* Parses the notification part fields of the MimeMultipart body of a MDN message. The multipart is expected to conform to the MDN specification
* as described in RFC3798.
* @return The notification part fields as a set of Internet headers.
*/
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMultipart mm) {
InternetHeaders retVal = null;
if (mm == null)
throw new IllegalArgumentException("Multipart can not be null");
try {
if (mm.getCount() < 2)
throw new IllegalArgumentException("Multipart can not be null");
// the second part should be the notification
BodyPart part = mm.getBodyPart(1);
try {
Object contecntObj = part.getContent();
if (dsnClass != null && dsnClass.getCanonicalName().equals(contecntObj.getClass().getCanonicalName())) {
retVal = (InternetHeaders) getHeaders.invoke(contecntObj);
return retVal;
}
} catch (Exception e) {
/* no-op */
}
if (!part.getContentType().equalsIgnoreCase(MDNStandard.MediaType.DispositionNotification))
throw new IllegalArgumentException("Notification part content type is not " + MDNStandard.MediaType.DispositionNotification);
// parse fields
retVal = new InternetHeaders();
String[] fields = getPartContentBodyAsString(part).split("\r\n");
for (String field : fields) {
int idx = field.indexOf(":");
if (idx > -1) {
String name = field.substring(0, idx);
String value = field.substring(idx + 1).trim();
retVal.setHeader(name, value);
}
}
} catch (MessagingException e) {
throw new IllegalArgumentException("Failed to parse notification fields.", e);
}
return retVal;
}
use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class MDNFactory_createTest method getNotificationFieldsAsHeaders.
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMultipart mm) {
InternetHeaders retVal = null;
if (mm == null)
throw new IllegalArgumentException("Multipart can not be null");
try {
if (mm.getCount() < 2)
throw new IllegalArgumentException("Multipart can not be null");
// the second part should be the notification
BodyPart part = mm.getBodyPart(1);
if (part.getContent() instanceof DispositionNotification) {
return ((DispositionNotification) part.getContent()).getNotifications();
}
// parse fields
retVal = new InternetHeaders();
String[] fields = Notification.getPartContentBodyAsString(part).split("\r\n");
for (String field : fields) {
int idx = field.indexOf(":");
if (idx > -1) {
String name = field.substring(0, idx);
String value = field.substring(idx + 1).trim();
retVal.setHeader(name, value);
}
}
} catch (Exception e) {
throw new NHINDException("Failed to parse notification fields.", e);
}
return retVal;
}
use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class MDNFactory_createTest method testCreate_withGernalAttributes.
public void testCreate_withGernalAttributes() throws Exception {
final Disposition disp = new Disposition(NotificationType.Processed);
final MdnGateway gateway = new MdnGateway("junitGateway");
MimeMultipartReport report = MDNFactory.create("test", "junitUA", "junitProduct", "sender@send.com", "final@final.com", "12345", "junit error", gateway, disp, "", "", new ArrayList<String>());
assertNotNull(report);
final InternetHeaders headers = getNotificationFieldsAsHeaders(report);
assertTrue(headers.getHeader(MDNStandard.Headers.ReportingAgent, ",").startsWith("junitUA"));
assertTrue(headers.getHeader(MDNStandard.Headers.ReportingAgent, ",").endsWith("junitProduct"));
assertEquals("rfc822; sender@send.com", headers.getHeader(MDNStandard.Headers.OriginalRecipeint, ","));
assertEquals("rfc822; final@final.com", headers.getHeader(MDNStandard.Headers.FinalRecipient, ","));
assertTrue(headers.getHeader(MDNStandard.Headers.Gateway, ",").endsWith("junitGateway"));
assertTrue(headers.getHeader(MDNStandard.Headers.Disposition, ",").endsWith(NotificationType.Processed.toString()));
BodyPart part0 = report.getBodyPart(0);
Object obj = part0.getContent();
assertEquals("test", obj);
}
use of javax.mail.BodyPart in project nhin-d by DirectProject.
the class NotificationTest method testError_AssertError.
public void testError_AssertError() throws Exception {
Notification noti = new Notification(NotificationType.Processed);
MimeMultipart mm = noti.getAsMultipart();
assertNotNull(mm);
assertEquals(2, mm.getCount());
BodyPart part = mm.getBodyPart(1);
assertTrue(part.getContentType().startsWith("message/disposition-notification"));
InternetHeaders headers = noti.getNotificationFieldsAsHeaders();
assertNull(headers.getHeader(MDNStandard.Headers.Error));
// set a new gateway
noti.setError("Junit Error");
mm = noti.getAsMultipart();
assertNotNull(mm);
assertEquals(2, mm.getCount());
part = mm.getBodyPart(1);
assertTrue(part.getContentType().startsWith("message/disposition-notification"));
headers = noti.getNotificationFieldsAsHeaders();
assertNotNull(headers.getHeader(MDNStandard.Headers.Error));
assertEquals("Junit Error", headers.getHeader(MDNStandard.Headers.Error, ","));
assertEquals(headers.getHeader(MDNStandard.Headers.Error, ","), noti.getError());
}
Aggregations