Search in sources :

Example 21 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class SetCalendarItem method deserializeSetCalendarItemData.

private Mailbox.SetCalendarItemData deserializeSetCalendarItemData(RedoLogInput in, boolean attachmentIndexingEnabled) throws IOException, MessagingException {
    Mailbox.SetCalendarItemData toRet = new Mailbox.SetCalendarItemData();
    int mboxId = getMailboxId();
    try {
        // keep this for backward compatibility with when SetCalendarItemData
        in.readBoolean();
        // used to have mForce field
        ICalTimeZone localTz = Util.decodeTimeZoneFromMetadata(new Metadata(in.readUTF()));
        toRet.invite = Invite.decodeMetadata(mboxId, new Metadata(in.readUTF()), null, localTz);
        boolean hasPm;
        if (getVersion().atLeast(1, 24)) {
            hasPm = in.readBoolean();
        } else {
            hasPm = true;
        }
        // If version is earlier than 1.24, we always have ParsedMessage array.
        if (hasPm) {
            long receivedDate = in.readLong();
            int dataLen = in.readInt();
            byte[] rawPmData = new byte[dataLen];
            in.readFully(rawPmData, 0, dataLen);
            InputStream is = new SharedByteArrayInputStream(rawPmData);
            MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is);
            toRet.message = new ParsedMessage(mm, receivedDate, attachmentIndexingEnabled);
        }
    } catch (ServiceException ex) {
        ex.printStackTrace();
        throw new IOException("Cannot read serialized entry for CreateInvite " + ex.toString());
    }
    return toRet;
}
Also used : SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Metadata(com.zimbra.cs.mailbox.Metadata) IOException(java.io.IOException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 22 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class TestParsedMessage method testAddMessage.

/**
     * Tests adding a <tt>ParsedMessage</tt> to a mailbox.
     */
@Test
public void testAddMessage() throws Exception {
    String msg = TestUtil.getTestMessage(NAME_PREFIX + " testAddMessage", SENDER_NAME, SENDER_NAME, null);
    // Test ParsedMessage from byte[]
    ParsedMessage pm = new ParsedMessage(msg.getBytes(), true);
    runAddMessageTest(msg, pm);
    // Test ParsedMessage from File
    mFile = File.createTempFile("TestParsedMessage", null);
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(msg.getBytes());
    pm = new ParsedMessage(mFile, null, true);
    runAddMessageTest(msg, pm);
    // Test ParsedMessage from MimeMessage
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(msg.getBytes()));
    pm = new ParsedMessage(mimeMsg, true);
    runAddMessageTest(msg, pm);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 23 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class TestParsedMessage method testGetData.

/**
     * Confirms that the digest returned by a <tt>ParsedMessage</tt> is the same,
     * regardless of whether the source comes from a byte array, file or <tt>MimeMessage</tt>.
     */
@Test
public void testGetData() throws Exception {
    String msg = TestUtil.getTestMessage(NAME_PREFIX + " testGetData", SENDER_NAME, SENDER_NAME, null);
    // Test ParsedMessage from byte[]
    ParsedMessage pm = new ParsedMessage(msg.getBytes(), true);
    runContentTests(msg, pm);
    // Test ParsedMessage from File
    mFile = File.createTempFile("TestParsedMessage", null);
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(msg.getBytes());
    pm = new ParsedMessage(mFile, null, true);
    runContentTests(msg, pm);
    // Test ParsedMessage from MimeMessage
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(msg.getBytes()));
    pm = new ParsedMessage(mimeMsg, true);
    runContentTests(msg, pm);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 24 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class TestMailSender method testRestrictEnvelopeSender.

@Test
public void testRestrictEnvelopeSender() throws Exception {
    Server server = Provisioning.getInstance().getLocalServer();
    server.setSmtpPort(TEST_SMTP_PORT);
    Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
    Account account = mbox.getAccount();
    // Create a message with a different From header value.
    String from = TestUtil.getAddress("testRestrictEnvelopeSender");
    String subject = NAME_PREFIX + " testRestrictEnvelopeSender";
    MessageBuilder builder = new MessageBuilder().withFrom(from).withToRecipient(RECIPIENT_NAME).withSubject(subject).withBody("Who are you?");
    String content = builder.create();
    MimeMessage msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    account.setSmtpRestrictEnvelopeFrom(true);
    // Restrict envelope sender, disallow custom from.
    account.setAllowAnyFromAddress(false);
    DummySmtpServer smtp = startDummySmtpServer(null, null);
    mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    Assert.assertEquals(account.getName(), smtp.getMailFrom());
    // Test contains to handle personal name
    Assert.assertTrue(getHeaderValue(smtp.getDataLines(), "From").contains(account.getName()));
    // Restrict envelope sender, allow custom from.
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    account.setAllowAnyFromAddress(true);
    smtp = startDummySmtpServer(null, null);
    mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    Assert.assertEquals(account.getName(), smtp.getMailFrom());
    Assert.assertEquals(from, getHeaderValue(smtp.getDataLines(), "From"));
    account.setSmtpRestrictEnvelopeFrom(false);
    // Don't restrict envelope sender, disallow custom from.
    account.setAllowAnyFromAddress(false);
    smtp = startDummySmtpServer(null, null);
    mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    Assert.assertEquals(account.getName(), smtp.getMailFrom());
    Assert.assertTrue(getHeaderValue(smtp.getDataLines(), "From").contains(account.getName()));
    // Don't restrict envelope sender, allow custom from.
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    account.setAllowAnyFromAddress(true);
    smtp = startDummySmtpServer(null, null);
    mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    Assert.assertEquals(from, smtp.getMailFrom());
    Assert.assertEquals(from, getHeaderValue(smtp.getDataLines(), "From"));
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 25 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class ParsedMessage method updateMimeMessage.

public void updateMimeMessage() throws IOException, MessagingException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    mimeMessage.writeTo(buffer);
    byte[] content = buffer.toByteArray();
    ByteUtil.closeStream(sharedStream);
    sharedStream = new SharedByteArrayInputStream(content);
    mimeMessage = expandedMessage = null;
    mimeMessage = expandedMessage = new Mime.FixedMimeMessage(JMSession.getSession(), sharedStream);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Aggregations

SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)61 MimeMessage (javax.mail.internet.MimeMessage)53 Test (org.junit.Test)47 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)31 Session (javax.mail.Session)15 JMSession (com.zimbra.cs.util.JMSession)14 Transport (javax.mail.Transport)14 InputStream (java.io.InputStream)9 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)7 ByteBuilder (com.zimbra.common.zmime.ZMimeUtility.ByteBuilder)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 FixedMimeMessage (com.zimbra.cs.mime.Mime.FixedMimeMessage)6 MessagingException (javax.mail.MessagingException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)4 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)4 Message (com.zimbra.cs.mailbox.Message)4 ParseMimeMessage (com.zimbra.cs.service.mail.ParseMimeMessage)4 SendMsgRequest (com.zimbra.soap.mail.message.SendMsgRequest)4