Coverage report

  %line %branch
org.apache.jetspeed.localization.impl.LocalizationValveImpl
0% 
0% 

 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  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  
             // Code taken from LocaleSelectorPorltet
 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  
                 // not a valid language
 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  
      * (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  0
         Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
 102  0
         if (null == locale)
 103  
         {        
 104  
             // Get the prefered locale from user's preferences(persistent storage) if not anon user
 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  
                             // TODO if preferred lang or locale is defined in PLT.D, it's better to use it
 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  
         // Pass control to the next Valve in the Pipeline
 170  0
         context.invokeNext(request);
 171  
 
 172  0
     }
 173  
 
 174  
     public String toString()
 175  
     {
 176  0
         return "LocalizationValve";
 177  
     }
 178  
 
 179  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.