Search in sources :

Example 61 with BodyPart

use of javax.mail.BodyPart in project jmeter by apache.

the class MailReaderSampler method appendMultiPart.

private void appendMultiPart(SampleResult child, StringBuilder cdata, MimeMultipart mmp) throws MessagingException, IOException {
    String preamble = mmp.getPreamble();
    if (preamble != null) {
        cdata.append(preamble);
    }
    child.setResponseData(cdata.toString(), child.getDataEncodingNoDefault());
    int count = mmp.getCount();
    for (int j = 0; j < count; j++) {
        BodyPart bodyPart = mmp.getBodyPart(j);
        final Object bodyPartContent = bodyPart.getContent();
        final String contentType = bodyPart.getContentType();
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("Part: " + j);
        sr.setContentType(contentType);
        sr.setDataEncoding(RFC_822_DEFAULT_ENCODING);
        sr.setEncodingAndType(contentType);
        sr.sampleStart();
        if (bodyPartContent instanceof InputStream) {
            sr.setResponseData(IOUtils.toByteArray((InputStream) bodyPartContent));
        } else if (bodyPartContent instanceof MimeMultipart) {
            appendMultiPart(sr, cdata, (MimeMultipart) bodyPartContent);
        } else {
            sr.setResponseData(bodyPartContent.toString(), sr.getDataEncodingNoDefault());
        }
        sr.setResponseOK();
        if (sr.getEndTime() == 0) {
            // not been set by any child samples
            sr.sampleEnd();
        }
        child.addSubResult(sr);
    }
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) InputStream(java.io.InputStream) SampleResult(org.apache.jmeter.samplers.SampleResult)

Example 62 with BodyPart

use of javax.mail.BodyPart in project jmeter by apache.

the class SmtpSampler method getSamplerData.

private String getSamplerData(Message message) throws MessagingException, IOException {
    StringBuilder sb = new StringBuilder();
    // throws ME
    Object content = message.getContent();
    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        String contentType = multipart.getContentType();
        ContentType ct = new ContentType(contentType);
        String boundary = ct.getParameter("boundary");
        for (int i = 0; i < multipart.getCount(); i++) {
            // throws ME
            sb.append("--");
            sb.append(boundary);
            sb.append("\n");
            // throws ME
            BodyPart bodyPart = multipart.getBodyPart(i);
            // throws IOE, ME
            writeBodyPart(sb, bodyPart);
        }
        sb.append("--");
        sb.append(boundary);
        sb.append("--");
        sb.append("\n");
    } else if (content instanceof BodyPart) {
        BodyPart bodyPart = (BodyPart) content;
        // throws IOE, ME
        writeBodyPart(sb, bodyPart);
    } else if (content instanceof String) {
        sb.append(content);
    } else {
        sb.append("Content has class: ");
        sb.append(content.getClass().getCanonicalName());
    }
    return sb.toString();
}
Also used : BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ContentType(javax.mail.internet.ContentType)

Example 63 with BodyPart

use of javax.mail.BodyPart in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertMultipart.

public void testCreateNotification_AssertMultipart() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    MimeMultipart mm = noti.getAsMultipart();
    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"));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    part.writeTo(outStream);
    String content = new String(outStream.toByteArray());
    assertTrue(content.contains("automatic-action/MDN-sent-automatically;processed"));
}
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 64 with BodyPart

use of javax.mail.BodyPart 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)

Example 65 with BodyPart

use of javax.mail.BodyPart in project nhin-d by DirectProject.

the class NotificationTest method testOriginalMessageId_AssertOriginalMessageId.

public void testOriginalMessageId_AssertOriginalMessageId() 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.OriginalMessageID));
    // set a new gateway
    noti.setOriginalMessageId("Orig Msg Id");
    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.OriginalMessageID));
    assertEquals("Orig Msg Id", headers.getHeader(MDNStandard.Headers.OriginalMessageID, ","));
    assertEquals(headers.getHeader(MDNStandard.Headers.OriginalMessageID, ","), noti.getOriginalMessageId());
}
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)77 MimeMultipart (javax.mail.internet.MimeMultipart)57 MimeBodyPart (javax.mail.internet.MimeBodyPart)46 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)21 Multipart (javax.mail.Multipart)16 Header (javax.mail.Header)15 IOException (java.io.IOException)14 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 HashMap (java.util.HashMap)13 DataHandler (javax.activation.DataHandler)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 Test (org.testng.annotations.Test)10 FileDataSource (javax.activation.FileDataSource)9 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