Search in sources :

Example 41 with PackageException

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

the class MimePackage method addAttachment.

/**
     * Add an attachment with the specified content - the attachment will have a
     * content type text\plain and the specified character set
     *
     * @param content
     *            the content of the attachment
     * @param charset
     *            the character set
     * @param fileName
     *            the file name for the content-disposition header
     * @throws PackageException
     *             on error
     */
@PublicAtsApi
public void addAttachment(String content, String charset, String fileName) throws PackageException {
    try {
        // add attachment to multipart content
        MimeBodyPart attPart = new MimeBodyPart();
        attPart.setText(content, charset, PART_TYPE_TEXT_PLAIN);
        attPart.setDisposition(MimeBodyPart.ATTACHMENT);
        attPart.setFileName(fileName);
        addPart(attPart, PART_POSITION_LAST);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
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 42 with PackageException

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

the class MimePackage method setPriority.

/**
     * Set the message priority
     *
     * @param priority
     * @throws PackageException
     */
@PublicAtsApi
public void setPriority(PackagePriority priority) throws PackageException {
    try {
        // set the priority
        message.setHeader("X-Priority", String.valueOf(priority.toInt()));
        // set MS Outlook display-priority header
        message.setHeader("Importance", priority.toString());
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 43 with PackageException

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

the class MimePackage method getAttachmentContentType.

/**
     * Get the attachment content type
     *
     * @param partIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getAttachmentContentType(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= attachmentPartIndices.size()) {
        throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(attachmentPartIndices.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)

Example 44 with PackageException

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

the class MimePackage method setSubject.

/**
     * Set the message subject
     *
     * @param subject
     * @param charset
     * @throws PackageException
     */
@PublicAtsApi
public void setSubject(String subject, String charset) throws PackageException {
    try {
        message.setSubject(subject, charset);
        subjectCharset = charset;
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 45 with PackageException

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

the class MimePackage method getFirstBody.

/**
     * Get the content of the first part with the specified content type
     * <br>If this is a multipart message, we search only into the first nested level.
     *
     * @return the contents (body) of the first part with given content type.
     *  Null if such is not found in current or the direct sub-level
     * @throws PackageException
     */
@PublicAtsApi
private String getFirstBody(String contentType) throws PackageException {
    String textBody = null;
    boolean storeReconnected;
    try {
        String messageContentType = message.getContentType().toLowerCase();
        String contentDisposition = null;
        storeReconnected = reconnectStoreIfClosed();
        try {
            if (messageContentType.startsWith(contentType)) {
                contentDisposition = message.getDisposition();
                if (!Part.ATTACHMENT.equalsIgnoreCase(contentDisposition)) {
                    // this is a plain text message
                    textBody = message.getContent().toString();
                }
            } else {
                Object content = message.getContent();
                if (content instanceof Multipart) {
                    // a multi-part message
                    Multipart parts = (Multipart) message.getContent();
                    // first look on top level
                    for (int i = 0; i < parts.getCount(); i++) {
                        BodyPart mimePart = parts.getBodyPart(i);
                        if (!(mimePart.getContent() instanceof Multipart)) {
                            textBody = getBodyIfNotAttachment(mimePart, contentType);
                            if (textBody != null) {
                                break;
                            }
                        }
                    }
                    if (textBody == null) {
                        // not found on top level - look multipart entries
                        for (int i = 0; i < parts.getCount(); i++) {
                            BodyPart mimePart = parts.getBodyPart(i);
                            if (mimePart.getContent() instanceof Multipart) {
                                Multipart nestedParts = (Multipart) mimePart.getContent();
                                for (int m = 0; m < nestedParts.getCount(); m++) {
                                    BodyPart nestedMimePart = nestedParts.getBodyPart(m);
                                    textBody = getBodyIfNotAttachment(nestedMimePart, contentType);
                                    if (textBody != null) {
                                        break;
                                    }
                                }
                            }
                            if (textBody != null) {
                                break;
                            }
                        }
                    }
                }
            }
        } finally {
            closeStoreConnection(storeReconnected);
        }
        return textBody;
    } catch (MessagingException e) {
        throw new PackageException(e);
    } catch (IOException e) {
        throw new PackageException(e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) 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)

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