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.util.Enumeration;
20  import java.util.Locale;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.jetspeed.PortalReservedParameters;
25  import org.apache.jetspeed.i18n.CurrentLocale;
26  import org.apache.jetspeed.pipeline.PipelineException;
27  import org.apache.jetspeed.pipeline.valve.AbstractValve;
28  import org.apache.jetspeed.pipeline.valve.LocalizationValve;
29  import org.apache.jetspeed.pipeline.valve.ValveContext;
30  import org.apache.jetspeed.request.RequestContext;
31  
32  /***
33   * LocalizationValveImpl
34   * 
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
36   * @version $Id: LocalizationValveImpl.java 378091 2006-02-15 21:12:28Z taylor $
37   */
38  public class SimplifiedLocalizationValveImpl extends AbstractValve implements LocalizationValve
39  {
40      private static final Log log = LogFactory.getLog(LocalizationValveImpl.class);
41      private Locale defaultLocale = null;
42      
43      public SimplifiedLocalizationValveImpl() {}
44      
45      public SimplifiedLocalizationValveImpl(String defaultLanguage)
46      {
47          String language = defaultLanguage != null ? defaultLanguage.trim() : "";
48          if (language.length()>0)
49          {
50              // Code taken from LocaleSelectorPorltet
51              String country = "";
52              String variant = "";
53              int countryIndex = language.indexOf('_');
54              if (countryIndex > -1)
55              {
56                  country = language.substring(countryIndex + 1).trim();
57                  language = language.substring(0, countryIndex).trim();
58                  int vDash = country.indexOf("_");
59                  if (vDash > 0)
60                  {
61                      String cTemp = country.substring(0, vDash);
62                      variant = country.substring(vDash + 1);
63                      country = cTemp;
64                  }
65              }
66  
67              defaultLocale = new Locale(language, country, variant);
68              if ( defaultLocale.getLanguage().length() == 0 )
69              {
70                  // not a valid language
71                  defaultLocale = null;
72                  log.warn("Invalid or unrecognized default language: "+language);
73              }
74              else
75              {
76                  log.info("Default language set: "+defaultLocale);
77              }
78                  
79          }
80      }
81      
82      /*
83       * (non-Javadoc)
84       * 
85       * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
86       *      org.apache.jetspeed.pipeline.valve.ValveContext)
87       */
88      public void invoke( RequestContext request, ValveContext context ) throws PipelineException
89      {
90          Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
91          
92          if ( locale == null && defaultLocale != null )
93          {
94              locale = defaultLocale;
95          }
96  
97          if (locale == null)
98          {
99              locale = request.getRequest().getLocale();
100         }
101         
102         if (locale == null)
103         {
104             Enumeration preferedLocales = request.getRequest().getLocales();
105             while (preferedLocales.hasMoreElements() && locale == null)
106             {
107                 locale = (Locale) preferedLocales.nextElement();
108             }
109         }
110 
111         if (locale == null)
112         {
113             locale = Locale.getDefault();
114         }
115 
116         request.setLocale(locale);
117         request.getRequest().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
118         request.getRequest().getSession().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
119         CurrentLocale.set(locale);
120        
121         // Pass control to the next Valve in the Pipeline
122         context.invokeNext(request);
123 
124     }
125 
126     public String toString()
127     {
128         return "LocalizationValve";
129     }
130 
131 }