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.adaptris.util.text.mime.InputStreamDataSource.java

@Override
public InputStream getInputStream() throws IOException {
    return new SharedByteArrayInputStream(wrappedBytes);
}

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

@Test(timeout = 3000)
public void send() 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
            .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\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();//ww w .  j  a  va  2 s  .  com

    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 sendPartially() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("250 OK").recvLine() // RCPT TO 1
            .sendLine("250 OK").recvLine() // RCPT TO 2
            .sendLine("550 not found").recvLine() // RCPT TO 3
            .sendLine("550 not found").recvLine() // DATA
            .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT
            .sendLine("221 bye").build().start(PORT);

    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\n"
            + "To: rcpt1@zimbra.com, rcpt2@zimbra.com, rcpt3@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    try {// w  ww. j av  a 2s .  c  om
        transport.sendMessage(msg, msg.getAllRecipients());
    } catch (SendFailedException e) {
        Assert.assertEquals(1, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(2, 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:<rcpt1@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt2@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt3@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.qa.unittest.TestUserServlet.java

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;/*from  w  w  w  . jav a 2s. co  m*/
    boolean foundMeta = false;
    boolean foundMessage = false;
    while ((entry = tarIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".meta")) {
            assertTrue("Fround " + entry.getName(), hasMeta);
            foundMeta = true;
        }
        if (entry.getName().endsWith(".eml")) {
            byte[] content = new byte[(int) entry.getSize()];
            assertEquals(content.length, tarIn.read(content));
            MimeMessage message = new ZMimeMessage(JMSession.getSession(),
                    new SharedByteArrayInputStream(content));
            byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
            if (hasBody) {
                assertTrue(entry.getName() + " has no body", body.length > 0);
            } else {
                assertEquals(entry.getName() + " has a body", 0, body.length);
            }
            foundMessage = true;
        }
    }
    tarIn.close();
    assertTrue(foundMessage);
    if (hasMeta) {
        assertTrue(foundMeta);
    }
}

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

@Test(timeout = 3000)
public void mailFromError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO
            .sendLine("250 OK").recvLine() // MAIL FROM
            .sendLine("451 error").recvLine() // QUIT
            .build().start(PORT);//from  w ww .j ava2 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\n" + "Subject: 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(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("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}

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

@Test
public void multipartReportRfc822Headers() throws Exception {
    String content = baseNdrContent + boundary + "\r\n" + "Content-Description: Notification\r\n"
            + "Content-Type: text/plain;charset=us-ascii\r\n\r\n"
            + "<mta@example.com>: host mta.example.com[255.255.255.0] said: 554 delivery error: This user doesn't have an example.com account (in reply to end of DATA command)\r\n\r\n"
            + boundary + "\r\n" + "Content-Description: Delivery report\r\n"
            + "Content-Type: message/delivery-status\r\n\r\n" + "X-Postfix-Queue-ID: 12345\r\n"
            + "X-Postfix-Sender: rfc822; test123@example.com\r\n"
            + "Arrival-Date: Wed,  8 Aug 2012 00:05:30 +0900 (JST)\r\n\r\n" + boundary + "\r\n"
            + "Content-Description: Undelivered Message Headers\r\n" + "Content-Type: text/rfc822-headers\r\n"
            + "Content-Transfer-Encoding: 7bit\r\n\r\n" + "From: admin@example\r\n"
            + "To: test15@example.com\r\n" + "Message-ID: <123456@client.example.com>\r\n"
            + "Subject: test msg";

    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(),
            new SharedByteArrayInputStream(content.getBytes()));

    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);//w  ww .  ja  va2  s.co m
    Assert.assertEquals(4, parts.size());
    MPartInfo mpart = parts.get(0);
    Assert.assertEquals("multipart/report", mpart.getContentType());
    List<MPartInfo> children = mpart.getChildren();
    Assert.assertEquals(3, children.size());
    Set<String> types = Sets.newHashSet("text/plain", "message/delivery-status", "text/rfc822-headers");
    for (MPartInfo child : children) {
        Assert.assertTrue("Expected: " + child.getContentType(), types.remove(child.getContentType()));
    }

    Set<MPartInfo> bodies = Mime.getBody(parts, false);
    Assert.assertEquals(2, bodies.size());

    types = Sets.newHashSet("text/plain", "text/rfc822-headers");
    for (MPartInfo body : bodies) {
        Assert.assertTrue("Expected: " + body.getContentType(), types.remove(body.getContentType()));
    }
}

