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.reactive.function.server;
import java.util.Map;
import java.util.function.Function;
import reactor.core.publisher.Mono;
@@ -60,6 +61,10 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor {
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
}
@Override
public void attributes(Map<String, Object> attributes) {
}
@Override
public void unknown(RouterFunction<?> routerFunction) {
}

View File

@@ -16,8 +16,14 @@
package org.springframework.web.reactive.function.server;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import reactor.core.publisher.Mono;
import org.springframework.util.Assert;
/**
* Represents a function that routes to a {@linkplain HandlerFunction handler function}.
*
@@ -115,4 +121,39 @@ public interface RouterFunction<T extends ServerResponse> {
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.reactive.function.server;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -330,6 +331,35 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
return this;
}
@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,8 @@
package org.springframework.web.reactive.function.server;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
@@ -858,6 +860,27 @@ public abstract class RouterFunctions {
<T extends Throwable> Builder onError(Class<T> exceptionType,
BiFunction<? super T, ServerRequest, Mono<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
@@ -902,6 +925,14 @@ public abstract class RouterFunctions {
*/
void resources(Function<ServerRequest, Mono<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.
@@ -1126,6 +1157,58 @@ 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 Mono<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);
}
}
private static class HandlerStrategiesResponseContext implements ServerResponse.Context {
private final HandlerStrategies strategies;

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.server;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
@@ -69,6 +70,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();