package io.undertow.predicate;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
public class MethodPredicate implements Predicate {
private final HttpString[] methods;
MethodPredicate(String[] methods) {
HttpString[] values = new HttpString[methods.length];
for(int i = 0; i < methods.length; ++i) {
values[i] = HttpString.tryFromString(methods[i]);
}
this.methods = values;
}
@Override
public boolean resolve(final HttpServerExchange value) {
for(int i =0; i < methods.length; ++i) {
if(value.getRequestMethod().equals(methods[i])) {
return true;
}
}
return false;
}
@Override
public String toString() {
if( methods.length == 1 ) {
return "method( '" + methods[0] + "' )";
} else {
return "method( {" + Arrays.asList( methods ).stream().map(s->s.toString()).collect(Collectors.joining(", ")) + "} )";
}
}
public static class Builder implements PredicateBuilder {
@Override
public String name() {
return "method";
}
@Override
public Map<String, Class<?>> parameters() {
return Collections.<String, Class<?>>singletonMap("value", String[].class);
}
@Override
public Set<String> requiredParameters() {
return Collections.singleton("value");
}
@Override
public String defaultParameter() {
return "value";
}
@Override
public Predicate build(final Map<String, Object> config) {
String[] methods = (String[]) config.get("value");
return new MethodPredicate(methods);
}
}
}