Search in sources :

Example 1 with ZMimePart

use of com.zimbra.client.ZMessage.ZMimePart in project zm-mailbox by Zimbra.

the class TestUtil method getContent.

public static byte[] getContent(ZMailbox mbox, String msgId, String name) throws ServiceException, IOException {
    ZMessage msg = mbox.getMessageById(msgId);
    ZMimePart part = getPart(msg, name);
    if (part == null) {
        return null;
    }
    return ByteUtil.getContent(mbox.getRESTResource("?id=" + msgId + "&part=" + part.getPartName()), 1024);
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMimePart(com.zimbra.client.ZMessage.ZMimePart)

Example 2 with ZMimePart

use of com.zimbra.client.ZMessage.ZMimePart in project zm-mailbox by Zimbra.

the class TestConversion method downloadAttachmentsFromTNEFmsg.

/**
     * Tests downloading attachments from a TNEF message (bug 44263).
     */
@Test
public void downloadAttachmentsFromTNEFmsg() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String msgName = "/unittest/tnef.msg";
    File tnefMsg = new File(LC.zimbra_home.value() + msgName);
    Assert.assertTrue(String.format("To run this test copy data%1$s to /opt/zimbra%1$s", msgName), tnefMsg.exists() && tnefMsg.canRead());
    // Add the TNEF message
    String msgContent = new String(ByteUtil.getContent(tnefMsg));
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, msgContent);
    // Test downloading attachments.
    ZMessage msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + NAME_PREFIX + " Rich text (TNEF) test\"");
    byte[] data = TestUtil.getContent(mbox, msg.getId(), "upload.gif");
    Assert.assertEquals(73, data.length);
    data = TestUtil.getContent(mbox, msg.getId(), "upload2.gif");
    Assert.assertEquals(851, data.length);
    ZMimePart part = TestUtil.getPart(msg, "upload.gif");
    checkPartSize(73, part.getSize());
    part = TestUtil.getPart(msg, "upload2.gif");
    checkPartSize(851, part.getSize());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) ZMimePart(com.zimbra.client.ZMessage.ZMimePart) File(java.io.File) Test(org.junit.Test)

Example 3 with ZMimePart

use of com.zimbra.client.ZMessage.ZMimePart in project zm-mailbox by Zimbra.

the class TestGetMsg method verifyMessageContent.

private void verifyMessageContent(ZMailbox mbox, String msgId, boolean wantHtml, Integer requestMaxLength, Integer expectedLength, boolean expectedTruncated, String body, String contentType) throws Exception {
    ZGetMessageParams params = new ZGetMessageParams();
    params.setId(msgId);
    params.setWantHtml(wantHtml);
    params.setMax(requestMaxLength);
    ZMessage msg = mbox.getMessage(params);
    ZMimePart mp = msg.getMimeStructure();
    Assert.assertEquals(expectedTruncated, mp.wasTruncated());
    String expected = body;
    if (expectedLength != null) {
        expected = body.substring(0, expectedLength);
    }
    if (contentType.equals(MimeConstants.CT_TEXT_ENRICHED)) {
        // HTML conversion in TextEnrichedHandler will drop trailing
        // characters when the enriched data is malformed (tags not
        // closed, etc.).
        Assert.assertTrue(mp.getContent().length() > 0);
        Assert.assertTrue(expected.startsWith(mp.getContent()));
    } else {
        Assert.assertEquals(expected, mp.getContent());
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZGetMessageParams(com.zimbra.client.ZGetMessageParams) ZMimePart(com.zimbra.client.ZMessage.ZMimePart)

Example 4 with ZMimePart

use of com.zimbra.client.ZMessage.ZMimePart in project zm-mailbox by Zimbra.

the class TestSendAndReceive method testMalformedContentType.

/**
     * Confirms that we can forward attachments with a malformed content type (bug 42452).
     */
@Test
public void testMalformedContentType() throws Exception {
    TestUtil.createAccount(USER_NAME);
    // Generate original message.
    String subject = NAME_PREFIX + " testMalformedContentType";
    MessageBuilder builder = new MessageBuilder().withFrom(USER_NAME).withToRecipient(USER_NAME).withSubject(subject).withAttachment("This is an attachment", "test.txt", MimeConstants.CT_TEXT_PLAIN);
    // Hack Content-Type so that it's invalid.
    BufferedReader reader = new BufferedReader(new StringReader(builder.create()));
    StringBuilder msgBuf = new StringBuilder();
    String line = reader.readLine();
    boolean replaced = false;
    while (line != null) {
        if (line.matches("Content-Type.*test.txt.*")) {
            line = line.replace("Content-Type: text/plain;", "Content-Type: text/plain;;");
            Assert.assertTrue("Unexpected line: " + line, line.contains(";;"));
            replaced = true;
        }
        msgBuf.append(line).append("\r\n");
        line = reader.readLine();
    }
    Assert.assertTrue("Could not find text/plain attachment.", replaced);
    // Add message to the mailbox.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, msgBuf.toString());
    // Forward the attachment in a new message.
    ZMessage srcMsg = TestUtil.getMessage(mbox, "subject:\"" + subject + "\"");
    ZMimePart srcAttachPart = srcMsg.getMimeStructure().getChildren().get(1);
    Assert.assertEquals("test.txt", srcAttachPart.getFileName());
    ZOutgoingMessage outgoing = new ZOutgoingMessage();
    outgoing.setMessagePart(new MessagePart(MimeConstants.CT_TEXT_PLAIN, "Forwarding attachment."));
    outgoing.setMessagePartsToAttach(Arrays.asList(new AttachedMessagePart(srcMsg.getId(), srcAttachPart.getPartName(), null)));
    String address = TestUtil.getAddress(USER_NAME);
    ZEmailAddress sender = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_FROM);
    ZEmailAddress recipient = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_TO);
    outgoing.setAddresses(Arrays.asList(sender, recipient));
    String fwdSubject = NAME_PREFIX + " testMalformedContentType forward";
    outgoing.setSubject(fwdSubject);
    mbox.sendMessage(outgoing, null, false);
    // Make sure the forwarded message arrives.
    ZMessage fwd = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + fwdSubject + "\"");
    ZMimePart fwdAttachPart = fwd.getMimeStructure().getChildren().get(1);
    Assert.assertEquals("test.txt", fwdAttachPart.getFileName());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZOutgoingMessage(com.zimbra.client.ZMailbox.ZOutgoingMessage) MessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ZMimePart(com.zimbra.client.ZMessage.ZMimePart) ZMailbox(com.zimbra.client.ZMailbox) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ZEmailAddress(com.zimbra.client.ZEmailAddress) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 5 with ZMimePart

