Search in sources :

Example 51 with PackageException

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

the class MimePackage method parseContent.

private void parseContent(MimePart part, boolean doNotParseBrokenParts) throws PackageException {
    if (exceedsMaxNestedLevel()) {
        if (log.isInfoEnabled() && !skippedParsingMsgIsAlreadyLogged) {
            log.info("Skipping parsing of nested message parts from current MimePackage because max nested level is reached." + " Current max nesting level is " + ActionLibraryConfigurator.getInstance().getMimePackageMaxNestedLevel());
            skippedParsingMsgIsAlreadyLogged = true;
        }
        return;
    }
    try {
        Object content = part.getContent();
        if (content instanceof Multipart) {
            // if multipart recurse through all child parts
            MimeMultipart mimeMultipart = (MimeMultipart) content;
            int partCount = mimeMultipart.getCount();
            for (int i = 0; i < partCount; i++) {
                try {
                    parseContent((MimeBodyPart) mimeMultipart.getBodyPart(i));
                } catch (PackageException pe) {
                    if (doNotParseBrokenParts) {
                        log.warn("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
                    } else {
                        log.error("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
                        throw new PackageException(pe);
                    }
                }
            }
        } else if (content instanceof MimeMessage) {
            MimeMessage mimeMessage = (MimeMessage) content;
            nestedMimePackages.add(new MimePackage(this.nestedPath, nestedMimePackages.size(), mimeMessage, partOfImapFolder));
            // to treat it as such - it will not be decomposed
            if (isPartAttachment(part)) {
                parts.add(part);
                attachmentPartIndices.add(parts.size() - 1);
            } else {
                try {
                    parseContent(mimeMessage);
                } catch (PackageException pe) {
                    if (doNotParseBrokenParts) {
                        log.warn("Could not parse part: " + mimeMessage.getContentID());
                    } else {
                        log.error("Could not parse part: " + mimeMessage.getContentID());
                        throw pe;
                    }
                }
            }
        } else {
            InternetHeaders internetHeaders = null;
            if (part.getContentType().toLowerCase().startsWith(CONTENT_TYPE_RFC822_HEADERS)) {
                try {
                    // check for "text/rfc822-headers"
                    internetHeaders = getInternetHeaders(content);
                } catch (PackageException e) {
                    throw new PackageException("Content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but headers are not parsed successfully.", e);
                }
                if (internetHeaders == null) {
                    // javax.mail implementation is not very strict
                    log.error("Mail part with content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but could not be parsed. Contents: " + content);
                }
            }
            if (internetHeaders != null) {
                // the "Content-Type" is "text/rfc822-headers" and javamail returns it as InternetHeaders.
                // this is a message with headers only, we will keep it as a
                // nested MimePackage
                MimePackage nestedMimePackage = new MimePackage();
                nestedMimePackage.setNestedPath(this.nestedPath, nestedMimePackages.size());
                Enumeration<?> enumerator = internetHeaders.getAllHeaders();
                while (enumerator.hasMoreElements()) {
                    Header inetHeader = (Header) enumerator.nextElement();
                    nestedMimePackage.addHeader(inetHeader.getName(), inetHeader.getValue());
                }
                nestedMimePackages.add(nestedMimePackage);
            } else {
                // add the body
                parts.add(part);
                if (isPartAttachment(part)) {
                    attachmentPartIndices.add(parts.size() - 1);
                } else {
                    regularPartIndices.add(parts.size() - 1);
                }
            }
        }
    } catch (MessagingException me) {
        throw new PackageException("Could not parse MIME part", me);
    } catch (IOException ioe) {
        throw new PackageException("Could not parse MIME message", ioe);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Enumeration(java.util.Enumeration) InternetHeaders(javax.mail.internet.InternetHeaders) Header(javax.mail.Header) PackageHeader(com.axway.ats.action.objects.model.PackageHeader) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) IOException(java.io.IOException)

Example 52 with PackageException

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

the class MimePackage method addPartFromFile.

/**
     * adds a given body part of a specified type from a specified file to the
     * specified position all line endings are set to "\r\n"
     *
     * @param fileName
     *            name of the file to get the data from
     * @param contentType
     *            the content type of the part
     * @throws PackageException
     *             on error
     */
@PublicAtsApi
public void addPartFromFile(String fileName, String contentType) throws PackageException {
    // the normalized buffer - Windows like line ending
    StringBuffer normalizedBuff = new StringBuffer();
    try (BufferedReader buffReader = new BufferedReader(new java.io.FileReader(fileName))) {
        String currLine;
        do {
            currLine = buffReader.readLine();
            if (currLine != null) {
                normalizedBuff.append(currLine);
                normalizedBuff.append("\r\n");
            }
        } while (currLine != null);
        // add the new body part
        addPart(normalizedBuff.toString(), contentType);
    } catch (IOException ioe) {
        throw new PackageException(ioe);
    }
}
Also used : BufferedReader(java.io.BufferedReader) 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)

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