Search in sources :

Example 11 with BodyPart

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"));
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 12 with BodyPart

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;
}
Also used : BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders) MessagingException(javax.mail.MessagingException) MessagingException(javax.mail.MessagingException)

Example 13 with BodyPart

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;
}
Also used : BodyPart(javax.mail.BodyPart) DispositionNotification(com.sun.mail.dsn.DispositionNotification) InternetHeaders(javax.mail.internet.InternetHeaders) NHINDException(org.nhindirect.stagent.NHINDException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 14 with BodyPart

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);
}
Also used : MimeMultipartReport(org.apache.mailet.base.mail.MimeMultipartReport) BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders)

Example 15 with BodyPart

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());
}
Also used : BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders) MimeMultipart(javax.mail.internet.MimeMultipart) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Aggregations

BodyPart (javax.mail.BodyPart)71 MimeMultipart (javax.mail.internet.MimeMultipart)52 MimeBodyPart (javax.mail.internet.MimeBodyPart)41 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)19 Header (javax.mail.Header)15 Multipart (javax.mail.Multipart)15 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 DataHandler (javax.activation.DataHandler)10 Test (org.testng.annotations.Test)10 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6 FileDataSource (javax.activation.FileDataSource)6