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 * LocalizationValveImpl
45 *
46 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
47 * @version $Id: LocalizationValveImpl.java 516448 2007-03-09 16:25:47Z ate $
48 */
49 public class LocalizationValveImpl extends AbstractValve implements LocalizationValve
50 {
51 private static final Log log = LogFactory.getLog(LocalizationValveImpl.class);
52 private Locale defaultLocale = null;
53
54 public LocalizationValveImpl() {}
55
56 public LocalizationValveImpl(String defaultLanguage)
57 {
58 String language = defaultLanguage != null ? defaultLanguage.trim() : "";
59 if (language.length()>0)
60 {
61
62 String country = "";
63 String variant = "";
64 int countryIndex = language.indexOf('_');
65 if (countryIndex > -1)
66 {
67 country = language.substring(countryIndex + 1).trim();
68 language = language.substring(0, countryIndex).trim();
69 int vDash = country.indexOf("_");
70 if (vDash > 0)
71 {
72 String cTemp = country.substring(0, vDash);
73 variant = country.substring(vDash + 1);
74 country = cTemp;
75 }
76 }
77
78 defaultLocale = new Locale(language, country, variant);
79 if ( defaultLocale.getLanguage().length() == 0 )
80 {
81
82 defaultLocale = null;
83 log.warn("Invalid or unrecognized default language: "+language);
84 }
85 else
86 {
87 log.info("Default language set: "+defaultLocale);
88 }
89
90 }
91 }
92
93
94
95
96
97
98
99 public void invoke( RequestContext request, ValveContext context ) throws PipelineException
100 {
101 Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
102 if (null == locale)
103 {
104
105 Subject subject = request.getSubject();
106 if (null != subject)
107 {
108 Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class);
109 if (null != userPrincipal)
110 {
111 log.debug("Got user principal: " + userPrincipal.getName());
112 UserManager userMgr = (UserManager) Jetspeed.getComponentManager().getComponent(UserManager.class);
113 try
114 {
115 if (!userMgr.getAnonymousUser().equals(userPrincipal.getName())
116 && userMgr.userExists(userPrincipal.getName()))
117 {
118 User user = userMgr.getUser(userPrincipal.getName());
119
120 Preferences prefs = user.getPreferences();
121 String localeString = prefs.get(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, null);
122 if (localeString != null)
123 {
124 locale = JetspeedLocale.convertStringToLocale(localeString);
125 }
126 }
127 }
128 catch (SecurityException e)
129 {
130 log.warn("Unexpected SecurityException in UserInfoManager", e);
131 }
132 }
133 }
134 }
135 if (locale == null)
136 {
137 locale = (Locale) request.getSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
138 }
139
140 if ( locale == null && defaultLocale != null )
141 {
142 locale = defaultLocale;
143 }
144
145 if (locale == null)
146 {
147 locale = request.getRequest().getLocale();
148 }
149
150 if (locale == null)
151 {
152 Enumeration preferedLocales = request.getRequest().getLocales();
153 while (preferedLocales.hasMoreElements() && locale == null)
154 {
155 locale = (Locale) preferedLocales.nextElement();
156 }
157 }
158
159 if (locale == null)
160 {
161 locale = Locale.getDefault();
162 }
163
164 request.setLocale(locale);
165 request.getRequest().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
166 request.getRequest().getSession().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
167 CurrentLocale.set(locale);
168
169
170 context.invokeNext(request);
171
172 }
173
174 public String toString()
175 {
176 return "LocalizationValve";
177 }
178
179 }