Search in sources :

Example 31 with SharedByteArrayInputStream

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

the class ZMimeParserTest method repetition.

@Test
public void repetition() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    String boundary = BOUNDARY1;
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    bb.append("Content-Type: multipart/mixed; boundary=").append(boundary).append("\r\n");
    for (int i = 0; i < 100; i++) {
        bb.append("--").append(boundary).append("\r\n");
        bb.append("Content-Type: text/plain\r\n");
        bb.append("\r\n");
        bb.append("foo!  bar!  loud noises\r\n\r\n");
    }
    bb.append("--").append(boundary).append("--\r\n");
    try {
        MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
        Object content = mm.getContent();
        Assert.assertTrue("content is multipart", content instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) content;
        Assert.assertEquals("count reduced??", 100, mp.getCount());
    } catch (ClassCastException e) {
        Assert.fail("mishandled double Content-Type headers");
    }
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 32 with SharedByteArrayInputStream

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

the class CalendarItem method decomposeBlob.

/**
     * Break up a multipart/digest blob into separate MimeMessages keyed by InvId header value.
     * @param digestStream
     * @return
     * @throws MessagingException
     */
public static Map<Integer, MimeMessage> decomposeBlob(byte[] digestBlob) throws ServiceException {
    Map<Integer, MimeMessage> map = new HashMap<Integer, MimeMessage>();
    try {
        InputStream bais = new SharedByteArrayInputStream(digestBlob);
        FixedMimeMessage digestMm = new FixedMimeMessage(JMSession.getSession(), bais);
        // It should be multipart/digest.
        MimeMultipart mmp;
        Object obj = digestMm.getContent();
        if (obj instanceof MimeMultipart)
            mmp = (MimeMultipart) obj;
        else
            throw ServiceException.FAILURE("Expected MimeMultipart, but got " + obj.getClass().getName() + ": " + obj.toString(), null);
        int numParts = mmp.getCount();
        for (int i = 0; i < numParts; i++) {
            MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
            int invId = 0;
            String[] hdrs = mbp.getHeader("invId");
            if (hdrs != null && hdrs.length > 0) {
                invId = Integer.parseInt(hdrs[0]);
                MimeMessage mm;
                Object objMbp = mbp.getContent();
                if (objMbp instanceof MimeMessage)
                    mm = (MimeMessage) objMbp;
                else
                    throw ServiceException.FAILURE("Expected MimeMessage, but got " + objMbp.getClass().getName() + ": " + objMbp.toString(), null);
                map.put(invId, mm);
            }
        }
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    } catch (NumberFormatException e) {
        throw ServiceException.FAILURE("Can't parse calendar item blob", e);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) IOException(java.io.IOException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 33 with SharedByteArrayInputStream

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

the class ParsedMessage method initialize.

private void initialize(byte[] rawData, Long receivedDate, boolean indexAttachments) throws ServiceException {
    if (rawData == null || rawData.length == 0) {
        throw ServiceException.FAILURE("Message data cannot be null or empty.", null);
    }
    sharedStream = new SharedByteArrayInputStream(rawData);
    initialize(receivedDate, indexAttachments);
}
Also used : SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 34 with SharedByteArrayInputStream

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

the class ParseMimeMessage method importMsgSoap.

public static MimeMessage importMsgSoap(Element msgElem) throws ServiceException {
    /* msgElem == "<m>" E_MSG */
    assert (msgElem.getName().equals(MailConstants.E_MSG));
    Element contentElement = msgElem.getElement(MailConstants.E_CONTENT);
    byte[] content;
    try {
        // Convert LF to CRLF because the XML parser normalizes element text to LF.
        String text = StringUtil.lfToCrlf(contentElement.getText());
        content = text.getBytes("utf-8");
    } catch (UnsupportedEncodingException e) {
        throw ServiceException.FAILURE("encoding error", e);
    }
    long maxSize = Provisioning.getInstance().getConfig().getLongAttr(Provisioning.A_zimbraMtaMaxMessageSize, DEFAULT_MAX_SIZE);
    if ((maxSize != 0) && (content.length > maxSize)) {
        throw ServiceException.INVALID_REQUEST("inline message too large", null);
    }
    InputStream messageStream = new SharedByteArrayInputStream(content);
    try {
        return new Mime.FixedMimeMessage(JMSession.getSession(), messageStream);
    } catch (MessagingException me) {
        throw ServiceException.FAILURE("MessagingExecption", me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) Element(com.zimbra.common.soap.Element) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 35 with SharedByteArrayInputStream

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

the class TestContentTransferEncoding method testSimpleMimeMessage.

/*
     * This tests the CTE header of a forwarded message being inferred from the existing message when the message is a simple MIME message
     */
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testSimpleMimeMessage() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getSimpleMimeString().getBytes()));
    ParsedMessage pm = new ParsedMessage(mimeMsg, true);
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    Message msg = mbox.addMessage(null, pm, dopt, null);
    MsgToSend msgToSend = new MsgToSend();
    msgToSend.setOrigId(String.valueOf(msg.getId()));
    msgToSend.setReplyType("w");
    msgToSend.setSubject("Fwd: Simple Test");
    msgToSend.setMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "simple test"));
    SendMsgRequest req = new SendMsgRequest();
    req.setMsg(msgToSend);
    MimeMessage parsed = sendForwardedMessage(req, msg);
    assertEquals("test", parsed.getHeader("Content-Transfer-Encoding")[0]);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) MsgToSend(com.zimbra.soap.mail.type.MsgToSend) SendMsgRequest(com.zimbra.soap.mail.message.SendMsgRequest) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

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