use of com.zimbra.client.ZMessage.ZMimePart in project zm-mailbox by Zimbra.

the class TestSendAndReceive method testInlineAttachment.

/**
     * Test inline attachment. See bug #88933
     */
@Test
public void testInlineAttachment() throws Exception {
    TestUtil.createAccount(USER_NAME);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    //create inline image attachment
    String imgName = "synacore.jpg";
    String content = "<content><img src=\"cid:" + imgName + "\"></content>";
    List<AttachedMessagePart> inlineAttachments = new ArrayList<AttachedMessagePart>();
    String attachId = mbox.uploadAttachment(imgName, new byte[1024], MimeConstants.CT_IMAGE_JPEG, 5000);
    AttachedMessagePart inlineImg = new AttachedMessagePart(attachId, imgName);
    inlineAttachments.add(inlineImg);
    MessagePart html = new MessagePart(MimeConstants.CT_TEXT_HTML, content, inlineAttachments);
    //create outgoing message
    String subject = NAME_PREFIX + " testInlineAttachment " + MimeConstants.CT_TEXT_PLAIN + " 1";
    ZOutgoingMessage outgoingMsg = new ZOutgoingMessage();
    List<ZEmailAddress> addresses = new ArrayList<ZEmailAddress>();
    addresses.add(new ZEmailAddress(TestUtil.addDomainIfNecessary(USER_NAME), null, null, ZEmailAddress.EMAIL_TYPE_TO));
    outgoingMsg.setAddresses(addresses);
    outgoingMsg.setSubject(subject);
    MessagePart related = new MessagePart(MimeConstants.CT_MULTIPART_RELATED, html);
    outgoingMsg.setMessagePart(related);
    mbox.sendMessage(outgoingMsg, null, false);
    //verify that received message has the attachment
    ZMessage incoming = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    Assert.assertTrue("this message should have an attachment", incoming.hasAttachment());
    Assert.assertNotNull("this message should have mime parts", incoming.getMimeStructure());
    Assert.assertNotNull("this message should have two subparts", incoming.getMimeStructure().getChildren());
    List<ZMimePart> parts = incoming.getMimeStructure().getChildren();
    Assert.assertEquals("this message should have 2 subparts", 2, parts.size());
    Assert.assertTrue("one of the parts should have a content id", parts.get(0).getContentId() != null || parts.get(1).getContentId() != null);
    Assert.assertTrue("one of the parts should be " + imgName, imgName.equalsIgnoreCase(parts.get(0).getFileName()) || imgName.equalsIgnoreCase(parts.get(1).getFileName()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZOutgoingMessage(com.zimbra.client.ZMailbox.ZOutgoingMessage) ZMailbox(com.zimbra.client.ZMailbox) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ZEmailAddress(com.zimbra.client.ZEmailAddress) MessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ArrayList(java.util.ArrayList) ZMimePart(com.zimbra.client.ZMessage.ZMimePart) Test(org.junit.Test)

Aggregations

ZMimePart (com.zimbra.client.ZMessage.ZMimePart)6 ZMessage (com.zimbra.client.ZMessage)5 ZMailbox (com.zimbra.client.ZMailbox)3 Test (org.junit.Test)3 ZEmailAddress (com.zimbra.client.ZEmailAddress)2 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)2 AttachedMessagePart (com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart)2 MessagePart (com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart)2 ZGetMessageParams (com.zimbra.client.ZGetMessageParams)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1