Example usage for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream

List of usage examples for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream

Introduction

In this page you can find the example usage for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream.

Prototype

public SharedByteArrayInputStream(byte[] buf) 

Source Link

Document

Create a SharedByteArrayInputStream representing the entire byte array.

Usage

From source file:com.zimbra.cs.mime.MimeTest.java

@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(";");
    }// w w w  .  ja v a  2 s.co  m
    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);
}

From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java

@Test(timeout = 3000)
public void rcptToError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO
            .sendLine("550 error").recvLine() // QUIT
            .build().start(PORT);//  w w  w. jav a2s  .  c  o m

    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.sendpartial", "true");
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException e) {
        Assert.assertEquals(0, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(1, e.getInvalidAddresses().length);
    } finally {
        transport.close();
    }

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}

From source file:com.zimbra.cs.mime.MimeTest.java

@Test
@Ignore("expensive test")
public void bigMime() throws Exception {
    String content = baseMpMixedContent + "\r\n";
    StringBuilder sb = new StringBuilder(content);
    long total = 0;
    int count = 10;
    int partCount = 100;
    int textcount = 1000000;
    int lineSize = 200;
    for (int i = 0; i < partCount; i++) {
        sb.append("------------1111971890AC3BB91\r\n")
                .append("Content-Type: text/plain; charset=windows-1250\r\n")
                .append("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
        for (int j = 0; j < textcount; j += lineSize) {
            String text = RandomStringUtils.randomAlphabetic(lineSize);
            sb.append(text).append("\r\n");
        }/*from w w  w.  j  a  v  a 2 s . co  m*/
        sb.append("\r\n");
    }
    for (int i = 0; i < count; i++) {
        long start = System.currentTimeMillis();
        MimeMessage mm =

                new Mime.FixedMimeMessage(JMSession.getSession(),
                        new SharedByteArrayInputStream(sb.toString().getBytes()));

        List<MPartInfo> parts = Mime.getParts(mm);
        long end = System.currentTimeMillis();
        total += (end - start);
        ZimbraLog.test.info("took %dms", end - start);
        Assert.assertNotNull(parts);
        Assert.assertEquals(partCount + 1, parts.size());
        MPartInfo body = Mime.getTextBody(parts, false);
        Assert.assertNotNull(body);
    }
    ZimbraLog.test.info("Avg %dms", total / count);

}

From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java

@Test(timeout = 3000)
public void endOfData() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO
            .sendLine("250 OK").recvLine() // DATA
            .sendLine("354 OK").recvUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT
            .sendLine("221 bye").build().start(PORT);

    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\n" + ".\n" + "..\n" + ".\n";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();/*  w w  w .  j  a  v a 2 s . c  o m*/

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertEquals("From: sender@zimbra.com\r\nTo: rcpt@zimbra.com\r\nSubject: test\r\n\r\n" + "..\r\n"
            + "...\r\n" + "..\r\n" + ".\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}

From source file:com.zimbra.cs.mime.MimeTest.java

@Test
public void strayBoundaryInEpilogue() throws Exception {
    String content = baseMpMixedContent + "\r\n";
    StringBuilder sb = new StringBuilder(content);
    sb.append("------------1111971890AC3BB91\r\n").append("Content-Type: text/plain; charset=windows-1250\r\n")
            .append("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
    String plainText = RandomStringUtils.randomAlphabetic(10);
    sb.append(plainText).append("\r\n");
    sb.append("------------1111971890AC3BB91--").append("\r\n");

    //this is the point of this test; if MIME has a stray boundary it used to cause NPE
    sb.append("\r\n").append("--bogusBoundary").append("\r\n").append("\r\n");

    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(),
            new SharedByteArrayInputStream(sb.toString().getBytes()));
    List<MPartInfo> parts = Mime.getParts(mm);

    Assert.assertEquals(2, parts.size());

    MPartInfo body = Mime.getTextBody(parts, false);
    Assert.assertNotNull(body);/*from   w  ww.  ja v a  2  s  .  c o  m*/

    Assert.assertTrue(TestUtil.bytesEqual(plainText.getBytes(), body.getMimePart().getInputStream()));
}

From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java

@Test(timeout = 3000)
public void dataError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO
            .sendLine("250 OK").recvLine() // DATA
            .sendLine("451 error").recvLine() // QUIT
            .build().start(PORT);/*from w w w  . java2  s . c  om*/

    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException e) {
        Assert.assertEquals(1, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(0, e.getInvalidAddresses().length);
    } finally {
        transport.close();
    }

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}

From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java

@Test(timeout = 3000)
public void dataException() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO
            .sendLine("250 OK").recvLine() // DATA
            .sendLine("354 OK").swallowUntil("\r\n.\r\n").build().start(PORT);

    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))) {
        @Override// ww w .  java2 s.com
        public void writeTo(OutputStream os, String[] ignoreList) throws MessagingException {
            throw new MessagingException(); // exception while encoding
        }
    };
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException expected) {
    } finally {
        transport.close();
    }

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertNull(server.replay());
}

