|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.Map; |
22 | 23 | import java.util.Optional;
|
23 | 24 | import java.util.stream.Collectors;
|
24 | 25 |
|
| 26 | +import org.jetbrains.annotations.NotNull; |
25 | 27 | import reactor.core.publisher.Mono;
|
26 | 28 |
|
27 | 29 | import org.springframework.core.Conventions;
|
|
31 | 33 | import org.springframework.core.ResolvableType;
|
32 | 34 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
33 | 35 | import org.springframework.lang.Nullable;
|
| 36 | +import org.springframework.util.Assert; |
34 | 37 | import org.springframework.util.StringUtils;
|
35 | 38 | import org.springframework.web.bind.annotation.ModelAttribute;
|
| 39 | +import org.springframework.web.method.HandlerMethod; |
36 | 40 | import org.springframework.web.reactive.BindingContext;
|
37 | 41 | import org.springframework.web.reactive.HandlerResult;
|
38 | 42 | import org.springframework.web.reactive.result.method.InvocableHandlerMethod;
|
|
47 | 51 | */
|
48 | 52 | class ModelInitializer {
|
49 | 53 |
|
| 54 | + private final ControllerMethodResolver methodResolver; |
| 55 | + |
50 | 56 | private final ReactiveAdapterRegistry adapterRegistry;
|
51 | 57 |
|
52 | 58 |
|
53 |
| - public ModelInitializer(ReactiveAdapterRegistry adapterRegistry) { |
| 59 | + public ModelInitializer(ControllerMethodResolver methodResolver, ReactiveAdapterRegistry adapterRegistry) { |
| 60 | + Assert.notNull(methodResolver, "ControllerMethodResolver is required"); |
| 61 | + Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required"); |
| 62 | + this.methodResolver = methodResolver; |
54 | 63 | this.adapterRegistry = adapterRegistry;
|
55 | 64 | }
|
56 | 65 |
|
57 | 66 |
|
58 | 67 | /**
|
59 |
| - * Initialize the default model in the given {@code BindingContext} through |
60 |
| - * the {@code @ModelAttribute} methods and indicate when complete. |
61 |
| - * <p>This will wait for {@code @ModelAttribute} methods that return |
62 |
| - * {@code Mono<Void>} since those may be adding attributes asynchronously. |
63 |
| - * However if methods return async attributes, those will be added to the |
64 |
| - * model as-is and without waiting for them to be resolved. |
65 |
| - * @param bindingContext the BindingContext with the default model |
66 |
| - * @param attributeMethods the {@code @ModelAttribute} methods |
| 68 | + * Initialize the {@link org.springframework.ui.Model Model} based on a |
| 69 | + * (type-level) {@code @SessionAttributes} annotation and |
| 70 | + * {@code @ModelAttribute} methods. |
| 71 | + * @param handlerMethod the target controller method |
| 72 | + * @param bindingContext the context containing the model |
67 | 73 | * @param exchange the current exchange
|
68 | 74 | * @return a {@code Mono} for when the model is populated.
|
69 | 75 | */
|
70 | 76 | @SuppressWarnings("Convert2MethodRef")
|
71 |
| - public Mono<Void> initModel(BindingContext bindingContext, |
72 |
| - List<InvocableHandlerMethod> attributeMethods, ServerWebExchange exchange) { |
| 77 | + public Mono<Void> initModel(HandlerMethod handlerMethod, InitBinderBindingContext bindingContext, |
| 78 | + ServerWebExchange exchange) { |
| 79 | + |
| 80 | + List<InvocableHandlerMethod> modelMethods = |
| 81 | + this.methodResolver.getModelAttributeMethods(handlerMethod); |
| 82 | + |
| 83 | + SessionAttributesHandler sessionAttributesHandler = |
| 84 | + this.methodResolver.getSessionAttributesHandler(handlerMethod); |
| 85 | + |
| 86 | + if (!sessionAttributesHandler.hasSessionAttributes()) { |
| 87 | + return invokeModelAttributeMethods(bindingContext, modelMethods, exchange); |
| 88 | + } |
| 89 | + |
| 90 | + return exchange.getSession() |
| 91 | + .flatMap(session -> { |
| 92 | + Map<String, Object> attributes = sessionAttributesHandler.retrieveAttributes(session); |
| 93 | + bindingContext.getModel().mergeAttributes(attributes); |
| 94 | + bindingContext.setSessionContext(sessionAttributesHandler, session); |
| 95 | + return invokeModelAttributeMethods(bindingContext, modelMethods, exchange) |
| 96 | + .doOnSuccess(aVoid -> { |
| 97 | + findModelAttributes(handlerMethod, sessionAttributesHandler).forEach(name -> { |
| 98 | + if (!bindingContext.getModel().containsAttribute(name)) { |
| 99 | + Object value = session.getRequiredAttribute(name); |
| 100 | + bindingContext.getModel().addAttribute(name, value); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + @NotNull |
| 108 | + private Mono<Void> invokeModelAttributeMethods(BindingContext bindingContext, |
| 109 | + List<InvocableHandlerMethod> modelMethods, ServerWebExchange exchange) { |
73 | 110 |
|
74 | 111 | List<Mono<HandlerResult>> resultList = new ArrayList<>();
|
75 |
| - attributeMethods.forEach(invocable -> resultList.add(invocable.invoke(exchange, bindingContext))); |
| 112 | + modelMethods.forEach(invocable -> resultList.add(invocable.invoke(exchange, bindingContext))); |
76 | 113 |
|
77 | 114 | return Mono
|
78 |
| - .zip(resultList, objectArray -> { |
79 |
| - return Arrays.stream(objectArray) |
80 |
| - .map(object -> handleResult(((HandlerResult) object), bindingContext)) |
81 |
| - .collect(Collectors.toList()); |
82 |
| - }) |
83 |
| - .flatMap(completionList -> Mono.when(completionList)); |
| 115 | + .zip(resultList, objectArray -> |
| 116 | + Arrays.stream(objectArray) |
| 117 | + .map(object -> handleResult(((HandlerResult) object), bindingContext)) |
| 118 | + .collect(Collectors.toList())) |
| 119 | + .flatMap(Mono::when); |
84 | 120 | }
|
85 | 121 |
|
86 | 122 | private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
|
@@ -109,4 +145,35 @@ private String getAttributeName(MethodParameter param) {
|
109 | 145 | .orElse(Conventions.getVariableNameForParameter(param));
|
110 | 146 | }
|
111 | 147 |
|
| 148 | + /** Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ |
| 149 | + private List<String> findModelAttributes(HandlerMethod handlerMethod, |
| 150 | + SessionAttributesHandler sessionAttributesHandler) { |
| 151 | + |
| 152 | + List<String> result = new ArrayList<>(); |
| 153 | + for (MethodParameter parameter : handlerMethod.getMethodParameters()) { |
| 154 | + if (parameter.hasParameterAnnotation(ModelAttribute.class)) { |
| 155 | + String name = getNameForParameter(parameter); |
| 156 | + Class<?> paramType = parameter.getParameterType(); |
| 157 | + if (sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { |
| 158 | + result.add(name); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + return result; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Derive the model attribute name for the given method parameter based on |
| 167 | + * a {@code @ModelAttribute} parameter annotation (if present) or falling |
| 168 | + * back on parameter type based conventions. |
| 169 | + * @param parameter a descriptor for the method parameter |
| 170 | + * @return the derived name |
| 171 | + * @see Conventions#getVariableNameForParameter(MethodParameter) |
| 172 | + */ |
| 173 | + public static String getNameForParameter(MethodParameter parameter) { |
| 174 | + ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); |
| 175 | + String name = (ann != null ? ann.value() : null); |
| 176 | + return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter)); |
| 177 | + } |
| 178 | + |
112 | 179 | }
|
0 commit comments