提交 c34a62e1 编写于 作者: J Juergen Hoeller

LocaleChangeInterceptor allows for ignoring invalid locale values

Issue: SPR-9456
上级 df171ff5
...@@ -20,6 +20,9 @@ import javax.servlet.ServletException; ...@@ -20,6 +20,9 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
...@@ -31,6 +34,7 @@ import org.springframework.web.servlet.support.RequestContextUtils; ...@@ -31,6 +34,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
* via a configurable request parameter (default parameter name: "locale"). * via a configurable request parameter (default parameter name: "locale").
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev
* @since 20.06.2003 * @since 20.06.2003
* @see org.springframework.web.servlet.LocaleResolver * @see org.springframework.web.servlet.LocaleResolver
*/ */
...@@ -41,10 +45,15 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { ...@@ -41,10 +45,15 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
*/ */
public static final String DEFAULT_PARAM_NAME = "locale"; public static final String DEFAULT_PARAM_NAME = "locale";
protected final Log logger = LogFactory.getLog(getClass());
private String paramName = DEFAULT_PARAM_NAME; private String paramName = DEFAULT_PARAM_NAME;
private String[] httpMethods; private String[] httpMethods;
private boolean ignoreInvalidLocale = false;
/** /**
* Set the name of the parameter that contains a locale specification * Set the name of the parameter that contains a locale specification
...@@ -79,12 +88,28 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { ...@@ -79,12 +88,28 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
return this.httpMethods; return this.httpMethods;
} }
/**
* Set whether to ignore an invalid value for the locale parameter.
* @since 4.2.2
*/
public void setIgnoreInvalidLocale(boolean ignoreInvalidLocale) {
this.ignoreInvalidLocale = ignoreInvalidLocale;
}
/**
* Return whether to ignore an invalid value for the locale parameter.
* @since 4.2.2
*/
public boolean isIgnoreInvalidLocale() {
return this.ignoreInvalidLocale;
}
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException { throws ServletException {
String newLocale = request.getParameter(this.paramName); String newLocale = request.getParameter(getParamName());
if (newLocale != null) { if (newLocale != null) {
if (checkHttpMethod(request.getMethod())) { if (checkHttpMethod(request.getMethod())) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
...@@ -92,19 +117,30 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { ...@@ -92,19 +117,30 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
throw new IllegalStateException( throw new IllegalStateException(
"No LocaleResolver found: not in a DispatcherServlet request?"); "No LocaleResolver found: not in a DispatcherServlet request?");
} }
try {
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale)); localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
} }
catch (IllegalArgumentException ex) {
if (isIgnoreInvalidLocale()) {
logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
}
else {
throw ex;
}
}
}
} }
// Proceed in any case. // Proceed in any case.
return true; return true;
} }
private boolean checkHttpMethod(String currentMethod) { private boolean checkHttpMethod(String currentMethod) {
if (ObjectUtils.isEmpty(getHttpMethods())) { String[] configuredMethods = getHttpMethods();
if (ObjectUtils.isEmpty(configuredMethods)) {
return true; return true;
} }
for (String httpMethod : getHttpMethods()) { for (String configuredMethod : configuredMethods) {
if (httpMethod.equalsIgnoreCase(currentMethod)) { if (configuredMethod.equalsIgnoreCase(currentMethod)) {
return true; return true;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册