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:
Arjen Poutsma
2020-10-15 15:52:58 +02:00
parent 8d86d61f9f
commit 417e7e03d4
17 changed files with 712 additions and 0 deletions

View File

@@ -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) {
}

View File

@@ -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);
}
}

View File

@@ -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()) {

View File

@@ -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);
}
}
}

View File

@@ -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();