Search in sources :

Example 31 with MimePackage

use of com.axway.ats.action.objects.MimePackage 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 32 with MimePackage

use of com.axway.ats.action.objects.MimePackage in project ats-framework by Axway.

the class SubjectRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    //get the actual subject value
    String subjectValue;
    try {
        subjectValue = emailMessage.getSubject();
    } catch (PackageException pe) {
        throw new RbvException(pe);
    }
    //if there is no such header return false 
    boolean actualResult = false;
    if (subjectValue != null) {
        switch(matchMode) {
            case LEFT:
                actualResult = subjectValue.startsWith(expectedValue);
                break;
            case RIGHT:
                actualResult = subjectValue.endsWith(expectedValue);
                break;
            case EQUALS:
                actualResult = subjectValue.equals(expectedValue);
                break;
            case FIND:
                actualResult = subjectValue.indexOf(expectedValue) >= 0;
                break;
            case REGEX:
                actualResult = Pattern.compile(expectedValue).matcher(subjectValue).find();
                break;
        }
        log.info("Actual subject is '" + subjectValue + "'");
    } else {
        log.info("No subject was found");
    }
    return actualResult;
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) RbvException(com.axway.ats.rbv.model.RbvException) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 33 with MimePackage

use of com.axway.ats.action.objects.MimePackage in project ats-framework by Axway.

the class MimePartCountRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    int actualNumParts;
    if (lookForAttachments) {
        actualNumParts = emailMessage.getAttachmentPartCount();
    } else {
        actualNumParts = emailMessage.getRegularPartCount();
    }
    log.debug("Actual number of parts is " + actualNumParts);
    boolean actualResult = actualNumParts == expectedNumParts;
    return actualResult;
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage)

Example 34 with MimePackage

use of com.axway.ats.action.objects.MimePackage in project ats-framework by Axway.

the class MimePartRule method performMatch.

@Override
protected 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 = ((ImapMetaData) metaData).getMimePackage();
    try {
        InputStream actualPartDataStream = null;
        try {
            actualPartDataStream = emailMessage.getPartData(partIndex, isPartAttachment);
        } catch (NoSuchMimePartException e) {
            //if there is no such mime part then the parts do not match
            log.debug("No MIME part at position '" + partIndex + "'");
            return false;
        }
        if (actualPartDataStream != null) {
            long actualChecksum = emailMessage.getPartChecksum(partIndex, isPartAttachment);
            actualResult = (expectedChecksum == actualChecksum);
        } else {
            log.debug("MIME part at position '" + partIndex + "' does not have any content");
            return false;
        }
    } catch (PackageException pe) {
        throw new RbvException(pe);
    }
    return actualResult;
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) ImapMetaData(com.axway.ats.rbv.imap.ImapMetaData) InputStream(java.io.InputStream) RbvException(com.axway.ats.rbv.model.RbvException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 35 with MimePackage

use of com.axway.ats.action.objects.MimePackage in project ats-framework by Axway.

the class Test_MimePackage method testGetContentOfSavedMail.

@Test
public void testGetContentOfSavedMail() throws PackageException {
    String nestedMailPath = Test_MimePackage.class.getResource("nestedMessageGetContentTest.msg").getPath();
    MimePackage mimePack = PackageLoader.loadMimePackageFromFile(nestedMailPath);
    String contentHtml = mimePack.getHtmlTextBody();
    assertNotNull(contentHtml);
    assertTrue(contentHtml.contains("Some text<br>"));
    assertEquals(null, mimePack.getPlainTextBody());
    // nested MimePackage test
    MimePackage nested = mimePack.getNeededMimePackage(new int[] { 0 });
    String nestedHtmlBody = nested.getHtmlTextBody();
    System.out.println("nestedHtmlBody: " + nestedHtmlBody);
    assertTrue(nestedHtmlBody.contains("Message body<br>"));
    assertNull(nested.getPlainTextBody());
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Aggregations

MimePackage (com.axway.ats.action.objects.MimePackage)53 Test (org.junit.Test)35 BaseTest (com.axway.ats.action.BaseTest)29 ImapMetaData (com.axway.ats.rbv.imap.ImapMetaData)15 Before (org.junit.Before)7 PackageException (com.axway.ats.action.objects.model.PackageException)6 BaseTest (com.axway.ats.rbv.BaseTest)6 FileInputStream (java.io.FileInputStream)6 MimePart (javax.mail.internet.MimePart)6 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)5 RbvException (com.axway.ats.rbv.model.RbvException)5 Test_ImapStorage (com.axway.ats.rbv.imap.Test_ImapStorage)4 StringInMimePartRule (com.axway.ats.rbv.imap.rules.StringInMimePartRule)4 MimePartRule (com.axway.ats.rbv.imap.rules.MimePartRule)2 InputStream (java.io.InputStream)2 MessagingException (javax.mail.MessagingException)2 BeforeClass (org.junit.BeforeClass)2 MailSender (com.axway.ats.action.mail.MailSender)1 MailTransportListener (com.axway.ats.action.mail.model.MailTransportListener)1 DELIVERY_STATE (com.axway.ats.action.mail.model.MailTransportListener.DELIVERY_STATE)1