1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.localization.impl; |
18 |
|
|
19 |
|
import java.security.Principal; |
20 |
|
import java.util.Enumeration; |
21 |
|
import java.util.Locale; |
22 |
|
import java.util.prefs.Preferences; |
23 |
|
|
24 |
|
import javax.security.auth.Subject; |
25 |
|
|
26 |
|
import org.apache.commons.logging.Log; |
27 |
|
import org.apache.commons.logging.LogFactory; |
28 |
|
import org.apache.jetspeed.Jetspeed; |
29 |
|
import org.apache.jetspeed.PortalReservedParameters; |
30 |
|
import org.apache.jetspeed.i18n.CurrentLocale; |
31 |
|
import org.apache.jetspeed.pipeline.PipelineException; |
32 |
|
import org.apache.jetspeed.pipeline.valve.AbstractValve; |
33 |
|
import org.apache.jetspeed.pipeline.valve.LocalizationValve; |
34 |
|
import org.apache.jetspeed.pipeline.valve.ValveContext; |
35 |
|
import org.apache.jetspeed.request.RequestContext; |
36 |
|
import org.apache.jetspeed.security.SecurityException; |
37 |
|
import org.apache.jetspeed.security.SecurityHelper; |
38 |
|
import org.apache.jetspeed.security.User; |
39 |
|
import org.apache.jetspeed.security.UserManager; |
40 |
|
import org.apache.jetspeed.security.UserPrincipal; |
41 |
|
import org.apache.jetspeed.util.JetspeedLocale; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public class LocalizationValveImpl extends AbstractValve implements LocalizationValve |
50 |
|
{ |
51 |
0 |
private static final Log log = LogFactory.getLog(LocalizationValveImpl.class); |
52 |
0 |
private Locale defaultLocale = null; |
53 |
|
|
54 |
0 |
public LocalizationValveImpl() {} |
55 |
|
|
56 |
|
public LocalizationValveImpl(String defaultLanguage) |
57 |
0 |
{ |
58 |
0 |
String language = defaultLanguage != null ? defaultLanguage.trim() : ""; |
59 |
0 |
if (language.length()>0) |
60 |
|
{ |
61 |
|
|
62 |
0 |
String country = ""; |
63 |
0 |
String variant = ""; |
64 |
0 |
int countryIndex = language.indexOf('_'); |
65 |
0 |
if (countryIndex > -1) |
66 |
|
{ |
67 |
0 |
country = language.substring(countryIndex + 1).trim(); |
68 |
0 |
language = language.substring(0, countryIndex).trim(); |
69 |
0 |
int vDash = country.indexOf("_"); |
70 |
0 |
if (vDash > 0) |
71 |
|
{ |
72 |
0 |
String cTemp = country.substring(0, vDash); |
73 |
0 |
variant = country.substring(vDash + 1); |
74 |
0 |
country = cTemp; |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
0 |
defaultLocale = new Locale(language, country, variant); |
79 |
0 |
if ( defaultLocale.getLanguage().length() == 0 ) |
80 |
|
{ |
81 |
|
|
82 |
0 |
defaultLocale = null; |
83 |
0 |
log.warn("Invalid or unrecognized default language: "+language); |
84 |
|
} |
85 |
|
else |
86 |
|
{ |
87 |
0 |
log.info("Default language set: "+defaultLocale); |
88 |
|
} |
89 |
|
|
90 |
|
} |
91 |
0 |
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
public void invoke( RequestContext request, ValveContext context ) throws PipelineException |
100 |
|
{ |
101 |
0 |
Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE); |
102 |
0 |
if (null == locale) |
103 |
|
{ |
104 |
|
|
105 |
0 |
Subject subject = request.getSubject(); |
106 |
0 |
if (null != subject) |
107 |
|
{ |
108 |
0 |
Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class); |
109 |
0 |
if (null != userPrincipal) |
110 |
|
{ |
111 |
0 |
log.debug("Got user principal: " + userPrincipal.getName()); |
112 |
0 |
UserManager userMgr = (UserManager) Jetspeed.getComponentManager().getComponent(UserManager.class); |
113 |
|
try |
114 |
|
{ |
115 |
0 |
if (!userMgr.getAnonymousUser().equals(userPrincipal.getName()) |
116 |
|
&& userMgr.userExists(userPrincipal.getName())) |
117 |
|
{ |
118 |
0 |
User user = userMgr.getUser(userPrincipal.getName()); |
119 |
|
|
120 |
0 |
Preferences prefs = user.getPreferences(); |
121 |
0 |
String localeString = prefs.get(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, null); |
122 |
0 |
if (localeString != null) |
123 |
|
{ |
124 |
0 |
locale = JetspeedLocale.convertStringToLocale(localeString); |
125 |
|
} |
126 |
|
} |
127 |
|
} |
128 |
0 |
catch (SecurityException e) |
129 |
|
{ |
130 |
0 |
log.warn("Unexpected SecurityException in UserInfoManager", e); |
131 |
0 |
} |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
0 |
if (locale == null) |
136 |
|
{ |
137 |
0 |
locale = (Locale) request.getSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE); |
138 |
|
} |
139 |
|
|
140 |
0 |
if ( locale == null && defaultLocale != class="keyword">null ) |
141 |
|
{ |
142 |
0 |
locale = defaultLocale; |
143 |
|
} |
144 |
|
|
145 |
0 |
if (locale == null) |
146 |
|
{ |
147 |
0 |
locale = request.getRequest().getLocale(); |
148 |
|
} |
149 |
|
|
150 |
0 |
if (locale == null) |
151 |
|
{ |
152 |
0 |
Enumeration preferedLocales = request.getRequest().getLocales(); |
153 |
0 |
while (preferedLocales.hasMoreElements() && locale == null) |
154 |
|
{ |
155 |
0 |
locale = (Locale) preferedLocales.nextElement(); |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
0 |
if (locale == null) |
160 |
|
{ |
161 |
0 |
locale = Locale.getDefault(); |
162 |
|
} |
163 |
|
|
164 |
0 |
request.setLocale(locale); |
165 |
0 |
request.getRequest().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale); |
166 |
0 |
request.getRequest().getSession().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale); |
167 |
0 |
CurrentLocale.set(locale); |
168 |
|
|
169 |
|
|
170 |
0 |
context.invokeNext(request); |
171 |
|
|
172 |
0 |
} |
173 |
|
|
174 |
|
public String toString() |
175 |
|
{ |
176 |
0 |
return "LocalizationValve"; |
177 |
|
} |
178 |
|
|
179 |
|
} |