1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2022 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.
18
18
19
19
import java .util .Locale ;
20
20
import java .util .TimeZone ;
21
+ import java .util .function .Function ;
21
22
22
23
import jakarta .servlet .http .HttpServletRequest ;
23
24
import jakarta .servlet .http .HttpServletResponse ;
24
25
25
26
import org .springframework .context .i18n .LocaleContext ;
26
27
import org .springframework .context .i18n .TimeZoneAwareLocaleContext ;
27
28
import org .springframework .lang .Nullable ;
29
+ import org .springframework .util .Assert ;
28
30
import org .springframework .web .util .WebUtils ;
29
31
30
32
/**
54
56
* against the current {@code HttpServletRequest}.
55
57
*
56
58
* @author Juergen Hoeller
59
+ * @author Vedran Pavic
57
60
* @since 27.02.2003
58
61
* @see #setDefaultLocale
59
62
* @see #setDefaultTimeZone
@@ -85,6 +88,15 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
85
88
86
89
private String timeZoneAttributeName = TIME_ZONE_SESSION_ATTRIBUTE_NAME ;
87
90
91
+ private Function <HttpServletRequest , Locale > defaultLocaleFunction = request -> {
92
+ Locale defaultLocale = getDefaultLocale ();
93
+ if (defaultLocale == null ) {
94
+ defaultLocale = request .getLocale ();
95
+ }
96
+ return defaultLocale ;
97
+ };
98
+
99
+ private Function <HttpServletRequest , TimeZone > defaultTimeZoneFunction = request -> getDefaultTimeZone ();
88
100
89
101
/**
90
102
* Specify the name of the corresponding attribute in the {@code HttpSession},
@@ -106,12 +118,40 @@ public void setTimeZoneAttributeName(String timeZoneAttributeName) {
106
118
this .timeZoneAttributeName = timeZoneAttributeName ;
107
119
}
108
120
121
+ /**
122
+ * Set the function used to determine the default locale for the given request,
123
+ * called if no {@link Locale} session attribute has been found.
124
+ * <p>The default implementation returns the specified default locale,
125
+ * if any, else falls back to the request's accept-header locale.
126
+ * @param defaultLocaleFunction the function used to determine the default locale
127
+ * @since 6.0
128
+ * @see #setDefaultLocale
129
+ * @see jakarta.servlet.http.HttpServletRequest#getLocale()
130
+ */
131
+ public void setDefaultLocaleFunction (Function <HttpServletRequest , Locale > defaultLocaleFunction ) {
132
+ Assert .notNull (defaultLocaleFunction , "defaultLocaleFunction must not be null" );
133
+ this .defaultLocaleFunction = defaultLocaleFunction ;
134
+ }
135
+
136
+ /**
137
+ * Set the function used to determine the default time zone for the given request,
138
+ * called if no {@link TimeZone} session attribute has been found.
139
+ * <p>The default implementation returns the specified default time zone,
140
+ * if any, or {@code null} otherwise.
141
+ * @param defaultTimeZoneFunction the function used to determine the default time zone
142
+ * @since 6.0
143
+ * @see #setDefaultTimeZone
144
+ */
145
+ public void setDefaultTimeZoneFunction (Function <HttpServletRequest , TimeZone > defaultTimeZoneFunction ) {
146
+ Assert .notNull (defaultTimeZoneFunction , "defaultTimeZoneFunction must not be null" );
147
+ this .defaultTimeZoneFunction = defaultTimeZoneFunction ;
148
+ }
109
149
110
150
@ Override
111
151
public Locale resolveLocale (HttpServletRequest request ) {
112
152
Locale locale = (Locale ) WebUtils .getSessionAttribute (request , this .localeAttributeName );
113
153
if (locale == null ) {
114
- locale = determineDefaultLocale (request );
154
+ locale = this . defaultLocaleFunction . apply (request );
115
155
}
116
156
return locale ;
117
157
}
@@ -123,7 +163,7 @@ public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
123
163
public Locale getLocale () {
124
164
Locale locale = (Locale ) WebUtils .getSessionAttribute (request , localeAttributeName );
125
165
if (locale == null ) {
126
- locale = determineDefaultLocale (request );
166
+ locale = SessionLocaleResolver . this . defaultLocaleFunction . apply (request );
127
167
}
128
168
return locale ;
129
169
}
@@ -132,7 +172,7 @@ public Locale getLocale() {
132
172
public TimeZone getTimeZone () {
133
173
TimeZone timeZone = (TimeZone ) WebUtils .getSessionAttribute (request , timeZoneAttributeName );
134
174
if (timeZone == null ) {
135
- timeZone = determineDefaultTimeZone (request );
175
+ timeZone = SessionLocaleResolver . this . defaultTimeZoneFunction . apply (request );
136
176
}
137
177
return timeZone ;
138
178
}
@@ -165,13 +205,11 @@ public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletRe
165
205
* @return the default locale (never {@code null})
166
206
* @see #setDefaultLocale
167
207
* @see jakarta.servlet.http.HttpServletRequest#getLocale()
208
+ * @deprecated as of 6.0, in favor of {@link #setDefaultLocaleFunction(Function)}
168
209
*/
210
+ @ Deprecated
169
211
protected Locale determineDefaultLocale (HttpServletRequest request ) {
170
- Locale defaultLocale = getDefaultLocale ();
171
- if (defaultLocale == null ) {
172
- defaultLocale = request .getLocale ();
173
- }
174
- return defaultLocale ;
212
+ return this .defaultLocaleFunction .apply (request );
175
213
}
176
214
177
215
/**
@@ -182,10 +220,12 @@ protected Locale determineDefaultLocale(HttpServletRequest request) {
182
220
* @param request the request to resolve the time zone for
183
221
* @return the default time zone (or {@code null} if none defined)
184
222
* @see #setDefaultTimeZone
223
+ * @deprecated as of 6.0, in favor of {@link #setDefaultTimeZoneFunction(Function)}
185
224
*/
225
+ @ Deprecated
186
226
@ Nullable
187
227
protected TimeZone determineDefaultTimeZone (HttpServletRequest request ) {
188
- return getDefaultTimeZone ( );
228
+ return this . defaultTimeZoneFunction . apply ( request );
189
229
}
190
230
191
231
}
0 commit comments