Introduce RouterFunction attributes
This commit introduces support for router function attributes, a way to associate meta-data with a route. Closes: gh-25938
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -59,6 +60,10 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor {
|
||||
public void resources(Function<ServerRequest, Optional<Resource>> lookupFunction) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributes(Map<String, Object> attributes) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unknown(RouterFunction<?> routerFunction) {
|
||||
}
|
||||
|
||||
@@ -16,7 +16,12 @@
|
||||
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Represents a function that routes to a {@linkplain HandlerFunction handler function}.
|
||||
@@ -114,4 +119,40 @@ public interface RouterFunction<T extends ServerResponse> {
|
||||
default void accept(RouterFunctions.Visitor visitor) {
|
||||
visitor.unknown(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new routing function with the given attribute.
|
||||
* @param name the attribute name
|
||||
* @param value the attribute value
|
||||
* @return a function that has the specified attributes
|
||||
* @since 5.3
|
||||
*/
|
||||
default RouterFunction<T> withAttribute(String name, Object value) {
|
||||
Assert.hasLength(name, "Name must not be empty");
|
||||
Assert.notNull(value, "Value must not be null");
|
||||
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put(name, value);
|
||||
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new routing function with attributes manipulated with the given consumer.
|
||||
* <p>The map provided to the consumer is "live", so that the consumer can be used
|
||||
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
|
||||
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
|
||||
* {@link Map} methods.
|
||||
* @param attributesConsumer a function that consumes the attributes map
|
||||
* @return this builder
|
||||
* @since 5.3
|
||||
*/
|
||||
default RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
|
||||
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
|
||||
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributesConsumer.accept(attributes);
|
||||
return new RouterFunctions.AttributesRouterFunction<>(this, attributes);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ 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;
|
||||
@@ -323,6 +324,35 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
|
||||
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<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
|
||||
.withAttribute(name, value);
|
||||
this.routerFunctions.set(lastIdx, attributed);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunctions.Builder withAttributes(Consumer<Map<String, Object>> 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<ServerResponse> attributed = this.routerFunctions.get(lastIdx)
|
||||
.withAttributes(attributesConsumer);
|
||||
this.routerFunctions.set(lastIdx, attributed);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunction<ServerResponse> build() {
|
||||
if (this.routerFunctions.isEmpty()) {
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
@@ -769,6 +772,27 @@ public abstract class RouterFunctions {
|
||||
Builder onError(Class<? extends Throwable> exceptionType,
|
||||
BiFunction<Throwable, ServerRequest, ServerResponse> responseProvider);
|
||||
|
||||
/**
|
||||
* Add an attribute with the given name and value to the last route built with this builder.
|
||||
* @param name the attribute name
|
||||
* @param value the attribute value
|
||||
* @return this builder
|
||||
* @since 5.3
|
||||
*/
|
||||
Builder withAttribute(String name, Object value);
|
||||
|
||||
/**
|
||||
* Manipulate the attributes of the last route built with the given consumer.
|
||||
* <p>The map provided to the consumer is "live", so that the consumer can be used
|
||||
* to {@linkplain Map#put(Object, Object) overwrite} existing attributes,
|
||||
* {@linkplain Map#remove(Object) remove} attributes, or use any of the other
|
||||
* {@link Map} methods.
|
||||
* @param attributesConsumer a function that consumes the attributes map
|
||||
* @return this builder
|
||||
* @since 5.3
|
||||
*/
|
||||
Builder withAttributes(Consumer<Map<String, Object>> attributesConsumer);
|
||||
|
||||
/**
|
||||
* Builds the {@code RouterFunction}. All created routes are
|
||||
* {@linkplain RouterFunction#and(RouterFunction) composed} with one another, and filters
|
||||
@@ -813,6 +837,14 @@ public abstract class RouterFunctions {
|
||||
*/
|
||||
void resources(Function<ServerRequest, Optional<Resource>> lookupFunction);
|
||||
|
||||
/**
|
||||
* Receive notification of a router function with attributes. The
|
||||
* given attributes apply to the router notification that follows this one.
|
||||
* @param attributes the attributes that apply to the following router
|
||||
* @since 5.3
|
||||
*/
|
||||
void attributes(Map<String, Object> attributes);
|
||||
|
||||
/**
|
||||
* Receive notification of an unknown router function. This method is called for router
|
||||
* functions that were not created via the various {@link RouterFunctions} methods.
|
||||
@@ -1044,4 +1076,56 @@ public abstract class RouterFunctions {
|
||||
}
|
||||
|
||||
|
||||
static final class AttributesRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> {
|
||||
|
||||
private final RouterFunction<T> delegate;
|
||||
|
||||
private final Map<String,Object> attributes;
|
||||
|
||||
public AttributesRouterFunction(RouterFunction<T> delegate, Map<String, Object> attributes) {
|
||||
this.delegate = delegate;
|
||||
this.attributes = initAttributes(attributes);
|
||||
}
|
||||
|
||||
private static Map<String, Object> initAttributes(Map<String, Object> attributes) {
|
||||
if (attributes.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
else {
|
||||
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<HandlerFunction<T>> route(ServerRequest request) {
|
||||
return this.delegate.route(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.attributes(this.attributes);
|
||||
this.delegate.accept(visitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunction<T> withAttribute(String name, Object value) {
|
||||
Assert.hasLength(name, "Name must not be empty");
|
||||
Assert.notNull(value, "Value must not be null");
|
||||
|
||||
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
|
||||
attributes.put(name, value);
|
||||
return new AttributesRouterFunction<>(this.delegate, attributes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RouterFunction<T> withAttributes(Consumer<Map<String, Object>> attributesConsumer) {
|
||||
Assert.notNull(attributesConsumer, "AttributesConsumer must not be null");
|
||||
|
||||
Map<String, Object> attributes = new LinkedHashMap<>(this.attributes);
|
||||
attributesConsumer.accept(attributes);
|
||||
return new AttributesRouterFunction<>(this.delegate, attributes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
@@ -68,6 +69,10 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi
|
||||
this.builder.append(lookupFunction).append('\n');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributes(Map<String, Object> attributes) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unknown(RouterFunction<?> routerFunction) {
|
||||
indent();
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
class AttributesTestVisitor implements RouterFunctions.Visitor {
|
||||
|
||||
@Nullable
|
||||
private Map<String, Object> attributes;
|
||||
|
||||
private int visitCount;
|
||||
|
||||
public int visitCount() {
|
||||
return this.visitCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startNested(RequestPredicate predicate) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endNested(RequestPredicate predicate) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
|
||||
assertThat(this.attributes).isNotNull();
|
||||
this.attributes = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resources(Function<ServerRequest, Optional<Resource>> lookupFunction) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributes(Map<String, Object> attributes) {
|
||||
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
|
||||
this.attributes = attributes;
|
||||
this.visitCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unknown(RouterFunction<?> routerFunction) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -214,4 +214,23 @@ class RouterFunctionBuilderTests {
|
||||
PathPatternsTestUtils.initRequest(httpMethod, null, requestUri, true, consumer), emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attributes() {
|
||||
RouterFunction<ServerResponse> route = RouterFunctions.route()
|
||||
.GET("/atts/1", request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "bar")
|
||||
.withAttribute("baz", "qux")
|
||||
.GET("/atts/2", request -> ServerResponse.ok().build())
|
||||
.withAttributes(atts -> {
|
||||
atts.put("foo", "bar");
|
||||
atts.put("baz", "qux");
|
||||
})
|
||||
.build();
|
||||
|
||||
AttributesTestVisitor visitor = new AttributesTestVisitor();
|
||||
route.accept(visitor);
|
||||
assertThat(visitor.visitCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.GET;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -109,7 +110,29 @@ class RouterFunctionTests {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void attributes() {
|
||||
RouterFunction<ServerResponse> route = RouterFunctions.route(
|
||||
GET("/atts/1"), request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "bar")
|
||||
.withAttribute("baz", "qux")
|
||||
.and(RouterFunctions.route(GET("/atts/2"), request -> ServerResponse.ok().build())
|
||||
.withAttributes(atts -> {
|
||||
atts.put("foo", "bar");
|
||||
atts.put("baz", "qux");
|
||||
}));
|
||||
|
||||
AttributesTestVisitor visitor = new AttributesTestVisitor();
|
||||
route.accept(visitor);
|
||||
assertThat(visitor.visitCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ServerResponse handlerMethod(ServerRequest request) {
|
||||
return ServerResponse.ok().body("42");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user