Search in sources :

Example 1 with Base64EncoderStream

use of com.zimbra.common.zmime.ZTransferEncoding.Base64EncoderStream in project zm-mailbox by Zimbra.

the class ZMimeMultipartTest method encoded.

@Test
public void encoded() throws Exception {
    final String boundary = "dfghjkl";
    final String preamble = "when in the course of human events...\r\n";
    final String plain = "The Rain in Spain.";
    final String html = "The <u>Rain</u> in <em>Spain</em>.";
    ByteBuilder bbheader = new ByteBuilder();
    bbheader.append("From: test@example.com\r\n");
    bbheader.append("To: rcpt@example.com\r\n");
    bbheader.append("Subject: message subject\r\n");
    bbheader.append("Message-ID: <11e1-b0c4-0800200c9a66@example.com>\r\n");
    bbheader.append("Content-Transfer-Encoding: base64\r\n");
    bbheader.append("Content-Type: multipart/alternative; boundary=").append(boundary).append("\r\n");
    bbheader.append("\r\n");
    ByteBuilder bbmulti = new ByteBuilder();
    bbmulti.append(preamble);
    bbmulti.append("--").append(boundary).append("\r\n");
    bbmulti.append("Content-Type: text/plain\r\n");
    bbmulti.append("\r\n");
    bbmulti.append(plain).append("\r\n");
    bbmulti.append("--").append(boundary).append("\r\n");
    bbmulti.append("Content-Type: text/html\r\n");
    bbmulti.append("\r\n");
    bbmulti.append(html).append("\r\n");
    bbmulti.append("--").append(boundary).append("--\r\n");
    // message with CTE header and base64-encoded body
    ByteBuilder bb = new ByteBuilder();
    bb.append(bbheader);
    bb.append(ByteUtil.getContent(new Base64EncoderStream(new ByteArrayInputStream(bbmulti.toByteArray())), -1));
    Session s = Session.getDefaultInstance(new Properties());
    ZMimeMessage mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
    Object o = mm.getContent();
    Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
    ZMimeMultipart multi = (ZMimeMultipart) o;
    Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
    Assert.assertEquals("2 subparts", 2, multi.getCount());
    Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
    Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
    // message with CTE header and nonencoded body
    bb = new ByteBuilder();
    bb.append(bbheader);
    bb.append(bbmulti);
    mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
    o = mm.getContent();
    Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
    multi = (ZMimeMultipart) o;
    Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
    Assert.assertEquals("2 subparts", 2, multi.getCount());
    Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
    Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Base64EncoderStream(com.zimbra.common.zmime.ZTransferEncoding.Base64EncoderStream) Properties(java.util.Properties) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

ByteBuilder (com.zimbra.common.zmime.ZMimeUtility.ByteBuilder)1 Base64EncoderStream (com.zimbra.common.zmime.ZTransferEncoding.Base64EncoderStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Properties (java.util.Properties)1 Session (javax.mail.Session)1 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)1 Test (org.junit.Test)1