
de.androbit.nibbler.http.RequestHandlerMatcher Maven / Gradle / Ivy
The newest version!
package de.androbit.nibbler.http;
import de.androbit.nibbler.dsl.HandlerDefinition;
import de.androbit.nibbler.dsl.PathDefinition;
import de.androbit.nibbler.http.uri.PathMatchResult;
import de.androbit.nibbler.http.uri.PathMatcher;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class RequestHandlerMatcher {
List pathDefinitions;
PathMatcher pathMatcher = new PathMatcher();
public static class MatchingHandlers {
private List matchingPathHandlers;
List methodHandlers;
Optional contentHandler;
public MatchingHandlers(List matchingPathHandlers, List methodHandlers, Optional contentHandler) {
this.matchingPathHandlers = matchingPathHandlers;
this.methodHandlers = methodHandlers;
this.contentHandler = contentHandler;
}
public List getMatchingPathHandlers() {
return matchingPathHandlers;
}
public List getMethodHandlers() {
return methodHandlers;
}
public Optional getContentHandler() {
return contentHandler;
}
}
public RequestHandlerMatcher(List pathDefinitions) {
this.pathDefinitions = pathDefinitions;
}
public MatchingHandlers getMatchingHandlers(RestRequest request) {
Optional acceptHeader = Optional.ofNullable(request.header(Header.Accept.name()));
List matchingPathHandlers = getPathHandlers(request.path().value())
.collect(Collectors.toList());
List methodHandlers = matchingPathHandlers.stream().flatMap(matchingMethodHandlers(request.method()))
.collect(Collectors.toList());
Optional contentHandler = getHandlerForContentType(acceptHeader, methodHandlers);
return new MatchingHandlers(matchingPathHandlers, methodHandlers, contentHandler);
}
Function> matchingMethodHandlers(RestHttpMethod restHttpMethod){
return pathDefinition -> {
return pathDefinition.getPathDefinition()
.getMethodHandlers()
.get(restHttpMethod)
.stream()
.filter(handlerWithMethod(restHttpMethod))
.flatMap(handlerDefinition -> Stream.of(new FoundHandlerDefinition(pathDefinition.getMatchResult(), handlerDefinition)));
};
}
Predicate handlerWithMethod(RestHttpMethod httpMethod) {
return handlerDefinition -> handlerDefinition.getRestHttpMethod() == httpMethod;
}
private Optional getHandlerForContentType(Optional acceptHeader, List methodHandlers) {
Stream contentHandlers = methodHandlers
.stream()
.filter(handler -> !acceptHeader.isPresent() ||
allTypesAccepted(acceptHeader) ||
handlerAccepts(acceptHeader, handler.getHandlerDefinition()));
return contentHandlers.findFirst();
}
private boolean allTypesAccepted(Optional acceptHeader) {
return acceptHeader.get().contains("*/*");
}
private boolean handlerAccepts(Optional acceptHeader, HandlerDefinition handler) {
return handler.getHandledType().isPresent() &&
acceptHeader.get().contains(handler.getHandledType().get().contentType());
}
private Stream getPathHandlers(String requestPath) {
return pathDefinitions
.stream()
.flatMap(path -> {
PathMatchResult matchResult = pathMatcher.match(path.getPathTemplate(), requestPath);
if (matchResult.isMatch()) {
return Stream.of(new MatchingPathDefinition(path, matchResult));
} else {
return Stream.empty();
}
});
}
class MatchingPathDefinition {
PathDefinition pathDefinition;
PathMatchResult matchResult;
MatchingPathDefinition(PathDefinition pathDefinition, PathMatchResult matchResult) {
this.pathDefinition = pathDefinition;
this.matchResult = matchResult;
}
public PathDefinition getPathDefinition() {
return pathDefinition;
}
public PathMatchResult getMatchResult() {
return matchResult;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy