Search in sources :

Example 31 with PackageException

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

the class MimePackage method getRegularPartData.

/**
     * Get the decoded data of a specified part
     *
     * @param partIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public InputStream getRegularPartData(int partIndex) throws PackageException {
    boolean storeReconnected = false;
    try {
        // store should be opened for actions including getting InputStream.
        // Hence store open is not in getPart
        storeReconnected = reconnectStoreIfClosed();
        MimePart part = getPart(partIndex, false);
        return part.getInputStream();
    } catch (MessagingException e) {
        throw new PackageException(e);
    } catch (IOException ioe) {
        throw new PackageException(ioe);
    } finally {
        try {
            closeStoreConnection(storeReconnected);
        } catch (MessagingException e) {
            log.error(e);
        }
    }
}
Also used : MessagingException(javax.mail.MessagingException) 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 32 with PackageException

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

the class MimePackage method addPart.

/**
     * Add a part with the specified text to the specified position
     *
     * @param content the part's content
     * @param contentTypeSubtype the part's subtype of content type like plain (sub type of text/plain) or html (sub type of text/html)
     * @param charset the part's charset
     * @throws PackageException
     */
@PublicAtsApi
public void addPart(String content, String contentTypeSubtype, String charset) throws PackageException {
    // create a new inline part
    MimeBodyPart part = new MimeBodyPart();
    try {
        part.setText(content, charset, contentTypeSubtype);
        part.setDisposition(MimeBodyPart.INLINE);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
    addPart(part, PART_POSITION_LAST);
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) MimeBodyPart(javax.mail.internet.MimeBodyPart) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 33 with PackageException

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

the class PackageLoader method loadPackageFromDb.

private static InputStream loadPackageFromDb(int packageId, String messagesHost, String messagesDB, String messagestable, String messagesUser, String messagesPassword) throws PackageException {
    DbConnMySQL dbConnection = new DbConnMySQL(messagesHost, messagesDB, messagesUser, messagesPassword);
    MysqlDbProvider dbProvider = new MysqlDbProvider(dbConnection);
    try {
        InputStream packageContent = dbProvider.selectValue(messagestable, "message_id", Integer.toString(packageId), "data");
        log.info("Successfully extracted package with id '" + packageId + "' from '" + messagestable + "' DB");
        return packageContent;
    } catch (DbRecordsException dbre) {
        throw new PackageException("Package with id '" + packageId + "' does not exist in 'messages' DB");
    } catch (DbException dbe) {
        throw new PackageException("Could not get package with id '" + packageId + "' from the 'messages' DB");
    }
}
Also used : MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) PackageException(com.axway.ats.action.objects.model.PackageException) DbRecordsException(com.axway.ats.core.dbaccess.exceptions.DbRecordsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 34 with PackageException

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

the class MimePackage method setBody.

/**
     * Replaces body parts with the specified content. <br>
     * If this is a plain text only message, the provided HTML content will not
     * affect the message in any way.
     * <em>Note:</em> This method has effect only if there are already such (plain and/or HTML) parts.
     *
     * @param plainTextContent
     *            the text content of the message
     * @param htmlContent
     *            the HTML content of the message
     * @throws PackageException
     */
@PublicAtsApi
public void setBody(String plainTextContent, String htmlContent) throws PackageException {
    try {
        String messageContentType = message.getContentType();
        if (messageContentType == null) {
            // not expected as default should be "text/plain"
            log.info("No content type is set yet. Body of message is not changed");
            return;
        } else {
            // type is not not case-sensitive as mentioned in
            // http://www.w3.org/Protocols/rfc1341/4_Content-Type.html and RFC 2045
            messageContentType = messageContentType.toLowerCase();
        }
        if (messageContentType.startsWith(CONTENT_TYPE_TEXT_PLAIN)) {
            // this is a text/plain message
            message.setContent(plainTextContent, message.getContentType());
        } else {
            if (messageContentType.startsWith(CONTENT_TYPE_MULTIPART_PREFIX)) {
                // this is a MULTIPART message
                try {
                    BodyPart tmpBodyPart;
                    MimeMultipart tmpMultipartContent = (MimeMultipart) message.getContent();
                    for (int index = 0; index < tmpMultipartContent.getCount(); index++) {
                        tmpBodyPart = tmpMultipartContent.getBodyPart(index);
                        if (tmpBodyPart.getContentType().startsWith(CONTENT_PART_TYPE_TEXT_HTML)) {
                            // just replace content, do not create additional part
                            tmpBodyPart.setContent(htmlContent, tmpBodyPart.getContentType());
                        // for some reason after setting content there is match for text/plain too for the same body part
                        // so use if-else or continue
                        } else if (tmpBodyPart.getContentType().startsWith(CONTENT_PART_TYPE_TEXT_PLAIN)) {
                            // replace text part
                            tmpBodyPart.setContent(plainTextContent, tmpBodyPart.getContentType());
                            continue;
                        }
                    // do not check content type and go to process next part
                    }
                } catch (IOException ioe) {
                    throw new PackageException("Could not add MIME body parts", ioe);
                }
            }
        }
        // make sure all changes to the message are saved before decomposing
        try {
            message.saveChanges();
        } catch (MessagingException me) {
            throw new PackageException("Could not save message changes", me);
        }
        // we need to decompose again, as a new part has been added
        decompose();
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) 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 35 with PackageException

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

the class MimePackage method setSender.

/**
     * Set the sender (From header) for the package
     *
     * @param sender
     *            the sender email address
     * @throws PackageException
     */
@PublicAtsApi
public void setSender(String sender) throws PackageException {
    String newSenderAddress;
    String newSenderPersonal;
    try {
        InternetAddress address = new InternetAddress();
        sender = sender.replaceAll("[<>]", "").trim();
        boolean hasPersonal = sender.contains(" ");
        if (hasPersonal) {
            newSenderAddress = sender.substring(sender.lastIndexOf(' '));
            newSenderPersonal = sender.substring(0, sender.lastIndexOf(' '));
            address.setPersonal(newSenderPersonal.trim());
        } else {
            newSenderAddress = sender;
        }
        // set the sender address
        address.setAddress(newSenderAddress.trim());
        message.setFrom(address);
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        throw new PackageException("Sender not present");
    } catch (MessagingException me) {
        throw new PackageException(me);
    } catch (UnsupportedEncodingException uee) {
        throw new PackageException("Error setting address personal", uee);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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