17
17
package org .springframework .web .filter ;
18
18
19
19
import java .io .IOException ;
20
-
21
20
import javax .servlet .FilterChain ;
22
21
import javax .servlet .ServletException ;
23
22
import javax .servlet .http .HttpServletRequest ;
@@ -90,7 +89,7 @@ public CharacterEncodingFilter(String encoding, boolean forceEncoding) {
90
89
* override existing request encodings
91
90
* @param forceResponseEncoding whether the specified encoding is supposed to
92
91
* override existing response encodings
93
- * @since 4.3.0
92
+ * @since 4.3
94
93
* @see #setEncoding
95
94
* @see #setForceRequestEncoding(boolean)
96
95
* @see #setForceResponseEncoding(boolean)
@@ -102,6 +101,7 @@ public CharacterEncodingFilter(String encoding, boolean forceRequestEncoding, bo
102
101
this .forceResponseEncoding = forceResponseEncoding ;
103
102
}
104
103
104
+
105
105
/**
106
106
* Set the encoding to use for requests. This encoding will be passed into a
107
107
* {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
@@ -115,9 +115,10 @@ public void setEncoding(String encoding) {
115
115
116
116
/**
117
117
* Return the configured encoding for requests and/or responses
118
+ * @since 4.3
118
119
*/
119
120
public String getEncoding () {
120
- return encoding ;
121
+ return this . encoding ;
121
122
}
122
123
123
124
/**
@@ -144,17 +145,18 @@ public void setForceEncoding(boolean forceEncoding) {
144
145
* {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
145
146
* returns a non-null value. Switch this to "true" to enforce the specified
146
147
* encoding in any case.
147
- * @since 4.3.0
148
+ * @since 4.3
148
149
*/
149
150
public void setForceRequestEncoding (boolean forceRequestEncoding ) {
150
151
this .forceRequestEncoding = forceRequestEncoding ;
151
152
}
152
153
153
154
/**
154
155
* Return whether the encoding should be forced on requests
156
+ * @since 4.3
155
157
*/
156
158
public boolean isForceRequestEncoding () {
157
- return forceRequestEncoding ;
159
+ return this . forceRequestEncoding ;
158
160
}
159
161
160
162
/**
@@ -163,29 +165,33 @@ public boolean isForceRequestEncoding() {
163
165
* <p>Default is "false", i.e. do not modify the encoding.
164
166
* Switch this to "true" to enforce the specified encoding
165
167
* for responses in any case.
166
- * @since 4.3.0
168
+ * @since 4.3
167
169
*/
168
170
public void setForceResponseEncoding (boolean forceResponseEncoding ) {
169
171
this .forceResponseEncoding = forceResponseEncoding ;
170
172
}
171
173
172
174
/**
173
- * Return whether the encoding should be forced on responses
175
+ * Return whether the encoding should be forced on responses.
176
+ * @since 4.3
174
177
*/
175
178
public boolean isForceResponseEncoding () {
176
- return forceResponseEncoding ;
179
+ return this . forceResponseEncoding ;
177
180
}
178
181
182
+
179
183
@ Override
180
184
protected void doFilterInternal (
181
185
HttpServletRequest request , HttpServletResponse response , FilterChain filterChain )
182
186
throws ServletException , IOException {
183
- if (this .encoding != null ) {
184
- if (this .forceRequestEncoding || request .getCharacterEncoding () == null ) {
185
- request .setCharacterEncoding (this .encoding );
187
+
188
+ String encoding = getEncoding ();
189
+ if (encoding != null ) {
190
+ if (isForceRequestEncoding () || request .getCharacterEncoding () == null ) {
191
+ request .setCharacterEncoding (encoding );
186
192
}
187
- if (this . forceResponseEncoding ) {
188
- response .setCharacterEncoding (this . encoding );
193
+ if (isForceResponseEncoding () ) {
194
+ response .setCharacterEncoding (encoding );
189
195
}
190
196
}
191
197
filterChain .doFilter (request , response );
0 commit comments