Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.springframework.web.servlet.function.RouterFunctionBuilder Maven / Gradle / Ivy
/*
* Copyright 2002-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.web.servlet.function;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
/**
* Default implementation of {@link RouterFunctions.Builder}.
*
* @author Arjen Poutsma
* @since 5.2
*/
class RouterFunctionBuilder implements RouterFunctions.Builder {
private final List> routerFunctions = new ArrayList<>();
private final List> filterFunctions = new ArrayList<>();
private final List> errorHandlers = new ArrayList<>();
@Override
public RouterFunctions.Builder add(RouterFunction routerFunction) {
Assert.notNull(routerFunction, "RouterFunction must not be null");
this.routerFunctions.add(routerFunction);
return this;
}
private RouterFunctions.Builder add(RequestPredicate predicate, HandlerFunction handlerFunction) {
this.routerFunctions.add(RouterFunctions.route(predicate, handlerFunction));
return this;
}
// GET
@Override
public RouterFunctions.Builder GET(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.GET), handlerFunction);
}
@Override
public RouterFunctions.Builder GET(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.GET).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder GET(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.GET(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder GET(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.GET(pattern).and(predicate), handlerFunction);
}
// HEAD
@Override
public RouterFunctions.Builder HEAD(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.HEAD), handlerFunction);
}
@Override
public RouterFunctions.Builder HEAD(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.HEAD).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder HEAD(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.HEAD(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder HEAD(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.HEAD(pattern).and(predicate), handlerFunction);
}
// POST
@Override
public RouterFunctions.Builder POST(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.POST), handlerFunction);
}
@Override
public RouterFunctions.Builder POST(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.POST).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder POST(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.POST(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder POST(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.POST(pattern).and(predicate), handlerFunction);
}
// PUT
@Override
public RouterFunctions.Builder PUT(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.PUT), handlerFunction);
}
@Override
public RouterFunctions.Builder PUT(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.PUT).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder PUT(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.PUT(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder PUT(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.PUT(pattern).and(predicate), handlerFunction);
}
// PATCH
@Override
public RouterFunctions.Builder PATCH(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.PATCH), handlerFunction);
}
@Override
public RouterFunctions.Builder PATCH(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.PATCH).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder PATCH(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.PATCH(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder PATCH(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.PATCH(pattern).and(predicate), handlerFunction);
}
// DELETE
@Override
public RouterFunctions.Builder DELETE(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.DELETE), handlerFunction);
}
@Override
public RouterFunctions.Builder DELETE(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.DELETE).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder DELETE(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.DELETE(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder DELETE(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.DELETE(pattern).and(predicate), handlerFunction);
}
// OPTIONS
@Override
public RouterFunctions.Builder OPTIONS(HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.OPTIONS), handlerFunction);
}
@Override
public RouterFunctions.Builder OPTIONS(RequestPredicate predicate, HandlerFunction handlerFunction) {
return add(RequestPredicates.method(HttpMethod.OPTIONS).and(predicate), handlerFunction);
}
@Override
public RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction handlerFunction) {
return add(RequestPredicates.OPTIONS(pattern), handlerFunction);
}
@Override
public RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction);
}
// other
@Override
public RouterFunctions.Builder route(RequestPredicate predicate,
HandlerFunction handlerFunction) {
return add(RouterFunctions.route(predicate, handlerFunction));
}
@Override
public RouterFunctions.Builder resources(String pattern, Resource location) {
return add(RouterFunctions.resources(pattern, location));
}
@Override
public RouterFunctions.Builder resources(Function> lookupFunction) {
return add(RouterFunctions.resources(lookupFunction));
}
@Override
public RouterFunctions.Builder nest(RequestPredicate predicate,
Consumer builderConsumer) {
Assert.notNull(builderConsumer, "Consumer must not be null");
RouterFunctionBuilder nestedBuilder = new RouterFunctionBuilder();
builderConsumer.accept(nestedBuilder);
RouterFunction nestedRoute = nestedBuilder.build();
this.routerFunctions.add(RouterFunctions.nest(predicate, nestedRoute));
return this;
}
@Override
public RouterFunctions.Builder nest(RequestPredicate predicate,
Supplier> routerFunctionSupplier) {
Assert.notNull(routerFunctionSupplier, "RouterFunction Supplier must not be null");
RouterFunction nestedRoute = routerFunctionSupplier.get();
this.routerFunctions.add(RouterFunctions.nest(predicate, nestedRoute));
return this;
}
@Override
public RouterFunctions.Builder path(String pattern,
Consumer builderConsumer) {
return nest(RequestPredicates.path(pattern), builderConsumer);
}
@Override
public RouterFunctions.Builder path(String pattern,
Supplier> routerFunctionSupplier) {
return nest(RequestPredicates.path(pattern), routerFunctionSupplier);
}
@Override
public RouterFunctions.Builder filter(HandlerFilterFunction filterFunction) {
Assert.notNull(filterFunction, "HandlerFilterFunction must not be null");
this.filterFunctions.add(filterFunction);
return this;
}
@Override
public RouterFunctions.Builder before(Function requestProcessor) {
Assert.notNull(requestProcessor, "RequestProcessor must not be null");
return filter(HandlerFilterFunction.ofRequestProcessor(requestProcessor));
}
@Override
public RouterFunctions.Builder after(
BiFunction responseProcessor) {
Assert.notNull(responseProcessor, "ResponseProcessor must not be null");
return filter(HandlerFilterFunction.ofResponseProcessor(responseProcessor));
}
@Override
public RouterFunctions.Builder onError(Predicate predicate,
BiFunction responseProvider) {
Assert.notNull(predicate, "Predicate must not be null");
Assert.notNull(responseProvider, "ResponseProvider must not be null");
this.errorHandlers.add(0, HandlerFilterFunction.ofErrorHandler(predicate, responseProvider));
return this;
}
@Override
public RouterFunctions.Builder onError(Class exceptionType,
BiFunction responseProvider) {
Assert.notNull(exceptionType, "ExceptionType must not be null");
Assert.notNull(responseProvider, "ResponseProvider must not be null");
return onError(exceptionType::isInstance, responseProvider);
}
@Override
public RouterFunctions.Builder withAttribute(String name, Object value) {
Assert.hasLength(name, "Name must not be empty");
Assert.notNull(value, "Value must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction attributed = this.routerFunctions.get(lastIdx)
.withAttribute(name, value);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunctions.Builder withAttributes(Consumer> attributesConsumer) {
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("attributes can only be called after any other method (GET, path, etc.)");
}
int lastIdx = this.routerFunctions.size() - 1;
RouterFunction attributed = this.routerFunctions.get(lastIdx)
.withAttributes(attributesConsumer);
this.routerFunctions.set(lastIdx, attributed);
return this;
}
@Override
public RouterFunction build() {
if (this.routerFunctions.isEmpty()) {
throw new IllegalStateException("No routes registered. Register a route with GET(), POST(), etc.");
}
RouterFunction result = new BuiltRouterFunction(this.routerFunctions);
if (this.filterFunctions.isEmpty() && this.errorHandlers.isEmpty()) {
return result;
}
else {
HandlerFilterFunction filter =
Stream.concat(this.filterFunctions.stream(), this.errorHandlers.stream())
.reduce(HandlerFilterFunction::andThen)
.orElseThrow(IllegalStateException::new);
return result.filter(filter);
}
}
/**
* Router function returned by {@link #build()} that simply iterates over the registered routes.
*/
private static class BuiltRouterFunction extends RouterFunctions.AbstractRouterFunction {
private final List> routerFunctions;
public BuiltRouterFunction(List> routerFunctions) {
Assert.notEmpty(routerFunctions, "RouterFunctions must not be empty");
this.routerFunctions = new ArrayList<>(routerFunctions);
}
@Override
public Optional> route(ServerRequest request) {
for (RouterFunction routerFunction : this.routerFunctions) {
Optional> result = routerFunction.route(request);
if (result.isPresent()) {
return result;
}
}
return Optional.empty();
}
@Override
public void accept(RouterFunctions.Visitor visitor) {
this.routerFunctions.forEach(routerFunction -> routerFunction.accept(visitor));
}
}
}