Set BEST_MATCHING_HANDLER_ATTRIBUTE in WebFlux.fn

SPR-17367
This commit is contained in:
Arjen Poutsma 2018-10-11 13:43:13 +02:00
parent f12c28e402
commit 8de5c05af0
1 changed files with 24 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.server.support;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import reactor.core.publisher.Mono;
@ -27,6 +28,7 @@ import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
@ -142,12 +144,32 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
protected Mono<?> getHandlerInternal(ServerWebExchange exchange) {
if (this.routerFunction != null) {
ServerRequest request = ServerRequest.create(exchange, this.messageReaders);
exchange.getAttributes().put(RouterFunctions.REQUEST_ATTRIBUTE, request);
return this.routerFunction.route(request);
return this.routerFunction.route(request)
.doOnNext(handler -> setAttributes(exchange.getAttributes(), request, handler));
}
else {
return Mono.empty();
}
}
@SuppressWarnings("unchecked")
private void setAttributes(Map<String, Object> attributes, ServerRequest serverRequest,
HandlerFunction<?> handlerFunction) {
attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest);
attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction);
String matchingPattern =
(String) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
if (matchingPattern != null) {
attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern);
}
Map<String, String> uriVariables =
(Map<String, String>) attributes
.get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (uriVariables != null) {
attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables);
}
}
}