Skip to content

Commit ec7d80b

Browse files
committed
Polishing
1 parent 21f9ca0 commit ec7d80b

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

spring-web/src/main/java/org/springframework/web/filter/OncePerRequestFilter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.filter;
1818

1919
import java.io.IOException;
20-
2120
import javax.servlet.FilterChain;
2221
import javax.servlet.ServletException;
2322
import javax.servlet.ServletRequest;
@@ -35,8 +34,8 @@
3534
* method with HttpServletRequest and HttpServletResponse arguments.
3635
*
3736
* <p>As of Servlet 3.0, a filter may be invoked as part of a
38-
* {@link javax.servlet.DispatcherType.REQUEST REQUEST} or
39-
* {@link javax.servlet.DispatcherType.ASYNC ASYNC} dispatches that occur in
37+
* {@link javax.servlet.DispatcherType#REQUEST REQUEST} or
38+
* {@link javax.servlet.DispatcherType#ASYNC ASYNC} dispatches that occur in
4039
* separate threads. A filter can be configured in {@code web.xml} whether it
4140
* should be involved in async dispatches. However, in some cases servlet
4241
* containers assume different default configuration. Therefore sub-classes can
@@ -53,7 +52,7 @@
5352
* won't be the last one.
5453
*
5554
* <p>Yet another dispatch type that also occurs in its own thread is
56-
* {@link javax.servlet.DispatcherType.ERROR ERROR}. Sub-classes can override
55+
* {@link javax.servlet.DispatcherType#ERROR ERROR}. Sub-classes can override
5756
* {@link #shouldNotFilterErrorDispatch()} if they wish to declare statically
5857
* if they should be invoked <em>once</em> during error dispatches.
5958
*
@@ -129,7 +128,6 @@ private boolean skipDispatch(HttpServletRequest request) {
129128
* in Servlet 3.0 means a filter can be invoked in more than one thread over
130129
* the course of a single request. This method returns {@code true} if the
131130
* filter is currently executing within an asynchronous dispatch.
132-
*
133131
* @param request the current request
134132
* @see WebAsyncManager#hasConcurrentResult()
135133
*/
@@ -140,7 +138,6 @@ protected boolean isAsyncDispatch(HttpServletRequest request) {
140138
/**
141139
* Whether request processing is in asynchronous mode meaning that the
142140
* response will not be committed after the current thread is exited.
143-
*
144141
* @param request the current request
145142
* @see WebAsyncManager#isConcurrentHandlingStarted()
146143
*/
@@ -151,7 +148,7 @@ protected boolean isAsyncStarted(HttpServletRequest request) {
151148
/**
152149
* Return the name of the request attribute that identifies that a request
153150
* is already filtered.
154-
* <p>Default implementation takes the configured name of the concrete filter
151+
* <p>The default implementation takes the configured name of the concrete filter
155152
* instance and appends ".FILTERED". If the filter is not fully initialized,
156153
* it falls back to its class name.
157154
* @see #getFilterName
@@ -188,7 +185,6 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce
188185
* types via {@code web.xml} or in Java through the {@code ServletContext},
189186
* servlet containers may enforce different defaults with regards to
190187
* dispatcher types. This flag enforces the design intent of the filter.
191-
*
192188
* <p>The default return value is "true", which means the filter will not be
193189
* invoked during subsequent async dispatches. If "false", the filter will
194190
* be invoked during async dispatches with the same guarantees of being

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.LinkedHashSet;
2323
import java.util.List;
2424
import java.util.Set;
25-
2625
import javax.servlet.http.HttpServletRequest;
2726
import javax.servlet.http.HttpServletResponse;
2827

@@ -41,8 +40,8 @@
4140
import org.springframework.web.servlet.HandlerMapping;
4241

4342
/**
44-
* Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle method return
45-
* values by writing to the response with {@link HttpMessageConverter}s.
43+
* Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle
44+
* method return values by writing to the response with {@link HttpMessageConverter}s.
4645
*
4746
* @author Arjen Poutsma
4847
* @author Rossen Stoyanchev
@@ -55,6 +54,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
5554

5655
private final ContentNegotiationManager contentNegotiationManager;
5756

57+
5858
protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> messageConverters) {
5959
this(messageConverters, null);
6060
}
@@ -63,12 +63,12 @@ protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>>
6363
ContentNegotiationManager manager) {
6464

6565
super(messageConverters);
66-
this.contentNegotiationManager = (manager != null) ? manager : new ContentNegotiationManager();
66+
this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager());
6767
}
6868

69+
6970
/**
7071
* Creates a new {@link HttpOutputMessage} from the given {@link NativeWebRequest}.
71-
*
7272
* @param webRequest the web request to create an output message from
7373
* @return the output message
7474
*/
@@ -81,18 +81,16 @@ protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequ
8181
* Writes the given return value to the given web request. Delegates to
8282
* {@link #writeWithMessageConverters(Object, MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)}
8383
*/
84-
protected <T> void writeWithMessageConverters(T returnValue,
85-
MethodParameter returnType,
86-
NativeWebRequest webRequest)
84+
protected <T> void writeWithMessageConverters(T returnValue, MethodParameter returnType, NativeWebRequest webRequest)
8785
throws IOException, HttpMediaTypeNotAcceptableException {
86+
8887
ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
8988
ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
9089
writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
9190
}
9291

9392
/**
9493
* Writes the given return type to the given output message.
95-
*
9694
* @param returnValue the value to write to the output message
9795
* @param returnType the type of the value
9896
* @param inputMessage the input messages. Used to inspect the {@code Accept} header.
@@ -102,23 +100,20 @@ protected <T> void writeWithMessageConverters(T returnValue,
102100
* the request cannot be met by the message converters
103101
*/
104102
@SuppressWarnings("unchecked")
105-
protected <T> void writeWithMessageConverters(T returnValue,
106-
MethodParameter returnType,
107-
ServletServerHttpRequest inputMessage,
108-
ServletServerHttpResponse outputMessage)
103+
protected <T> void writeWithMessageConverters(T returnValue, MethodParameter returnType,
104+
ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
109105
throws IOException, HttpMediaTypeNotAcceptableException {
110106

111107
Class<?> returnValueClass = returnValue.getClass();
112-
113108
HttpServletRequest servletRequest = inputMessage.getServletRequest();
114109
List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(servletRequest);
115110
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(servletRequest, returnValueClass);
116111

117112
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
118-
for (MediaType r : requestedMediaTypes) {
119-
for (MediaType p : producibleMediaTypes) {
120-
if (r.isCompatibleWith(p)) {
121-
compatibleMediaTypes.add(getMostSpecificMediaType(r, p));
113+
for (MediaType requestedType : requestedMediaTypes) {
114+
for (MediaType producibleType : producibleMediaTypes) {
115+
if (requestedType.isCompatibleWith(producibleType)) {
116+
compatibleMediaTypes.add(getMostSpecificMediaType(requestedType, producibleType));
122117
}
123118
}
124119
}
@@ -143,7 +138,7 @@ else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICAT
143138

144139
if (selectedMediaType != null) {
145140
selectedMediaType = selectedMediaType.removeQualityValue();
146-
for (HttpMessageConverter<?> messageConverter : messageConverters) {
141+
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
147142
if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
148143
((HttpMessageConverter<T>) messageConverter).write(returnValue, selectedMediaType, outputMessage);
149144
if (logger.isDebugEnabled()) {
@@ -154,15 +149,15 @@ else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICAT
154149
}
155150
}
156151
}
157-
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
152+
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
158153
}
159154

160155
/**
161156
* Returns the media types that can be produced:
162157
* <ul>
163-
* <li>The producible media types specified in the request mappings, or
164-
* <li>Media types of configured converters that can write the specific return value, or
165-
* <li>{@link MediaType#ALL}
158+
* <li>The producible media types specified in the request mappings, or
159+
* <li>Media types of configured converters that can write the specific return value, or
160+
* <li>{@link MediaType#ALL}
166161
* </ul>
167162
*/
168163
@SuppressWarnings("unchecked")
@@ -171,9 +166,9 @@ protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Cl
171166
if (!CollectionUtils.isEmpty(mediaTypes)) {
172167
return new ArrayList<MediaType>(mediaTypes);
173168
}
174-
else if (!allSupportedMediaTypes.isEmpty()) {
169+
else if (!this.allSupportedMediaTypes.isEmpty()) {
175170
List<MediaType> result = new ArrayList<MediaType>();
176-
for (HttpMessageConverter<?> converter : messageConverters) {
171+
for (HttpMessageConverter<?> converter : this.messageConverters) {
177172
if (converter.canWrite(returnValueClass, null)) {
178173
result.addAll(converter.getSupportedMediaTypes());
179174
}
@@ -187,16 +182,16 @@ else if (!allSupportedMediaTypes.isEmpty()) {
187182

188183
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
189184
List<MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
190-
return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;
185+
return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
191186
}
192187

193188
/**
194189
* Return the more specific of the acceptable and the producible media types
195190
* with the q-value of the former.
196191
*/
197192
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
198-
produceType = produceType.copyQualityValue(acceptType);
199-
return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) <= 0 ? acceptType : produceType;
193+
MediaType produceTypeToUse = produceType.copyQualityValue(acceptType);
194+
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
200195
}
201196

202197
}

0 commit comments

Comments
 (0)