Search in sources :

Example 1 with TypeException

use of com.axway.ats.core.validation.exceptions.TypeException in project ats-framework by Axway.

the class TypeServerName method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     */
@Override
public void validate() throws TypeException {
    // --- proper string validation ---
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_VALIDATING_SERVER + e.getMessage(), e.getParameterName(), e);
    }
    BaseType typeIP = null;
    BaseType typeHostname = null;
    BaseType typeCIDR = null;
    BaseType typePort = null;
    String[] tokens = HostUtils.splitAddressHostAndPort(this.validatedValue);
    if (tokens.length == 1 && requirePort) {
        throw new TypeException(ERROR_VALIDATING_SERVER_PORT, this.parameterName);
    }
    if (tokens.length == 2) {
        // port number detected
        typePort = TypeFactory.getInstance().createValidationType(ValidationType.NUMBER_PORT_NUMBER, tokens[1]);
        try {
            typePort.validate();
        } catch (TypeException e) {
            throw new TypeException(ERROR_VALIDATING_SERVER + e.getMessage(), this.parameterName);
        }
    }
    // validate hostname or IP address
    if (tokens[0].split(":").length > 1) {
        if (isValidIPv6Address(tokens[0])) {
            return;
        }
        throw new TypeException(ERROR_MESSAGE_MALFORMED, this.parameterName);
    }
    typeIP = TypeFactory.getInstance().createValidationType(ValidationType.STRING_IP, tokens[0]);
    typeHostname = TypeFactory.getInstance().createValidationType(ValidationType.STRING_HOST_NAME, tokens[0]);
    typeCIDR = TypeFactory.getInstance().createValidationType(ValidationType.STRING_CIDR, tokens[0]);
    // check if this is a valid hostname or IP address
    try {
        typeIP.validate();
        return;
    } catch (TypeException outerException) {
        // if not check if this is a valid hostname
        try {
            typeHostname.validate();
            return;
        } catch (TypeException innerException) {
            // check if this is a valid CIDR IPv4 address   //TODO: add support for IPv6
            if (this.extended) {
                try {
                    typeCIDR.validate();
                    return;
                } catch (TypeException e) {
                    throw new TypeException(ERROR_VALIDATING_SERVER + e.getMessage(), this.parameterName);
                }
            }
            throw new TypeException(ERROR_VALIDATING_SERVER_NEITHER, this.parameterName);
        }
    }
}
Also used : TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Example 2 with TypeException

use of com.axway.ats.core.validation.exceptions.TypeException in project ats-framework by Axway.

the class TypeStringCIDR method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     */
@Override
public void validate() throws TypeException {
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_VALIDATING_CIDR + e.getMessage(), e.getParameterName(), e);
    }
    //get the individual parts
    String[] parts = this.validatedValue.split("/");
    if (parts.length != 2) {
        throw new TypeException(ERROR_MESSAGE_INVALID, this.parameterName);
    }
    TypeRegex typeRegex = new TypeRegex(null, parts[0], null, TypeRegex.RegexType.IPv4_ADDRESS);
    try {
        typeRegex.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_VALIDATING_CIDR + e.getMessage(), e.getParameterName());
    }
    int cidrRange;
    try {
        cidrRange = Integer.parseInt(parts[1]);
    } catch (NumberFormatException nfe) {
        throw new TypeException(ERROR_MESSAGE_NON_INTEGER, this.parameterName, nfe);
    }
    if (cidrRange < 1 || cidrRange > 32) {
        throw new TypeException(ERROR_MESSAGE_INVALID_RANGE, this.parameterName);
    }
    String[] ipParts = parts[0].split("\\.");
    int currentIpPart;
    for (String ipPart : ipParts) {
        //we should be safe, as this has been confirmed as valid IP address
        currentIpPart = Integer.parseInt(ipPart);
        if (cidrRange >= 8) {
            cidrRange -= 8;
        } else if ((cidrRange > 0) && (cidrRange < 8)) {
            if ((((1 << (8 - cidrRange)) - 1) & currentIpPart) != 0) {
                throw new TypeException(ERROR_MESSAGE_INVALID_PART, this.parameterName);
            }
            cidrRange = 0;
        } else {
            if (currentIpPart != 0) {
                throw new TypeException(ERROR_MESSAGE_INVALID_PART, this.parameterName);
            }
        }
    }
}
Also used : TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Example 3 with TypeException

use of com.axway.ats.core.validation.exceptions.TypeException in project ats-framework by Axway.

the class TypeNumber method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     *
     * @throws TypeException
     */
@Override
public void validate() throws TypeException {
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_VALIDATING_NUMBER, this.parameterName, e);
    }
    // check if this is a number
    Double number = extractNumber(this.value);
    if (number == null) {
        throw new TypeException(ERROR_MESSAGE_CAST_EXCEPTION, this.parameterName);
    }
    this.validatedValue = number.doubleValue();
}
Also used : TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Example 4 with TypeException

use of com.axway.ats.core.validation.exceptions.TypeException in project ats-framework by Axway.

the class TypeEmailAddress method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     */
@Override
public void validate() throws TypeException {
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_MESSAGE_INVALID_EMAIL + e.getMessage(), this.parameterName, e);
    }
    try {
        String email = (String) this.value;
        // Note that the JavaMail's implementation of email address validation is
        // somewhat limited. The Javadoc says "The current implementation checks many,
        // but not all, syntax rules.". For example, the address a@ is correctly
        // flagged as invalid, but the address "a"@ is considered
        // valid by JavaMail, even though it is not valid according to RFC 822.
        new InternetAddress(email, true);
    } catch (AddressException ae) {
        throw new TypeException(ERROR_MESSAGE_INVALID_EMAIL, this.parameterName, ae);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Example 5 with TypeException

use of com.axway.ats.core.validation.exceptions.TypeException in project ats-framework by Axway.

the class TypeRange method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     */
@Override
public void validate() throws TypeException {
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_VALIDATING_RANGE + e.getMessage(), this.parameterName, e);
    }
    // check if arguments are in fact numbers
    Double min = extractNumber(this.arguments[0]);
    Double max = extractNumber(this.arguments[1]);
    if (min == null || max == null) {
        throw new TypeException(ERROR_MESSAGE_WRONG_RANGE, this.parameterName);
    }
    this.lowerLimit = min.doubleValue();
    this.upperLimit = max.doubleValue();
    if (this.upperLimit <= this.lowerLimit) {
        throw new TypeException(ERROR_MESSAGE_WRONG_RANGE, this.parameterName);
    }
    if (this.validatedValue <= this.upperLimit && this.validatedValue >= this.lowerLimit) {
        return;
    }
    throw new TypeException("Argument [" + this.validatedValue + "] is not within the predefined range: " + "[" + this.lowerLimit + "," + this.upperLimit + "]", this.parameterName);
}
Also used : TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Aggregations

TypeException (com.axway.ats.core.validation.exceptions.TypeException)7 ActionExecutionException (com.axway.ats.agent.core.exceptions.ActionExecutionException)1 InternalComponentException (com.axway.ats.agent.core.exceptions.InternalComponentException)1 Parameter (com.axway.ats.agent.core.model.Parameter)1 ValidationType (com.axway.ats.core.validation.ValidationType)1 InvalidInputArgumentsException (com.axway.ats.core.validation.exceptions.InvalidInputArgumentsException)1 BaseType (com.axway.ats.core.validation.types.BaseType)1 File (java.io.File)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1