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);
}
}
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();
}
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"));
}
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");
}
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());
}
Aggregations