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);
}
}
}
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);
}
}
}
}
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();
}
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);
}
}
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);
}
Aggregations