Search in sources :

Example 6 with MimePart

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

the class StringInMimePartRule method performMatch.

@Override
public boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    InputStream actualPartDataStream = null;
    BufferedInputStream bufferedStream = null;
    try {
        List<MimePart> partsToCheck = new ArrayList<MimePart>();
        if (partIndex == PART_MAIN_MESSAGE) {
            partsToCheck = emailMessage.getMimeParts();
        } else {
            partsToCheck.add(emailMessage.getPart(partIndex, isPartAttachment));
        }
        for (MimePart partToCheck : partsToCheck) {
            Object partContent = partToCheck.getContent();
            ContentType partContentType = new ContentType(partToCheck.getContentType());
            //skip if no content
            if (partContent == null) {
                log.debug("MIME part does not have any content");
                continue;
            }
            String partContentAsString;
            if (partContent instanceof String) {
                //directly read the content of the part
                partContentAsString = (String) partContent;
            } else if (partContent instanceof InputStream && partContentType.getBaseType().toLowerCase().startsWith(TEXT_MIME_TYPE_LC)) {
                // to be closed in finally
                actualPartDataStream = (InputStream) partContent;
                //get the charset of the part - default to us-ascii
                String charset = partContentType.getParameter("charset");
                if (charset == null) {
                    charset = "us-ascii";
                }
                //read stream by large chunks to minimize memory fragmentation
                int bufLen = 4096;
                byte[] buffer = new byte[bufLen];
                StringBuffer dataStringBuffer = new StringBuffer();
                bufferedStream = new BufferedInputStream(actualPartDataStream);
                int numBytesRead = bufLen;
                while (numBytesRead == bufLen) {
                    numBytesRead = bufferedStream.read(buffer, 0, bufLen);
                    if (numBytesRead != -1) {
                        dataStringBuffer.append(new String(buffer, 0, numBytesRead, charset));
                    } else {
                        //we've reached end of stream
                        break;
                    }
                }
                partContentAsString = dataStringBuffer.toString();
            } else {
                log.debug("Skipping MIME part as it is binary");
                continue;
            }
            if (isValueRegularExpression) {
                actualResult = Pattern.compile(expectedValue).matcher(partContentAsString).find();
            } else {
                actualResult = partContentAsString.indexOf(expectedValue) >= 0;
            }
            //continue anymore
            if (actualResult) {
                break;
            }
        }
        return actualResult;
    } catch (MessagingException me) {
        throw new RbvException(me);
    } catch (PackageException pe) {
        throw new RbvException(pe);
    } catch (IOException ioe) {
        throw new RbvException(ioe);
    } finally {
        IoUtils.closeStream(actualPartDataStream);
        IoUtils.closeStream(bufferedStream);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) RbvException(com.axway.ats.rbv.model.RbvException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MimePackage(com.axway.ats.action.objects.MimePackage) BufferedInputStream(java.io.BufferedInputStream) MimePart(javax.mail.internet.MimePart) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 7 with MimePart

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

the class Test_MimePackage method setBody_multiParts_with_textAndHtmlParts.

@Test
public void setBody_multiParts_with_textAndHtmlParts() throws Exception {
    // create a new message and add TEXT and HTML parts to it
    MimePackage newMailMessage = new MimePackage();
    newMailMessage.addPart("text plain body", MimePackage.PART_TYPE_TEXT_PLAIN);
    newMailMessage.addPart("html body", MimePackage.PART_TYPE_TEXT_HTML);
    MimePart textPart = newMailMessage.getPart(0, false);
    assertEquals(textPart.getContent(), "text plain body");
    assertEquals(textPart.getContentType(), "text/plain; charset=us-ascii");
    MimePart htmlPart = newMailMessage.getPart(1, false);
    assertEquals(htmlPart.getContent(), "html body");
    assertEquals(htmlPart.getContentType(), "text/html; charset=us-ascii");
    // modify both parts
    newMailMessage.setBody("new body");
    // verify the modifications
    MimePart newTextPart = newMailMessage.getPart(0, false);
    assertEquals(newTextPart.getContent(), "new body");
    assertEquals(newTextPart.getContentType(), "text/plain; charset=us-ascii");
    MimePart newHtmlPart = newMailMessage.getPart(1, false);
    assertEquals(newHtmlPart.getContent(), "new body");
    assertEquals(newHtmlPart.getContentType(), "text/html; charset=us-ascii");
    // verify there are no more parts
    try {
        newMailMessage.getPart(2, false);
        assertTrue("There is more than 2 parts, while we expect to have just 2", false);
    } catch (NoSuchMimePartException e) {
    }
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) MimePart(javax.mail.internet.MimePart) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 8 with MimePart

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

the class Test_MimePackage method setBody_multiParts_with_htmlPart_only.

@Test
public void setBody_multiParts_with_htmlPart_only() throws Exception {
    // create a new message and add HTML part to it
    MimePackage newMailMessage = new MimePackage();
    newMailMessage.addPart("html body", MimePackage.PART_TYPE_TEXT_HTML);
    MimePart htmlPart = newMailMessage.getPart(0, false);
    assertEquals(htmlPart.getContent(), "html body");
    assertEquals(htmlPart.getContentType(), "text/html; charset=us-ascii");
    // modify the only part
    newMailMessage.setBody("new body");
    // verify the modifications
    MimePart newHtmlPart = newMailMessage.getPart(0, false);
    assertEquals(newHtmlPart.getContent(), "new body");
    assertEquals(newHtmlPart.getContentType(), "text/html; charset=us-ascii");
    // verify there are no more parts
    try {
        newMailMessage.getPart(1, false);
        assertTrue("There is more than 1 part, while we expect to have just 1", false);
    } catch (NoSuchMimePartException e) {
    }
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) MimePart(javax.mail.internet.MimePart) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 9 with MimePart

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

the class MimePackage method getPartHeader.

/**
     * Get the specified header value from a specified MIME part
     *
     * @param headerName
     * @param partNum
     * @param headerIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getPartHeader(String headerName, int partNum, int headerIndex) throws PackageException {
    try {
        String[] headers;
        if (partNum >= parts.size()) {
            throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'");
        }
        MimePart part = parts.get(partNum);
        headers = part.getHeader(headerName);
        if ((headers != null) && (headers.length > headerIndex)) {
            return headers[headerIndex];
        } else {
            throw new NoSuchHeaderException(headerName, partNum, headerIndex);
        }
    } 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) NoSuchHeaderException(com.axway.ats.action.objects.model.NoSuchHeaderException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 10 with MimePart

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

the class MimePackage method getRegularPartContentType.

/**
     * Get the content type of a regular part
     *
     * @param partIndex
     *            the index of the regular part
     * @return the content type as string
     * @throws PackageException
     */
@PublicAtsApi
public String getRegularPartContentType(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.getBaseType();
    } 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)

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