Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Oct 15, 2020
1 parent 57ac32b commit abfbe20
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,10 @@ public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
}

@Override
public void attributes(Map<String, Object> attributes) {
}

@Override
public void unknown(RouterFunction<?> routerFunction) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand Down Expand Up @@ -115,4 +121,39 @@ 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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand Down Expand Up @@ -330,6 +331,35 @@ public <T extends Throwable> RouterFunctions.Builder onError(Class<T> exceptionT
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -858,6 +860,27 @@ Builder onError(Predicate<? super Throwable> predicate,
<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
Expand Down Expand Up @@ -902,6 +925,14 @@ public interface Visitor {
*/
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.
Expand Down Expand Up @@ -1126,6 +1157,58 @@ public void accept(Visitor visitor) {
}


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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.reactive.function.server;

import java.util.Map;
import java.util.Set;
import java.util.function.Function;

Expand Down Expand Up @@ -69,6 +70,10 @@ public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
this.builder.append(lookupFunction).append('\n');
}

@Override
public void attributes(Map<String, Object> attributes) {
}

@Override
public void unknown(RouterFunction<?> routerFunction) {
indent();
Expand Down
Loading

0 comments on commit abfbe20

Please sign in to comment.