Search in sources :

Example 1 with DispositionNotification

use of com.sun.mail.dsn.DispositionNotification 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 2 with DispositionNotification

use of com.sun.mail.dsn.DispositionNotification in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertGetParts.

public void testCreateNotification_AssertGetParts() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    ArrayList<MimeEntity> entities = (ArrayList<MimeEntity>) noti.getParts();
    MimeEntity entity = entities.get(0);
    assertEquals("Your message was successfully processed.", entity.getContent().toString());
    entity = entities.get(1);
    assertTrue(entity.getContentType().startsWith("message/disposition-notification"));
    DispositionNotification notification = (DispositionNotification) entity.getContent();
    assertEquals(notification.getNotifications().getHeader("disposition", ","), "automatic-action/MDN-sent-automatically;processed");
}
Also used : DispositionNotification(com.sun.mail.dsn.DispositionNotification) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 3 with DispositionNotification

use of com.sun.mail.dsn.DispositionNotification in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertDispatched.

public void testCreateNotification_AssertDispatched() throws Exception {
    Notification noti = new Notification(NotificationType.Dispatched);
    ArrayList<MimeEntity> entities = (ArrayList<MimeEntity>) noti.getParts();
    MimeEntity entity = entities.get(0);
    assertEquals("Your message was successfully processed.", entity.getContent().toString());
    entity = entities.get(1);
    assertTrue(entity.getContentType().startsWith("message/disposition-notification"));
    DispositionNotification notification = (DispositionNotification) entity.getContent();
    assertEquals(notification.getNotifications().getHeader("disposition", ","), "automatic-action/MDN-sent-automatically;dispatched");
}
Also used : DispositionNotification(com.sun.mail.dsn.DispositionNotification) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 4 with DispositionNotification

use of com.sun.mail.dsn.DispositionNotification in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertInputStream.

public void testCreateNotification_AssertInputStream() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    ByteArrayDataSource dataSource = new ByteArrayDataSource(noti.getInputStream(), noti.getAsMultipart().getContentType());
    MimeMultipart mm = new MimeMultipart(dataSource);
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    BodyPart part = mm.getBodyPart(0);
    assertTrue(part.getContentType().startsWith("text/plain"));
    assertEquals("Your message was successfully processed.", part.getContent().toString());
    part = mm.getBodyPart(1);
    assertTrue(part.getContentType().startsWith("message/disposition-notification"));
    DispositionNotification notification = (DispositionNotification) part.getContent();
    assertEquals(notification.getNotifications().getHeader("disposition", ","), "automatic-action/MDN-sent-automatically;processed");
}
Also used : BodyPart(javax.mail.BodyPart) DispositionNotification(com.sun.mail.dsn.DispositionNotification) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Aggregations

DispositionNotification (com.sun.mail.dsn.DispositionNotification)4 ArrayList (java.util.ArrayList)2 BodyPart (javax.mail.BodyPart)2 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)2 InternetHeaders (javax.mail.internet.InternetHeaders)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1 NHINDException (org.nhindirect.stagent.NHINDException)1