Skip to content

Commit 499be70

Browse files
committed
Update async dispatch check in OncePerRequestFilter
We no longer need to rely on an indirect check since Servlet 3.0 is expected so we can just check the DispatcherType of the request. Closes gh-26282
1 parent 0cf5005 commit 499be70

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -145,7 +145,7 @@ private boolean skipDispatch(HttpServletRequest request) {
145145
* @see WebAsyncManager#hasConcurrentResult()
146146
*/
147147
protected boolean isAsyncDispatch(HttpServletRequest request) {
148-
return WebAsyncUtils.getAsyncManager(request).hasConcurrentResult();
148+
return request.getDispatcherType().equals(DispatcherType.ASYNC);
149149
}
150150

151151
/**

spring-web/src/test/java/org/springframework/web/filter/CharacterEncodingFilterTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.filter;
1818

19+
import javax.servlet.DispatcherType;
1920
import javax.servlet.FilterChain;
2021
import javax.servlet.http.HttpServletRequest;
2122
import javax.servlet.http.HttpServletResponse;
@@ -51,6 +52,7 @@ public void forceEncodingAlwaysSetsEncoding() throws Exception {
5152
request.setCharacterEncoding(ENCODING);
5253
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
5354
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
55+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
5456

5557
HttpServletResponse response = mock(HttpServletResponse.class);
5658
FilterChain filterChain = mock(FilterChain.class);
@@ -71,6 +73,7 @@ public void encodingIfEmptyAndNotForced() throws Exception {
7173
given(request.getCharacterEncoding()).willReturn(null);
7274
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
7375
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
76+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
7477

7578
MockHttpServletResponse response = new MockHttpServletResponse();
7679

@@ -92,6 +95,7 @@ public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
9295
given(request.getCharacterEncoding()).willReturn(ENCODING);
9396
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
9497
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
98+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
9599

96100
MockHttpServletResponse response = new MockHttpServletResponse();
97101

@@ -112,6 +116,7 @@ public void withBeanInitialization() throws Exception {
112116
given(request.getCharacterEncoding()).willReturn(null);
113117
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
114118
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
119+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
115120

116121
MockHttpServletResponse response = new MockHttpServletResponse();
117122

@@ -135,6 +140,7 @@ public void withIncompleteInitialization() throws Exception {
135140
given(request.getCharacterEncoding()).willReturn(null);
136141
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
137142
given(request.getAttribute(filteredName(CharacterEncodingFilter.class.getName()))).willReturn(null);
143+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
138144

139145
MockHttpServletResponse response = new MockHttpServletResponse();
140146

@@ -156,6 +162,7 @@ public void setForceEncodingOnRequestOnly() throws Exception {
156162
request.setCharacterEncoding(ENCODING);
157163
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
158164
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
165+
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
159166

160167
HttpServletResponse response = mock(HttpServletResponse.class);
161168
FilterChain filterChain = mock(FilterChain.class);

0 commit comments

Comments
 (0)