Skip to content

Commit 7c22d60

Browse files
committed
Streamlined WebContentGenerator API variants: checkRequest, prepareResponse, applyCacheControl, applyCacheSeconds
Issue: SPR-11792
1 parent b277c08 commit 7c22d60

10 files changed

Lines changed: 251 additions & 223 deletions

File tree

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/AbstractController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.web.util.WebUtils;
2626

2727
/**
28-
* <p>Convenient superclass for controller implementations, using the Template Method
28+
* Convenient superclass for controller implementations, using the Template Method
2929
* design pattern.
3030
*
3131
* <p><b><a name="workflow">Workflow
@@ -130,7 +130,8 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons
130130
throws Exception {
131131

132132
// Delegate to WebContentGenerator for checking and preparing.
133-
checkAndPrepare(request, response);
133+
checkRequest(request);
134+
prepareResponse(response);
134135

135136
// Execute handleRequestInternal in synchronized block if required.
136137
if (this.synchronizeOnSession) {
@@ -152,6 +153,6 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons
152153
* @see #handleRequest
153154
*/
154155
protected abstract ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
155-
throws Exception;
156+
throws Exception;
156157

157158
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/WebContentInterceptor.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
import org.springframework.web.util.UrlPathHelper;
3636

3737
/**
38-
* Interceptor that checks and prepares request and response. Checks for supported
39-
* methods and a required session, and applies the specified {@link org.springframework.http.CacheControl}.
38+
* Handler interceptor that checks the request and prepares the response.
39+
* Checks for supported methods and a required session, and applies the
40+
* specified {@link org.springframework.http.CacheControl} builder.
4041
* See superclass bean properties for configuration options.
4142
*
42-
* <p>All the settings supported by this interceptor can also be set on AbstractController.
43-
* This interceptor is mainly intended for applying checks and preparations to a set of
44-
* controllers mapped by a HandlerMapping.
43+
* <p>All the settings supported by this interceptor can also be set on
44+
* {@link AbstractController}. This interceptor is mainly intended for applying
45+
* checks and preparations to a set of controllers mapped by a HandlerMapping.
4546
*
4647
* @author Juergen Hoeller
4748
* @author Brian Clozel
@@ -58,9 +59,10 @@ public class WebContentInterceptor extends WebContentGenerator implements Handle
5859

5960
private Map<String, CacheControl> cacheControlMappings = new HashMap<String, CacheControl>();
6061

62+
6163
public WebContentInterceptor() {
62-
// no restriction of HTTP methods by default,
63-
// in particular for use with annotated controllers
64+
// No restriction of HTTP methods by default,
65+
// in particular for use with annotated controllers...
6466
super(false);
6567
}
6668

@@ -134,11 +136,9 @@ public void setCacheMappings(Properties cacheMappings) {
134136
* <p>Overrides the default cache seconds setting of this interceptor.
135137
* Can specify a empty {@link org.springframework.http.CacheControl} instance
136138
* to exclude a URL path from default caching.
137-
*
138139
* <p>Supports direct matches, e.g. a registered "/test" matches "/test",
139140
* and a various Ant-style pattern matches, e.g. a registered "/t*" matches
140141
* both "/test" and "/team". For details, see the AntPathMatcher javadoc.
141-
*
142142
* @param cacheControl the {@code CacheControl} to use
143143
* @param paths URL paths that will map to the given {@code CacheControl}
144144
* @see #setCacheSeconds
@@ -151,7 +151,6 @@ public void addCacheMapping(CacheControl cacheControl, String... paths) {
151151
}
152152
}
153153

154-
155154
/**
156155
* Set the PathMatcher implementation to use for matching URL paths
157156
* against registered URL patterns, for determining cache mappings.
@@ -168,7 +167,9 @@ public void setPathMatcher(PathMatcher pathMatcher) {
168167

169168
@Override
170169
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
171-
throws ServletException {
170+
throws ServletException {
171+
172+
checkRequest(request);
172173

173174
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
174175
if (logger.isDebugEnabled()) {
@@ -181,19 +182,19 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
181182
if (logger.isDebugEnabled()) {
182183
logger.debug("Applying CacheControl to [" + lookupPath + "]");
183184
}
184-
checkAndPrepare(request, response, cacheControl);
185+
applyCacheControl(response, cacheControl);
185186
}
186187
else if (cacheSeconds != null) {
187188
if (logger.isDebugEnabled()) {
188189
logger.debug("Applying CacheControl to [" + lookupPath + "]");
189190
}
190-
checkAndPrepare(request, response, cacheSeconds);
191+
applyCacheSeconds(response, cacheSeconds);
191192
}
192193
else {
193194
if (logger.isDebugEnabled()) {
194195
logger.debug("Applying default cache seconds to [" + lookupPath + "]");
195196
}
196-
checkAndPrepare(request, response);
197+
prepareResponse(response);
197198
}
198199

199200
return true;
@@ -209,10 +210,10 @@ else if (cacheSeconds != null) {
209210
* @see org.springframework.util.AntPathMatcher
210211
*/
211212
protected CacheControl lookupCacheControl(String urlPath) {
212-
// direct match?
213+
// Direct match?
213214
CacheControl cacheControl = this.cacheControlMappings.get(urlPath);
214215
if (cacheControl == null) {
215-
// pattern match?
216+
// Pattern match?
216217
for (String registeredPath : this.cacheControlMappings.keySet()) {
217218
if (this.pathMatcher.match(registeredPath, urlPath)) {
218219
cacheControl = this.cacheControlMappings.get(registeredPath);
@@ -232,10 +233,10 @@ protected CacheControl lookupCacheControl(String urlPath) {
232233
* @see org.springframework.util.AntPathMatcher
233234
*/
234235
protected Integer lookupCacheSeconds(String urlPath) {
235-
// direct match?
236+
// Direct match?
236237
Integer cacheSeconds = this.cacheMappings.get(urlPath);
237238
if (cacheSeconds == null) {
238-
// pattern match?
239+
// Pattern match?
239240
for (String registeredPath : this.cacheMappings.keySet()) {
240241
if (this.pathMatcher.match(registeredPath, urlPath)) {
241242
cacheSeconds = this.cacheMappings.get(registeredPath);

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@
138138
* @see #setMethodNameResolver
139139
* @see #setWebBindingInitializer
140140
* @see #setSessionAttributeStore
141-
*
142-
* @deprecated in Spring 3.2 in favor of
141+
* @deprecated as of Spring 3.2, in favor of
143142
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter RequestMappingHandlerAdapter}
144143
*/
145144
@Deprecated
@@ -411,13 +410,10 @@ public ModelAndView handle(HttpServletRequest request, HttpServletResponse respo
411410
}
412411

413412
if (annotatedWithSessionAttributes) {
414-
// Always prevent caching in case of session attribute management.
415-
checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers);
416-
// Prepare cached set of session attributes names.
413+
checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true);
417414
}
418415
else {
419-
// Uses configured default cacheSeconds setting.
420-
checkAndPrepare(request, response);
416+
checkAndPrepare(request, response, true);
421417
}
422418

423419
// Execute invokeHandlerMethod in synchronized block if required.

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,13 @@ protected boolean supportsInternal(HandlerMethod handlerMethod) {
705705
protected ModelAndView handleInternal(HttpServletRequest request,
706706
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
707707

708+
checkRequest(request);
709+
708710
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
709-
// Always prevent caching in case of session attribute management.
710-
checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers);
711+
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
711712
}
712713
else {
713-
// Uses configured default cacheSeconds setting.
714-
checkAndPrepare(request, response);
714+
prepareResponse(response);
715715
}
716716

717717
// Execute invokeHandlerMethod in synchronized block if required.

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
import org.springframework.http.HttpHeaders;
4040
import org.springframework.http.HttpRange;
4141
import org.springframework.http.MediaType;
42-
import org.springframework.web.cors.CorsConfiguration;
4342
import org.springframework.http.server.ServletServerHttpRequest;
44-
import org.springframework.web.cors.CorsConfigurationSource;
4543
import org.springframework.util.Assert;
4644
import org.springframework.util.ClassUtils;
4745
import org.springframework.util.CollectionUtils;
@@ -52,6 +50,8 @@
5250
import org.springframework.util.StringUtils;
5351
import org.springframework.web.HttpRequestHandler;
5452
import org.springframework.web.context.request.ServletWebRequest;
53+
import org.springframework.web.cors.CorsConfiguration;
54+
import org.springframework.web.cors.CorsConfigurationSource;
5555
import org.springframework.web.servlet.HandlerMapping;
5656
import org.springframework.web.servlet.support.WebContentGenerator;
5757

@@ -91,7 +91,8 @@
9191
* @author Arjen Poutsma
9292
* @since 3.0.4
9393
*/
94-
public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
94+
public class ResourceHttpRequestHandler extends WebContentGenerator
95+
implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
9596

9697
private static final String CONTENT_ENCODING = "Content-Encoding";
9798

@@ -171,6 +172,12 @@ public void setCorsConfiguration(CorsConfiguration corsConfiguration) {
171172
this.corsConfiguration = corsConfiguration;
172173
}
173174

175+
@Override
176+
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
177+
return this.corsConfiguration;
178+
}
179+
180+
174181
@Override
175182
public void afterPropertiesSet() throws Exception {
176183
if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
@@ -180,11 +187,6 @@ public void afterPropertiesSet() throws Exception {
180187
initAllowedLocations();
181188
}
182189

183-
@Override
184-
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
185-
return corsConfiguration;
186-
}
187-
188190
/**
189191
* Look for a {@link org.springframework.web.servlet.resource.PathResourceResolver}
190192
* among the {@link #getResourceResolvers() resource resolvers} and configure
@@ -207,6 +209,7 @@ protected void initAllowedLocations() {
207209
}
208210
}
209211

212+
210213
/**
211214
* Processes a resource request.
212215
* <p>Checks for the existence of the requested resource in the configured list of locations.
@@ -223,23 +226,27 @@ protected void initAllowedLocations() {
223226
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
224227
throws ServletException, IOException {
225228

226-
checkAndPrepare(request, response);
229+
// Supported methods and required session
230+
checkRequest(request);
227231

228-
// check whether a matching resource exists
232+
// Check whether a matching resource exists
229233
Resource resource = getResource(request);
230234
if (resource == null) {
231235
logger.trace("No matching resource found - returning 404");
232236
response.sendError(HttpServletResponse.SC_NOT_FOUND);
233237
return;
234238
}
235239

236-
// header phase
240+
// Header phase
237241
if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
238242
logger.trace("Resource not modified - returning 304");
239243
return;
240244
}
241245

242-
// check the resource's media type
246+
// Apply cache settings, if any
247+
prepareResponse(response);
248+
249+
// Check the resource's media type
243250
MediaType mediaType = getMediaType(resource);
244251
if (mediaType != null) {
245252
if (logger.isTraceEnabled()) {
@@ -252,7 +259,7 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
252259
}
253260
}
254261

255-
// content phase
262+
// Content phase
256263
if (METHOD_HEAD.equals(request.getMethod())) {
257264
setHeaders(response, resource, mediaType);
258265
logger.trace("HEAD request - skipping content");
@@ -532,15 +539,12 @@ protected void writePartialContent(HttpServletRequest request, HttpServletRespon
532539
}
533540

534541
private void copyRange(InputStream in, OutputStream out, long start, long end) throws IOException {
535-
536542
long skipped = in.skip(start);
537-
538543
if (skipped < start) {
539544
throw new IOException("Skipped only " + skipped + " bytes out of " + start + " required.");
540545
}
541546

542547
long bytesToCopy = end - start + 1;
543-
544548
byte buffer[] = new byte[StreamUtils.BUFFER_SIZE];
545549
while (bytesToCopy > 0) {
546550
int bytesRead = in.read(buffer);
@@ -561,8 +565,7 @@ private void copyRange(InputStream in, OutputStream out, long start, long end) t
561565

562566
@Override
563567
public String toString() {
564-
return "ResourceHttpRequestHandler [locations=" +
565-
getLocations() + ", resolvers=" + getResourceResolvers() + "]";
568+
return "ResourceHttpRequestHandler [locations=" + getLocations() + ", resolvers=" + getResourceResolvers() + "]";
566569
}
567570

568571

0 commit comments

Comments
 (0)