use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class MimeTest method semiColonAddressSeparator.
@Test
public void semiColonAddressSeparator() throws Exception {
StringBuilder to = new StringBuilder("To: ");
int count = 4;
for (int i = 1; i < count + 1; i++) {
to.append("user").append(count).append("@example.com").append(";");
}
to.setLength(to.length() - 1);
to.append("\r\n");
String content = "From: user1@example.com\r\n" + to.toString() + "Subject: test\r\n" + "Content-Type: test/plain\r\n\r\n" + "test message";
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
Address[] recipients = mm.getAllRecipients();
Assert.assertEquals(count, recipients.length);
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class ParseMimeMessage method setContent.
/**
* The <mp>'s from the client and the MimeBodyParts in alternatives[] all want to be "content"
* of this MimeMessage. The alternatives[] all need to be "alternative" to whatever the client sends
* us....but we want to be careful so that we do NOT create a nested multipart/alternative structure
* within another one (that would be very tacky)....so this is a bit complicated.
*/
private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt) throws MessagingException, ServiceException, IOException {
String type = elem.getAttribute(MailConstants.A_CONTENT_TYPE, MimeConstants.CT_DEFAULT).trim();
ContentType ctype = new ContentType(type, ctxt.use2231).cleanup();
// is the client passing us a multipart?
if (ctype.getPrimaryType().equals("multipart")) {
// handle multipart content separately...
setMultipartContent(ctype, mm, mmp, elem, alternatives, ctxt);
return;
}
Element inline = elem.getOptionalElement(MailConstants.E_ATTACH);
if (inline != null) {
handleAttachments(inline, mmp, ctxt, elem.getAttribute(MailConstants.A_CONTENT_ID, null), Part.INLINE);
return;
}
if (alternatives != null) {
// create a multipart/alternative to hold all the alternatives
MimeMultipart mmpNew = new ZMimeMultipart("alternative");
if (mmp == null) {
mm.setContent(mmpNew);
} else {
MimeBodyPart mbpWrapper = new ZMimeBodyPart();
mbpWrapper.setContent(mmpNew);
mmp.addBodyPart(mbpWrapper);
}
mmp = mmpNew;
}
// once we get here, mmp is either NULL, a multipart/mixed from the toplevel,
// or a multipart/alternative created just above....either way we are safe to stick
// the client's nice and simple body right here
String text = elem.getAttribute(MailConstants.E_CONTENT, "");
byte[] raw = text.getBytes(Charsets.UTF_8);
if (raw.length > 0 || !LC.mime_exclude_empty_content.booleanValue() || ctype.getPrimaryType().equals("text")) {
ctxt.incrementSize("message body", raw.length);
// if the user has specified an alternative charset, make sure it exists and can encode the content
String charset = CharsetUtil.checkCharset(text, ctxt.defaultCharset);
ctype.setCharset(charset).setParameter(MimeConstants.P_CHARSET, charset);
Object content = ctype.getContentType().equals(ContentType.MESSAGE_RFC822) ? new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw)) : text;
if (mmp != null) {
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setContent(content, ctype.toString());
mmp.addBodyPart(mbp);
} else {
mm.setContent(content, ctype.toString());
}
}
if (alternatives != null) {
for (int i = 0; i < alternatives.length; i++) {
ctxt.incrementSize("alternative body", alternatives[i].getSize());
mmp.addBodyPart(alternatives[i]);
}
}
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestUserServlet method verifyTarball.
private void verifyTarball(ZMailbox mbox, String relativePath, boolean hasMeta, boolean hasBody) throws Exception {
InputStream in = mbox.getRESTResource(relativePath);
TarInputStream tarIn = new TarInputStream(new GZIPInputStream(in), "UTF-8");
TarEntry entry = null;
boolean foundMeta = false;
boolean foundMessage = false;
while ((entry = tarIn.getNextEntry()) != null) {
if (entry.getName().endsWith(".meta")) {
Assert.assertTrue("Fround " + entry.getName(), hasMeta);
foundMeta = true;
}
if (entry.getName().endsWith(".eml")) {
byte[] content = new byte[(int) entry.getSize()];
Assert.assertEquals(content.length, tarIn.read(content));
MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
if (hasBody) {
Assert.assertTrue(entry.getName() + " has no body", body.length > 0);
} else {
Assert.assertEquals(entry.getName() + " has a body", 0, body.length);
}
foundMessage = true;
}
}
tarIn.close();
Assert.assertTrue(foundMessage);
if (hasMeta) {
Assert.assertTrue(foundMeta);
}
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestUserServlet method verifyZipFile.
private void verifyZipFile(ZMailbox mbox, String relativePath, boolean hasBody) throws Exception {
InputStream in = mbox.getRESTResource(relativePath);
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry entry;
boolean foundMessage = false;
while ((entry = zipIn.getNextEntry()) != null) {
if (entry.getName().endsWith(".eml")) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ByteUtil.copy(zipIn, false, buf, true);
byte[] content = buf.toByteArray();
MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
if (hasBody) {
Assert.assertTrue(entry.getName() + " has no body", body.length > 0);
} else {
Assert.assertEquals(entry.getName() + " has a body", 0, body.length);
}
foundMessage = true;
}
}
zipIn.close();
Assert.assertTrue(foundMessage);
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestUtil method getHeaderValue.
public static String getHeaderValue(ZMailbox mbox, ZMessage msg, String headerName) throws Exception {
String content = msg.getContent();
if (content == null) {
content = getContent(mbox, msg.getId());
}
assertNotNull("Content was not fetched from the server", content);
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
return mimeMsg.getHeader(headerName, null);
}
Aggregations