Search in sources :

Example 6 with PackageException

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

the class MimePackage method getWholePackage.

@PublicAtsApi
public InputStream getWholePackage() throws PackageException {
    boolean storeReconnected = false;
    try {
        storeReconnected = reconnectStoreIfClosed();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        message.writeTo(outStream);
        return new ByteArrayInputStream(outStream.toByteArray());
    } catch (MessagingException me) {
        throw new PackageException("Could not write message content", me);
    } catch (IOException ioe) {
        throw new PackageException("Could not write message content", ioe);
    } finally {
        try {
            closeStoreConnection(storeReconnected);
        } catch (MessagingException ex) {
            log.warn(ex);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with PackageException

use of com.axway.ats.action.objects.model.PackageException 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 8 with PackageException

use of com.axway.ats.action.objects.model.PackageException 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)

Example 9 with PackageException

use of com.axway.ats.action.objects.model.PackageException 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 10 with PackageException

use of com.axway.ats.action.objects.model.PackageException 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)

Aggregations

PackageException (com.axway.ats.action.objects.model.PackageException)52 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)34 MessagingException (javax.mail.MessagingException)32 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 MimePart (javax.mail.internet.MimePart)11 IOException (java.io.IOException)10 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)9 FilePackage (com.axway.ats.action.objects.FilePackage)7 BaseTest (com.axway.ats.rbv.BaseTest)7 InternetAddress (javax.mail.internet.InternetAddress)7 Test (org.junit.Test)7 MimePackage (com.axway.ats.action.objects.MimePackage)6 RbvException (com.axway.ats.rbv.model.RbvException)6 InputStream (java.io.InputStream)6 RbvStorageException (com.axway.ats.rbv.model.RbvStorageException)5 ContentType (javax.mail.internet.ContentType)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 FileSystemMetaData (com.axway.ats.rbv.filesystem.FileSystemMetaData)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4