Search in sources :

Example 36 with PackageException

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

the class MimePackage method getAttachmentCharset.

/**
     * Get the attachment character set
     *
     * @param partIndex
     *            the index of the attachment
     * @return the character set for this attachment, null if there is no such
     * @throws PackageException
     */
@PublicAtsApi
public String getAttachmentCharset(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.getParameter("charset");
    } 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 37 with PackageException

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

the class MimePackage method addAttachmentDir.

/**
     * Appends all files in a specified folder as attachments
     *
     * @param folder the folder containing all files to be attached
     * @throws PackageException
     */
@PublicAtsApi
public void addAttachmentDir(String folder) throws PackageException {
    // fetch list of files in specified directory
    File dir = new File(folder);
    File[] list = dir.listFiles();
    if (null == list) {
        throw new PackageException("Could not read from directory '" + folder + "'.");
    } else {
        // process all files, skipping directories
        for (int i = 0; i < list.length; i++) {
            if ((null != list[i]) && (!list[i].isDirectory())) {
                // add attachment to multipart content
                MimeBodyPart attPart = new MimeBodyPart();
                FileDataSource ds = new FileDataSource(list[i].getPath());
                try {
                    attPart.setDataHandler(new DataHandler(ds));
                    attPart.setDisposition(MimeBodyPart.ATTACHMENT);
                    attPart.setFileName(ds.getName());
                } catch (MessagingException me) {
                    throw new PackageException(me);
                }
                addPart(attPart, PART_POSITION_LAST);
            }
        }
    }
}
Also used : MessagingException(javax.mail.MessagingException) FileDataSource(javax.activation.FileDataSource) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 38 with PackageException

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

the class MimePackage method tag.

/**
     * Tagging the message by adding "Automation-Message-Tag" header with the current time value(in milliseconds).
     * This is useful if want to recognize this message between others in the same IMAP folder.
     *      *
     * @throws ActionException
     */
@PublicAtsApi
public void tag() throws ActionException {
    // we use the message's sent time as a unique tag
    String tagValue = Long.toString(Calendar.getInstance().getTimeInMillis());
    try {
        setHeader("Automation-Message-Tag", tagValue);
        // if everything is OK, then this is our tag
        tag = tagValue;
        log.info(getDescription() + " tagged with tag '" + tag + "'");
    } catch (PackageException e) {
        throw new ObjectCannotBeTaggedException(getDescription(), null, e);
    }
}
Also used : NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) ObjectCannotBeTaggedException(com.axway.ats.action.objects.model.ObjectCannotBeTaggedException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 39 with PackageException

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

the class MimePackage method getPartHeaderValues.

/**
     * Get all header values from a specified MIME part
     *
     * @param headerName
     * @param partNum
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String[] getPartHeaderValues(String headerName, int partNum) throws PackageException {
    try {
        String[] headers;
        if (partNum >= parts.size()) {
            throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'");
        }
        MimePart part = parts.get(partNum);
        headers = part.getHeader(headerName);
        if ((headers != null) && (headers.length > 0)) {
            return headers;
        } else {
            throw new NoSuchHeaderException(headerName, partNum);
        }
    } 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) NoSuchHeaderException(com.axway.ats.action.objects.model.NoSuchHeaderException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 40 with PackageException

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

the class MimePackage method getSenderAddress.

/**
     * This method resturns only the email address portion of the sender
     * contained in the first From header
     *
     * @return the sender address
     * @throws PackageException
     */
@PublicAtsApi
public String getSenderAddress() throws PackageException {
    try {
        Address[] fromAddresses = message.getFrom();
        if (fromAddresses == null || fromAddresses.length == 0) {
            throw new PackageException("Sender not present");
        }
        InternetAddress fromAddress = (InternetAddress) fromAddresses[0];
        return fromAddress.getAddress();
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) 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) 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