Skip to content

Commit 90d5428

Browse files
committed
Add httpMethods property to LocalChangeInterceptor
Issue: SPR-13032
1 parent d32f577 commit 90d5428

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -20,6 +20,7 @@
2020
import javax.servlet.http.HttpServletRequest;
2121
import javax.servlet.http.HttpServletResponse;
2222

23+
import org.springframework.util.ObjectUtils;
2324
import org.springframework.util.StringUtils;
2425
import org.springframework.web.servlet.LocaleResolver;
2526
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -42,6 +43,8 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
4243

4344
private String paramName = DEFAULT_PARAM_NAME;
4445

46+
private String[] httpMethods;
47+
4548

4649
/**
4750
* Set the name of the parameter that contains a locale specification
@@ -59,21 +62,53 @@ public String getParamName() {
5962
return this.paramName;
6063
}
6164

65+
/**
66+
* Configure the HTTP method(s) over which the locale can be changed.
67+
* @param httpMethods the methods
68+
* @since 4.2
69+
*/
70+
public void setHttpMethods(String... httpMethods) {
71+
this.httpMethods = httpMethods;
72+
}
73+
74+
/**
75+
* Return the configured HTTP methods.
76+
* @since 4.2
77+
*/
78+
public String[] getHttpMethods() {
79+
return this.httpMethods;
80+
}
81+
6282

6383
@Override
6484
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
6585
throws ServletException {
6686

6787
String newLocale = request.getParameter(this.paramName);
6888
if (newLocale != null) {
69-
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
70-
if (localeResolver == null) {
71-
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
89+
if (checkHttpMethod(request.getMethod())) {
90+
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
91+
if (localeResolver == null) {
92+
throw new IllegalStateException(
93+
"No LocaleResolver found: not in a DispatcherServlet request?");
94+
}
95+
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
7296
}
73-
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
7497
}
7598
// Proceed in any case.
7699
return true;
77100
}
78101

102+
private boolean checkHttpMethod(String currentMethod) {
103+
if (ObjectUtils.isEmpty(getHttpMethods())) {
104+
return true;
105+
}
106+
for (String httpMethod : getHttpMethods()) {
107+
if (httpMethod.equalsIgnoreCase(currentMethod)) {
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
79114
}

0 commit comments

Comments
 (0)