|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
21 | 22 | import java.util.stream.Collectors;
|
22 | 23 |
|
23 | 24 | import reactor.core.publisher.Mono;
|
|
27 | 28 | import org.springframework.http.codec.ServerCodecConfigurer;
|
28 | 29 | import org.springframework.lang.Nullable;
|
29 | 30 | import org.springframework.util.CollectionUtils;
|
| 31 | +import org.springframework.web.reactive.function.server.HandlerFunction; |
30 | 32 | import org.springframework.web.reactive.function.server.RouterFunction;
|
31 | 33 | import org.springframework.web.reactive.function.server.RouterFunctions;
|
32 | 34 | import org.springframework.web.reactive.function.server.ServerRequest;
|
@@ -142,12 +144,32 @@ else if (total > 0) {
|
142 | 144 | protected Mono<?> getHandlerInternal(ServerWebExchange exchange) {
|
143 | 145 | if (this.routerFunction != null) {
|
144 | 146 | ServerRequest request = ServerRequest.create(exchange, this.messageReaders);
|
145 |
| - exchange.getAttributes().put(RouterFunctions.REQUEST_ATTRIBUTE, request); |
146 |
| - return this.routerFunction.route(request); |
| 147 | + return this.routerFunction.route(request) |
| 148 | + .doOnNext(handler -> setAttributes(exchange.getAttributes(), request, handler)); |
147 | 149 | }
|
148 | 150 | else {
|
149 | 151 | return Mono.empty();
|
150 | 152 | }
|
151 | 153 | }
|
152 | 154 |
|
| 155 | + @SuppressWarnings("unchecked") |
| 156 | + private void setAttributes(Map<String, Object> attributes, ServerRequest serverRequest, |
| 157 | + HandlerFunction<?> handlerFunction) { |
| 158 | + |
| 159 | + attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest); |
| 160 | + attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction); |
| 161 | + |
| 162 | + String matchingPattern = |
| 163 | + (String) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE); |
| 164 | + if (matchingPattern != null) { |
| 165 | + attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern); |
| 166 | + } |
| 167 | + Map<String, String> uriVariables = |
| 168 | + (Map<String, String>) attributes |
| 169 | + .get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); |
| 170 | + if (uriVariables != null) { |
| 171 | + attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables); |
| 172 | + } |
| 173 | + } |
| 174 | + |
153 | 175 | }
|
0 commit comments