View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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              // Code taken from LocaleSelectorPorltet
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                  // not a valid language
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       * (non-Javadoc)
95       * 
96       * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
97       *      org.apache.jetspeed.pipeline.valve.ValveContext)
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             // Get the prefered locale from user's preferences(persistent storage) if not anon user
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                             // TODO if preferred lang or locale is defined in PLT.D, it's better to use it
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         // Pass control to the next Valve in the Pipeline
170         context.invokeNext(request);
171 
172     }
173 
174     public String toString()
175     {
176         return "LocalizationValve";
177     }
178 
179 }