LocaleChangeInterceptor allows for ignoring invalid locale values
Issue: SPR-9456
This commit is contained in:
parent
df171ff5bb
commit
c34a62e141
|
@ -20,6 +20,9 @@ import javax.servlet.ServletException;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
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.StringUtils;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
@ -31,6 +34,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
|
|||
* via a configurable request parameter (default parameter name: "locale").
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 20.06.2003
|
||||
* @see org.springframework.web.servlet.LocaleResolver
|
||||
*/
|
||||
|
@ -41,10 +45,15 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
|
|||
*/
|
||||
public static final String DEFAULT_PARAM_NAME = "locale";
|
||||
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private String paramName = DEFAULT_PARAM_NAME;
|
||||
|
||||
private String[] httpMethods;
|
||||
|
||||
private boolean ignoreInvalidLocale = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set the name of the parameter that contains a locale specification
|
||||
|
@ -79,12 +88,28 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
|
|||
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
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws ServletException {
|
||||
|
||||
String newLocale = request.getParameter(this.paramName);
|
||||
String newLocale = request.getParameter(getParamName());
|
||||
if (newLocale != null) {
|
||||
if (checkHttpMethod(request.getMethod())) {
|
||||
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
|
||||
|
@ -92,7 +117,17 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
|
|||
throw new IllegalStateException(
|
||||
"No LocaleResolver found: not in a DispatcherServlet request?");
|
||||
}
|
||||
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
|
||||
try {
|
||||
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.
|
||||
|
@ -100,11 +135,12 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
|
|||
}
|
||||
|
||||
private boolean checkHttpMethod(String currentMethod) {
|
||||
if (ObjectUtils.isEmpty(getHttpMethods())) {
|
||||
String[] configuredMethods = getHttpMethods();
|
||||
if (ObjectUtils.isEmpty(configuredMethods)) {
|
||||
return true;
|
||||
}
|
||||
for (String httpMethod : getHttpMethods()) {
|
||||
if (httpMethod.equalsIgnoreCase(currentMethod)) {
|
||||
for (String configuredMethod : configuredMethods) {
|
||||
if (configuredMethod.equalsIgnoreCase(currentMethod)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue