Skip to content

Commit b3d67b9

Browse files
committed
Check supported locales in AcceptHeaderLocaleResolver
Issue: SPR-13330
1 parent a94ffbb commit b3d67b9

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

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

Lines changed: 51 additions & 2 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-2016 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.web.servlet.i18n;
1818

19+
import java.util.ArrayList;
20+
import java.util.Enumeration;
21+
import java.util.List;
1922
import java.util.Locale;
2023
import javax.servlet.http.HttpServletRequest;
2124
import javax.servlet.http.HttpServletResponse;
@@ -31,14 +34,60 @@
3134
* can only be changed through changing the client's locale settings.
3235
*
3336
* @author Juergen Hoeller
37+
* @author Rossen Stoyanchev
3438
* @since 27.02.2003
3539
* @see javax.servlet.http.HttpServletRequest#getLocale()
3640
*/
3741
public class AcceptHeaderLocaleResolver implements LocaleResolver {
3842

43+
private final List<Locale> supportedLocales = new ArrayList<Locale>();
44+
45+
46+
/**
47+
* Configure supported locales to check against the requested locales
48+
* determined via {@link HttpServletRequest#getLocales()}. If this is not
49+
* configured then {@link HttpServletRequest#getLocale()} is used instead.
50+
* @param locales the supported locales
51+
* @since 4.3
52+
*/
53+
public void setSupportedLocales(List<Locale> locales) {
54+
this.supportedLocales.clear();
55+
if (locales != null) {
56+
this.supportedLocales.addAll(locales);
57+
}
58+
}
59+
60+
/**
61+
* Return the configured list of supported locales.
62+
* @since 4.3
63+
*/
64+
public List<Locale> getSupportedLocales() {
65+
return this.supportedLocales;
66+
}
67+
68+
3969
@Override
4070
public Locale resolveLocale(HttpServletRequest request) {
41-
return request.getLocale();
71+
Locale locale = request.getLocale();
72+
if (!isSupportedLocale(locale)) {
73+
locale = findSupportedLocale(request, locale);
74+
}
75+
return locale;
76+
}
77+
78+
private boolean isSupportedLocale(Locale locale) {
79+
return (getSupportedLocales().isEmpty() || getSupportedLocales().contains(locale));
80+
}
81+
82+
private Locale findSupportedLocale(HttpServletRequest request, Locale fallback) {
83+
Enumeration<Locale> requestLocales = request.getLocales();
84+
while (requestLocales.hasMoreElements()) {
85+
Locale locale = requestLocales.nextElement();
86+
if (getSupportedLocales().contains(locale)) {
87+
return locale;
88+
}
89+
}
90+
return fallback;
4291
}
4392

4493
@Override
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.servlet.i18n;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.Locale;
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
import org.junit.Test;
24+
25+
import org.springframework.mock.web.test.MockHttpServletRequest;
26+
27+
import static java.util.Locale.CANADA;
28+
import static java.util.Locale.UK;
29+
import static java.util.Locale.US;
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Unit tests for {@link AcceptHeaderLocaleResolver}.
34+
* @author Rossen Stoyanchev
35+
*/
36+
public class AcceptHeaderLocaleResolverTests {
37+
38+
private AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
39+
40+
41+
@Test
42+
public void resolve() throws Exception {
43+
assertEquals(CANADA, this.resolver.resolveLocale(request(CANADA)));
44+
assertEquals(US, this.resolver.resolveLocale(request(US, CANADA)));
45+
}
46+
47+
@Test
48+
public void resolvePreferredSupported() throws Exception {
49+
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
50+
assertEquals(CANADA, this.resolver.resolveLocale(request(US, CANADA)));
51+
}
52+
53+
@Test
54+
public void resolvePreferredNotSupported() throws Exception {
55+
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
56+
assertEquals(US, this.resolver.resolveLocale(request(US, UK)));
57+
}
58+
59+
60+
private HttpServletRequest request(Locale... locales) {
61+
MockHttpServletRequest request = new MockHttpServletRequest();
62+
request.setPreferredLocales(Arrays.asList(locales));
63+
return request;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)