From source file:com.zimbra.cs.mime.MimeTest.java

@Test
public void testFullContentType() throws Exception {
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n"
            + "Content-Type: text/plain;param=foo\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n"
            + "R0a1231312ad124svsdsal=="; //obviously not a real file
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(),
            new SharedByteArrayInputStream(content.getBytes()));

    MimePart part = Mime.getMimePart(mm, "1");
    Assert.assertEquals("text/plain;param=foo", part.getContentType());
    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);/*from w w  w . j a v  a  2 s .  c o  m*/
    Assert.assertEquals(1, parts.size());
    MPartInfo info = parts.get(0);
    Assert.assertEquals("text/plain", info.getContentType());
    Assert.assertEquals("text/plain;param=foo", info.getFullContentType());
}

From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java

@Test(timeout = 3000)
public void quitNoResponse() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO
            .sendLine("250 OK").recvLine() // DATA
            .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT
            .build().start(PORT);/*from   w  ww .j  a  v a 2 s  . co m*/

    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}

From source file:com.zimbra.cs.mime.MimeTest.java

@Test
public void testMultipartPGP() throws Exception {
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n"
            + "Content-Type: multipart/encyrpted;\r\n" + " protocol=\"application/pgp-encrypted\";\r\n"
            + " boundary=" + boundary + "\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n"
            + "------------1111971890AC3BB91\r\n" + "Content-Type: application/pgp-encrypted\r\n"
            + "Content-Description: PGP/MIME version identification\r\n\r\n" + "Version: 1\r\n\r\n"
            + "------------1111971890AC3BB91\r\n"
            + "Content-Type: application/octet-stream; name=\"encrypted.asc\"\r\n"
            + "Content-Description: OpenPGP encrypted message\r\n"
            + "Content-Disposition: inline; filename=\"encrypted.asc\"\r\n\r\n"
            + "-----BEGIN PGP MESSAGE-----\r\n" + "Version: GnuPG v2.0.22 (GNU/Linux)\r\n\r\n"
            + "o82ejqwkjeh12398123bjkbas731321\r\n" //not a real message, just some placeholder data
            + "-----END PGP MESSAGE-----\r\n\r\n";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(),
            new SharedByteArrayInputStream(content.getBytes()));

    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);/*from  w ww.j a  v a  2  s.  c  o m*/
    Assert.assertEquals(3, parts.size());
    MPartInfo multiPart = parts.get(0);
    Assert.assertEquals("multipart/encyrpted", multiPart.getContentType());
    Assert.assertEquals(
            "multipart/encyrpted;\r\n protocol=\"application/pgp-encrypted\";\r\n boundary=" + boundary,
            multiPart.getFullContentType());

    MPartInfo pgpVersion = parts.get(1);
    Assert.assertEquals("application/pgp-encrypted", pgpVersion.getContentType());

    MPartInfo pgpMsg = parts.get(2);
    Assert.assertEquals("application/octet-stream", pgpMsg.getContentType());
    Assert.assertEquals("application/octet-stream; name=\"encrypted.asc\"", pgpMsg.getFullContentType());
}