Search in sources :

Example 1 with MimePart

use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.

the class ContactTest method modifyContactHavingAttachment.

/**
     * Modify Contact having an attachment (bug 70488).
     */
@Test
public void modifyContactHavingAttachment() throws Exception {
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", "Contact Initial Content");
    byte[] attachData = "attachment 1".getBytes();
    Attachment textAttachment = new Attachment(attachData, "text/plain", "customField", "file.txt");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = mbox.createContact(null, new ParsedContact(attrs, Lists.newArrayList(textAttachment)), Mailbox.ID_FOLDER_CONTACTS, null);
    ParsedContact pc = new ParsedContact(contact).modify(new ParsedContact.FieldDeltaList(), new ArrayList<Attachment>());
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), pc.getContentStream());
    MimePart mp = Mime.getMimePart(mm, "1");
    Assert.assertEquals("text/plain", mp.getContentType());
    Assert.assertEquals("attachment 1", mp.getContent());
}
Also used : HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) Test(org.junit.Test)

Example 2 with MimePart

use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.

the class MimeTest method fileAsStream.

private void fileAsStream(String extension, boolean expectText) throws MessagingException, IOException {
    if (extension.charAt(0) == '.') {
        extension = extension.substring(1);
    }
    String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: application/octet-stream;name=\"test." + extension + "\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + //obviously not a real file
    "R0a1231312ad124svsdsal==";
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    MimePart part = Mime.getMimePart(mm, "1");
    String expectedType = expectText ? "text/plain" : "application/octet-stream";
    Assert.assertEquals(expectedType, Mime.getContentType(part.getContentType()));
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 3 with MimePart

use of javax.mail.internet.MimePart in project ats-framework by Axway.

the class MimePackage method getRegularPartCharset.

/**
     * Get the character set of a regular part
     *
     * @param partIndex
     *            the index of the part
     * @return the charset
     * @throws PackageException
     */
@PublicAtsApi
public String getRegularPartCharset(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= regularPartIndices.size()) {
        throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(regularPartIndices.get(partIndex));
        // get the content type header
        ContentType contentType = new ContentType(part.getContentType());
        return contentType.getParameter("charset");
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with MimePart

use of javax.mail.internet.MimePart in project ats-framework by Axway.

the class MimePackage method getAllStreams.

@PublicAtsApi
public List<InputStream> getAllStreams() throws PackageException {
    boolean storeReconnected = false;
    try {
        // store should be opened for actions including getting InputStream.
        storeReconnected = reconnectStoreIfClosed();
        ArrayList<InputStream> streams = new ArrayList<InputStream>();
        try {
            for (MimePart part : parts) {
                streams.add(part.getInputStream());
            }
        } finally {
            closeStoreConnection(storeReconnected);
        }
        return streams;
    } catch (MessagingException me) {
        throw new PackageException("Could not read mime parts", me);
    } catch (IOException ioe) {
        throw new PackageException("Could not read mime parts", ioe);
    }
}
Also used : MessagingException(javax.mail.MessagingException) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SeekInputStream(com.axway.ats.core.io.SeekInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) IOException(java.io.IOException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with MimePart

use of javax.mail.internet.MimePart in project ats-framework by Axway.

the class MimePackage method setAttachmentFileName.

/**
     * Set/modify attachment file name
     *
     * @param attachmentPartIndex index among attachment parts only
     * @param fileName the file name. Add one or reset existing one
     * @throws NoSuchMimePartException if not such attachment part is found
     * @throws PackageException in case of other mail messaging exception
     */
@PublicAtsApi
public void setAttachmentFileName(int attachmentPartIndex, String fileName) throws PackageException {
    // first check if there is part at this position at all
    if (attachmentPartIndex >= attachmentPartIndices.size()) {
        throw new NoSuchMimePartException("No attachment at position '" + attachmentPartIndex + "'");
    }
    try {
        MimePart part = getPart(attachmentPartIndices.get(attachmentPartIndex));
        // set the attachment file name
        part.setFileName(fileName);
        // must save now
        this.message.saveChanges();
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

MimePart (javax.mail.internet.MimePart)46 MessagingException (javax.mail.MessagingException)22 MimeMessage (javax.mail.internet.MimeMessage)22 IOException (java.io.IOException)15 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)12 PackageException (com.axway.ats.action.objects.model.PackageException)11 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)10 PublicAtsApi (com.axway.ats.common.PublicAtsApi)10 InputStream (java.io.InputStream)10 ServiceException (com.zimbra.common.service.ServiceException)8 Test (org.junit.Test)8 MimePackage (com.axway.ats.action.objects.MimePackage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ContentType (javax.mail.internet.ContentType)6 BaseTest (com.axway.ats.action.BaseTest)5 Message (com.zimbra.cs.mailbox.Message)5 MPartInfo (com.zimbra.cs.mime.MPartInfo)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ContentType (com.zimbra.common.mime.ContentType)4 ArrayList (java.util.ArrayList)4