package io.undertow.predicate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import io.undertow.attribute.ExchangeAttribute;
import io.undertow.server.HttpServerExchange;
public class ContainsPredicate implements Predicate {
private final ExchangeAttribute attribute;
private final String[] values;
ContainsPredicate(final ExchangeAttribute attribute, final String[] values) {
this.attribute = attribute;
this.values = new String[values.length];
System.arraycopy(values, 0, this.values, 0, values.length);
}
@Override
public boolean resolve(final HttpServerExchange value) {
String attr = attribute.readAttribute(value);
if (attr == null) {
return false;
}
for (int i = 0; i < values.length; ++i) {
if (attr.contains(values[i])) {
return true;
}
}
return false;
}
public ExchangeAttribute getAttribute() {
return attribute;
}
public String[] getValues() {
String[] ret = new String[values.length];
System.arraycopy(values, 0, ret, 0, values.length);
return ret;
}
@Override
public String toString() {
return "contains( search={" + String.join(", ", Arrays.asList( values ) ) + "}, value='" + attribute.toString() + "' )";
}
public static class Builder implements PredicateBuilder {
@Override
public String name() {
return "contains";
}
@Override
public Map<String, Class<?>> parameters() {
final Map<String, Class<?>> params = new HashMap<>();
params.put("value", ExchangeAttribute.class);
params.put("search", String[].class);
return params;
}
@Override
public Set<String> requiredParameters() {
final Set<String> params = new HashSet<>();
params.add("value");
params.add("search");
return params;
}
@Override
public String defaultParameter() {
return null;
}
@Override
public Predicate build(final Map<String, Object> config) {
String[] search = (String[]) config.get("search");
ExchangeAttribute values = (ExchangeAttribute) config.get("value");
return new ContainsPredicate(values, search);
}
}
}