From source file:com.zimbra.cs.imap.ImapMessage.java

static Pair<Long, InputStream> getContent(MailItem item) throws ServiceException {
    if (item instanceof Message) {
        return new Pair<Long, InputStream>(item.getSize(), item.getContentStream());
    } else if (item instanceof Contact) {
        try {/*from   w  w  w  .j av  a2  s  . c  om*/
            VCard vcard = VCard.formatContact((Contact) item);
            QCodec qcodec = new QCodec();
            qcodec.setEncodeBlanks(true);
            StringBuilder header = new StringBuilder();
            header.append("Subject: ").append(qcodec.encode(vcard.fn, MimeConstants.P_CHARSET_UTF8))
                    .append(ImapHandler.LINE_SEPARATOR);
            synchronized (GMT_DATE_FORMAT) {
                header.append("Date: ").append(GMT_DATE_FORMAT.format(new Date(item.getDate())))
                        .append(ImapHandler.LINE_SEPARATOR);
            }
            header.append("Content-Type: text/x-vcard; charset=\"utf-8\"").append(ImapHandler.LINE_SEPARATOR);
            header.append("Content-Transfer-Encoding: 8bit").append(ImapHandler.LINE_SEPARATOR);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(header.toString().getBytes(MimeConstants.P_CHARSET_ASCII));
            baos.write(ImapHandler.LINE_SEPARATOR_BYTES);
            baos.write(vcard.getFormatted().getBytes(MimeConstants.P_CHARSET_UTF8));
            return new Pair<Long, InputStream>((long) baos.size(),
                    new SharedByteArrayInputStream(baos.toByteArray()));
        } catch (Exception e) {
            throw ServiceException.FAILURE("problems serializing contact " + item.getId(), e);
        }
    } else {
        return EMPTY_CONTENT;
    }
}

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

@Test(timeout = 3000)
public void mailSmtpFrom() 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
            .sendLine("221 bye").build().start(PORT);

    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com");
    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();/*from ww  w  .jav a  2  s.  com*/

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<from@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 multiTextBody() throws Exception {

    StringBuilder content = new StringBuilder(baseMpMixedContent);
    int count = 42;
    for (int i = 0; i < count; i++) {
        content.append("------------1111971890AC3BB91\r\n")
                .append("Content-Type: text/html; charset=windows-1250\r\n")
                .append("Content-Transfer-Encoding: quoted-printable\r\n\r\n").append("<html>Body ").append(i)
                .append("</html>\r\n");
    }/*from   www.  j  a  v a 2 s .  co m*/
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(),
            new SharedByteArrayInputStream(content.toString().getBytes()));

    List<MPartInfo> parts = Mime.getParts(mm);
    Assert.assertNotNull(parts);
    Assert.assertEquals(count + 1, parts.size());
    MPartInfo mpart = parts.get(0);
    Assert.assertEquals("multipart/mixed", mpart.getContentType());
    List<MPartInfo> children = mpart.getChildren();
    Assert.assertEquals(count, children.size());

    Set<MPartInfo> bodies = Mime.getBody(parts, false);
    Assert.assertEquals(count, bodies.size());
    for (MPartInfo body : bodies) {
        Assert.assertEquals("text/html", body.getContentType());
        Object mimeContent = body.getMimePart().getContent();
        Assert.assertTrue(mimeContent instanceof String);
        String string = (String) mimeContent;
        int idx = string.indexOf("" + (body.getPartNum() - 1));
        Assert.assertTrue(idx > 0); //body 0 is part 1, b1 is p2, and so on
    }
}

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

@Test(timeout = 3000)
public void nullMailFrom() 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
            .sendLine("221 bye").build().start(PORT);

    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com");
    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";
    SMTPMessage msg = new SMTPMessage(session,
            new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    msg.setEnvelopeFrom("<>"); // this should override the previously set mail.smtp.from
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();//from  ww  w  .  java  2 s  .  c  o m

    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<>\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());
}