io.symcpe.wraith.rules.validator.ConditionValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wraith-engine Show documentation
Show all versions of wraith-engine Show documentation
An efficient event correlation library
/**
* Copyright 2016 Symantec Corporation.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.symcpe.wraith.rules.validator;
import java.util.ArrayList;
import java.util.List;
import io.symcpe.wraith.conditions.AbstractSimpleCondition;
import io.symcpe.wraith.conditions.Condition;
import io.symcpe.wraith.conditions.logical.ComplexCondition;
import io.symcpe.wraith.conditions.relational.EqualsCondition;
import io.symcpe.wraith.conditions.relational.JavaRegexCondition;
import io.symcpe.wraith.conditions.relational.NumericCondition;
import io.symcpe.wraith.rules.Rule;
/**
* {@link Validator} for {@link Condition} associated with a {@link Rule}
*
* @author ambud_sharma
*/
public class ConditionValidator implements Validator {
private static final int MAX_LENGTH_REGEX = 200;
private List> conditionValidators = new ArrayList<>();
@SuppressWarnings("unchecked")
@Override
public void configure(List> validators) {
for (Validator> validator : validators) {
try {
conditionValidators.add((Validator) validator);
} catch (Exception e) {
}
}
}
@Override
public void validate(Condition condition) throws ValidationException {
if (condition instanceof ComplexCondition) {
for (Condition childCondition : ((ComplexCondition) condition).getConditions()) {
validate(childCondition);
}
} else {
if (condition instanceof AbstractSimpleCondition) {
AbstractSimpleCondition castedConditon = ((AbstractSimpleCondition) condition);
if (castedConditon.getkey() == null || castedConditon.getkey().isEmpty()) {
throw new ValidationException("Condition header key cannot be empty");
}
if (castedConditon instanceof NumericCondition) {
if (((NumericCondition) castedConditon).getValue() == Double.MIN_VALUE) {
throw new ValidationException("Numeric conditions must have a value");
}
} else if (castedConditon instanceof EqualsCondition) {
Object value = ((EqualsCondition) castedConditon).getValue();
if (value == null) {
throw new ValidationException("Equals condition must have a value");
}
if ((value instanceof String) && ((String) (value)).isEmpty()) {
throw new ValidationException("Equals condition value can't be empty");
}
} else if (castedConditon instanceof JavaRegexCondition) {
String regex = ((JavaRegexCondition)castedConditon).getValue();
if(regex.isEmpty()) {
throw new ValidationException("Regex value can't be empty");
}
if(regex.length()>MAX_LENGTH_REGEX) {
throw new ValidationException("Regex value must be smaller than "+MAX_LENGTH_REGEX+" characters");
}
}
} else {
// unsupported condition type
throw new ValidationException("Unsupported condition");
}
}
for (Validator validator : conditionValidators) {
validator.validate(condition);
}
}
}