1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -81,6 +81,8 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
81
81
public static final String DEFAULT_COOKIE_NAME = CookieLocaleResolver .class .getName () + ".LOCALE" ;
82
82
83
83
84
+ private boolean languageTagCompliant = false ;
85
+
84
86
private Locale defaultLocale ;
85
87
86
88
private TimeZone defaultTimeZone ;
@@ -94,6 +96,30 @@ public CookieLocaleResolver() {
94
96
setCookieName (DEFAULT_COOKIE_NAME );
95
97
}
96
98
99
+
100
+ /**
101
+ * Specify whether this resolver's cookies should be compliant with BCP 47
102
+ * language tags instead of Java's legacy locale specification format.
103
+ * The default is {@code false}.
104
+ * <p>Note: This mode requires JDK 7 or higher. Set this flag to {@code true}
105
+ * for BCP 47 compliance on JDK 7+ only.
106
+ * @since 4.3
107
+ * @see Locale#forLanguageTag(String)
108
+ * @see Locale#toLanguageTag()
109
+ */
110
+ public void setLanguageTagCompliant (boolean languageTagCompliant ) {
111
+ this .languageTagCompliant = languageTagCompliant ;
112
+ }
113
+
114
+ /**
115
+ * Return whether this resolver's cookies should be compliant with BCP 47
116
+ * language tags instead of Java's legacy locale specification format.
117
+ * @since 4.3
118
+ */
119
+ public boolean isLanguageTagCompliant () {
120
+ return this .languageTagCompliant ;
121
+ }
122
+
97
123
/**
98
124
* Set a fixed Locale that this resolver will return if no cookie found.
99
125
*/
@@ -111,6 +137,7 @@ protected Locale getDefaultLocale() {
111
137
112
138
/**
113
139
* Set a fixed TimeZone that this resolver will return if no cookie found.
140
+ * @since 4.0
114
141
*/
115
142
public void setDefaultTimeZone (TimeZone defaultTimeZone ) {
116
143
this .defaultTimeZone = defaultTimeZone ;
@@ -119,6 +146,7 @@ public void setDefaultTimeZone(TimeZone defaultTimeZone) {
119
146
/**
120
147
* Return the fixed TimeZone that this resolver will return if no cookie found,
121
148
* if any.
149
+ * @since 4.0
122
150
*/
123
151
protected TimeZone getDefaultTimeZone () {
124
152
return this .defaultTimeZone ;
@@ -161,7 +189,7 @@ private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
161
189
localePart = value .substring (0 , spaceIndex );
162
190
timeZonePart = value .substring (spaceIndex + 1 );
163
191
}
164
- locale = (!"-" .equals (localePart ) ? StringUtils . parseLocaleString (localePart ) : null );
192
+ locale = (!"-" .equals (localePart ) ? parseLocaleValue (localePart ) : null );
165
193
if (timeZonePart != null ) {
166
194
timeZone = StringUtils .parseTimeZoneString (timeZonePart );
167
195
}
@@ -171,7 +199,7 @@ private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
171
199
}
172
200
}
173
201
request .setAttribute (LOCALE_REQUEST_ATTRIBUTE_NAME ,
174
- (locale != null ? locale : determineDefaultLocale (request )));
202
+ (locale != null ? locale : determineDefaultLocale (request )));
175
203
request .setAttribute (TIME_ZONE_REQUEST_ATTRIBUTE_NAME ,
176
204
(timeZone != null ? timeZone : determineDefaultTimeZone (request )));
177
205
}
@@ -191,18 +219,45 @@ public void setLocaleContext(HttpServletRequest request, HttpServletResponse res
191
219
if (localeContext instanceof TimeZoneAwareLocaleContext ) {
192
220
timeZone = ((TimeZoneAwareLocaleContext ) localeContext ).getTimeZone ();
193
221
}
194
- addCookie (response , (locale != null ? locale : "-" ) + (timeZone != null ? ' ' + timeZone .getID () : "" ));
222
+ addCookie (response ,
223
+ (locale != null ? toLocaleValue (locale ) : "-" ) + (timeZone != null ? ' ' + timeZone .getID () : "" ));
195
224
}
196
225
else {
197
226
removeCookie (response );
198
227
}
199
228
request .setAttribute (LOCALE_REQUEST_ATTRIBUTE_NAME ,
200
- (locale != null ? locale : determineDefaultLocale (request )));
229
+ (locale != null ? locale : determineDefaultLocale (request )));
201
230
request .setAttribute (TIME_ZONE_REQUEST_ATTRIBUTE_NAME ,
202
231
(timeZone != null ? timeZone : determineDefaultTimeZone (request )));
203
232
}
204
233
205
234
235
+ /**
236
+ * Parse the given locale value coming from an incoming cookie.
237
+ * <p>The default implementation calls {@link StringUtils#parseLocaleString(String)}
238
+ * or JDK 7's {@link Locale#forLanguageTag(String)}, depending on the
239
+ * {@link #setLanguageTagCompliant "languageTagCompliant"} configuration property.
240
+ * @param locale the locale value to parse
241
+ * @return the corresponding {@code Locale} instance
242
+ * @since 4.3
243
+ */
244
+ protected Locale parseLocaleValue (String locale ) {
245
+ return (isLanguageTagCompliant () ? Locale .forLanguageTag (locale ) : StringUtils .parseLocaleString (locale ));
246
+ }
247
+
248
+ /**
249
+ * Render the given locale as a text value for inclusion in a cookie.
250
+ * <p>The default implementation calls {@link Locale#toString()}
251
+ * or JDK 7's {@link Locale#toLanguageTag()}, depending on the
252
+ * {@link #setLanguageTagCompliant "languageTagCompliant"} configuration property.
253
+ * @param locale the locale to stringify
254
+ * @return a String representation for the given locale
255
+ * @since 4.3
256
+ */
257
+ protected String toLocaleValue (Locale locale ) {
258
+ return (isLanguageTagCompliant () ? locale .toLanguageTag () : locale .toString ());
259
+ }
260
+
206
261
/**
207
262
* Determine the default locale for the given request,
208
263
* Called if no locale cookie has been found.
0 commit comments