Search in sources :

Example 1 with NoSuchHeaderException

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

the class MimePackage method getPartHeader.

/**
     * Get the specified header value from a specified MIME part
     *
     * @param headerName
     * @param partNum
     * @param headerIndex
     * @return
     * @throws PackageException
     */
@PublicAtsApi
public String getPartHeader(String headerName, int partNum, int headerIndex) 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 > headerIndex)) {
            return headers[headerIndex];
        } else {
            throw new NoSuchHeaderException(headerName, partNum, headerIndex);
        }
    } 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 2 with NoSuchHeaderException

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

the class HeaderRule 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);
    String[] headerValues;
    try {
        if (headerIndex == -1) {
            // we are going to check all header values
            if (partIndex == PART_MAIN_MESSAGE) {
                headerValues = emailMessage.getHeaderValues(headerName);
            } else {
                headerValues = emailMessage.getPartHeaderValues(headerName, partIndex);
            }
        } else {
            // we are going to check a particular header value
            String headerValue;
            if (partIndex == PART_MAIN_MESSAGE) {
                headerValue = emailMessage.getHeader(headerName, headerIndex);
            } else {
                headerValue = emailMessage.getPartHeader(headerName, partIndex, headerIndex);
            }
            headerValues = new String[] { headerValue };
        }
    } catch (NoSuchHeaderException nshe) {
        log.debug("Meta data has no header '" + headerName + "'");
        //no such header, so return false
        return false;
    } catch (PackageException pe) {
        throw new RbvException(pe);
    }
    //if there is no such header return false
    boolean actualResult = false;
    if (headerValues == null || headerValues.length == 0) {
        log.info("No header '" + headerName + "' was found");
    } else {
        for (String headerValue : headerValues) {
            switch(matchMode) {
                case LEFT:
                    actualResult = headerValue.startsWith(expectedValue);
                    break;
                case RIGHT:
                    actualResult = headerValue.endsWith(expectedValue);
                    break;
                case EQUALS:
                    actualResult = headerValue.equals(expectedValue);
                    break;
                case FIND:
                    actualResult = headerValue.indexOf(expectedValue) >= 0;
                    break;
                case REGEX:
                    actualResult = Pattern.compile(expectedValue).matcher(headerValue).find();
                    break;
            }
            log.info("Actual value for header '" + headerName + "' is '" + headerValue + "'");
            if (actualResult) {
                // we matched a header value, stop iterating the rest of the values
                break;
            }
        }
    }
    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) NoSuchHeaderException(com.axway.ats.action.objects.model.NoSuchHeaderException)

Example 3 with NoSuchHeaderException

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

Aggregations

NoSuchHeaderException (com.axway.ats.action.objects.model.NoSuchHeaderException)3 PackageException (com.axway.ats.action.objects.model.PackageException)3 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)2 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 MessagingException (javax.mail.MessagingException)2 MimePart (javax.mail.internet.MimePart)2 MimePackage (com.axway.ats.action.objects.MimePackage)1 RbvException (com.axway.ats.rbv.model.RbvException)1