Skip to content

Commit 4a380b8

Browse files
committed
Add RequestDataValueProcessor in spring-web-reactive
Issue: SPR-15001
1 parent e4d39bb commit 4a380b8

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
*/
4242
public abstract class AbstractView implements View, ApplicationContextAware {
4343

44+
/** Well-known name for the RequestDataValueProcessor in the bean factory. */
45+
public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";
46+
47+
4448
/** Logger that is available to subclasses */
4549
protected final Log logger = LogFactory.getLog(getClass());
4650

@@ -179,7 +183,21 @@ protected Map<String, Object> getModelAttributes(Map<String, ?> model, ServerWeb
179183
* @see #setRequestContextAttribute
180184
*/
181185
protected RequestContext createRequestContext(ServerWebExchange exchange, Map<String, Object> model) {
182-
return new RequestContext(exchange, model, this.applicationContext);
186+
return new RequestContext(exchange, model, getApplicationContext(), getRequestDataValueProcessor());
187+
}
188+
189+
/**
190+
* Return the {@link RequestDataValueProcessor} to use.
191+
* <p>The default implementation looks in the {@link #getApplicationContext()
192+
* Spring configuration} for a {@code RequestDataValueProcessor} bean with
193+
* the name {@link #REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME}.
194+
*/
195+
protected RequestDataValueProcessor getRequestDataValueProcessor() {
196+
if (getApplicationContext() != null) {
197+
String beanName = REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME;
198+
return getApplicationContext().getBean(beanName, RequestDataValueProcessor.class);
199+
}
200+
return null;
183201
}
184202

185203
/**

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
186186
}
187187

188188
/**
189-
* Create the target URL if necessary pre-pending the contextPath, expanding
190-
* URI template variables, and appending the current request query.
189+
* Create the target URL and, if necessary, pre-pend the contextPath, expand
190+
* URI template variables, append the current request query, and apply the
191+
* configured {@link #getRequestDataValueProcessor()
192+
* RequestDataValueProcessor}.
191193
*/
192194
protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) {
193195

@@ -206,7 +208,10 @@ protected final String createTargetUrl(Map<String, Object> model, ServerWebExcha
206208
targetUrl = appendCurrentRequestQuery(targetUrl.toString(), exchange.getRequest());
207209
}
208210

209-
return targetUrl.toString();
211+
String result = targetUrl.toString();
212+
213+
RequestDataValueProcessor processor = getRequestDataValueProcessor();
214+
return (processor != null ? processor.processUrl(exchange, result) : result);
210215
}
211216

212217
@SuppressWarnings("unchecked")

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,37 @@ public class RequestContext {
5757

5858
private final MessageSource messageSource;
5959

60+
private Locale locale;
61+
62+
private TimeZone timeZone;
63+
6064
private Boolean defaultHtmlEscape;
6165

6266
private Map<String, Errors> errorsMap;
6367

64-
private Locale locale;
68+
private RequestDataValueProcessor dataValueProcessor;
6569

66-
private TimeZone timeZone;
6770

71+
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource) {
72+
this(exchange, model, messageSource, null);
73+
}
6874

69-
public RequestContext(ServerWebExchange exchange, Map<String, Object> model,
70-
MessageSource messageSource) {
75+
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource,
76+
RequestDataValueProcessor dataValueProcessor) {
7177

7278
Assert.notNull(exchange, "'exchange' is required");
7379
Assert.notNull(model, "'model' is required");
7480
Assert.notNull(messageSource, "'messageSource' is required");
7581
this.exchange = exchange;
7682
this.model = model;
7783
this.messageSource = messageSource;
78-
this.defaultHtmlEscape = null; // TODO
7984

8085
Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
8186
this.locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
8287
this.timeZone = TimeZone.getDefault(); // TODO
88+
89+
this.defaultHtmlEscape = null; // TODO
90+
this.dataValueProcessor = dataValueProcessor;
8391
}
8492

8593

@@ -158,6 +166,14 @@ public Boolean getDefaultHtmlEscape() {
158166
return this.defaultHtmlEscape;
159167
}
160168

169+
/**
170+
* Return the {@link RequestDataValueProcessor} instance to apply to in form
171+
* tag libraries and to redirect URLs.
172+
*/
173+
public Optional<RequestDataValueProcessor> getRequestDataValueProcessor() {
174+
return Optional.ofNullable(this.dataValueProcessor);
175+
}
176+
161177
/**
162178
* Return the context path of the the current web application. This is
163179
* useful for building links to other resources within the application.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.reactive.result.view;
17+
18+
import java.util.Map;
19+
20+
import org.springframework.web.server.ServerWebExchange;
21+
22+
/**
23+
* A contract for inspecting and potentially modifying request data values such
24+
* as URL query parameters or form field values before they are rendered by a
25+
* view or before a redirect.
26+
*
27+
* <p>Implementations may use this contract for example as part of a solution
28+
* to provide data integrity, confidentiality, protection against cross-site
29+
* request forgery (CSRF), and others or for other tasks such as automatically
30+
* adding a hidden field to all forms and URLs.
31+
*
32+
* <p>View technologies that support this contract can obtain an instance to
33+
* delegate to via {@link RequestContext#getRequestDataValueProcessor()}.
34+
*
35+
* @author Rossen Stoyanchev
36+
* @since 5.0
37+
*/
38+
public interface RequestDataValueProcessor {
39+
40+
/**
41+
* Invoked when a new form action is rendered.
42+
* @param exchange the current exchange
43+
* @param action the form action
44+
* @param httpMethod the form HTTP method
45+
* @return the action to use, possibly modified
46+
*/
47+
String processAction(ServerWebExchange exchange, String action, String httpMethod);
48+
49+
/**
50+
* Invoked when a form field value is rendered.
51+
* @param exchange the current exchange
52+
* @param name the form field name
53+
* @param value the form field value
54+
* @param type the form field type ("text", "hidden", etc.)
55+
* @return the form field value to use, possibly modified
56+
*/
57+
String processFormFieldValue(ServerWebExchange exchange, String name, String value, String type);
58+
59+
/**
60+
* Invoked after all form fields have been rendered.
61+
* @param exchange the current exchange
62+
* @return additional hidden form fields to be added, or {@code null}
63+
*/
64+
Map<String, String> getExtraHiddenFields(ServerWebExchange exchange);
65+
66+
/**
67+
* Invoked when a URL is about to be rendered or redirected to.
68+
* @param exchange the current exchange
69+
* @param url the URL value
70+
* @return the URL to use, possibly modified
71+
*/
72+
String processUrl(ServerWebExchange exchange, String url);
73+
74+
}

0 commit comments

Comments
 (0)