Skip to content

Commit c34a62e

Browse files
committed
LocaleChangeInterceptor allows for ignoring invalid locale values
Issue: SPR-9456
1 parent df171ff commit c34a62e

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/LocaleChangeInterceptor.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import javax.servlet.http.HttpServletRequest;
2121
import javax.servlet.http.HttpServletResponse;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
2326
import org.springframework.util.ObjectUtils;
2427
import org.springframework.util.StringUtils;
2528
import org.springframework.web.servlet.LocaleResolver;
@@ -31,6 +34,7 @@
3134
* via a configurable request parameter (default parameter name: "locale").
3235
*
3336
* @author Juergen Hoeller
37+
* @author Rossen Stoyanchev
3438
* @since 20.06.2003
3539
* @see org.springframework.web.servlet.LocaleResolver
3640
*/
@@ -41,10 +45,15 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
4145
*/
4246
public static final String DEFAULT_PARAM_NAME = "locale";
4347

48+
49+
protected final Log logger = LogFactory.getLog(getClass());
50+
4451
private String paramName = DEFAULT_PARAM_NAME;
4552

4653
private String[] httpMethods;
4754

55+
private boolean ignoreInvalidLocale = false;
56+
4857

4958
/**
5059
* Set the name of the parameter that contains a locale specification
@@ -79,32 +88,59 @@ public String[] getHttpMethods() {
7988
return this.httpMethods;
8089
}
8190

91+
/**
92+
* Set whether to ignore an invalid value for the locale parameter.
93+
* @since 4.2.2
94+
*/
95+
public void setIgnoreInvalidLocale(boolean ignoreInvalidLocale) {
96+
this.ignoreInvalidLocale = ignoreInvalidLocale;
97+
}
98+
99+
/**
100+
* Return whether to ignore an invalid value for the locale parameter.
101+
* @since 4.2.2
102+
*/
103+
public boolean isIgnoreInvalidLocale() {
104+
return this.ignoreInvalidLocale;
105+
}
106+
82107

83108
@Override
84109
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
85110
throws ServletException {
86111

87-
String newLocale = request.getParameter(this.paramName);
112+
String newLocale = request.getParameter(getParamName());
88113
if (newLocale != null) {
89114
if (checkHttpMethod(request.getMethod())) {
90115
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
91116
if (localeResolver == null) {
92117
throw new IllegalStateException(
93118
"No LocaleResolver found: not in a DispatcherServlet request?");
94119
}
95-
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
120+
try {
121+
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
122+
}
123+
catch (IllegalArgumentException ex) {
124+
if (isIgnoreInvalidLocale()) {
125+
logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
126+
}
127+
else {
128+
throw ex;
129+
}
130+
}
96131
}
97132
}
98133
// Proceed in any case.
99134
return true;
100135
}
101136

102137
private boolean checkHttpMethod(String currentMethod) {
103-
if (ObjectUtils.isEmpty(getHttpMethods())) {
138+
String[] configuredMethods = getHttpMethods();
139+
if (ObjectUtils.isEmpty(configuredMethods)) {
104140
return true;
105141
}
106-
for (String httpMethod : getHttpMethods()) {
107-
if (httpMethod.equalsIgnoreCase(currentMethod)) {
142+
for (String configuredMethod : configuredMethods) {
143+
if (configuredMethod.equalsIgnoreCase(currentMethod)) {
108144
return true;
109145
}
110146
}

0 commit comments

Comments
 (0)