Coverage Report - org.apache.tapestry.services.impl.RequestLocaleManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestLocaleManagerImpl
0%
0/54
0%
0/28
3.182
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.services.impl;
 16  
 
 17  
 import org.apache.hivemind.service.ThreadLocale;
 18  
 import org.apache.tapestry.TapestryConstants;
 19  
 import org.apache.tapestry.TapestryUtils;
 20  
 import org.apache.tapestry.services.CookieSource;
 21  
 import org.apache.tapestry.services.RequestLocaleManager;
 22  
 import org.apache.tapestry.web.WebRequest;
 23  
 
 24  
 import java.util.*;
 25  
 
 26  
 /**
 27  
  * Service tapestry.request.RequestLocaleManager. Identifies the Locale provided by the client
 28  
  * (either in a Tapestry-specific cookie, or interpolated from the HTTP header.
 29  
  *
 30  
  * @author Howard Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  0
 public class RequestLocaleManagerImpl implements RequestLocaleManager
 34  
 {
 35  
     private WebRequest _request;
 36  
 
 37  
     /**
 38  
      * Extracted at start of request, and used at end of request to see if locale has changed.
 39  
      * Because of this thread-specific state, the service must use the threaded service lifecycle
 40  
      * model.
 41  
      */
 42  
 
 43  
     private Locale _requestLocale;
 44  
 
 45  
     private CookieSource _cookieSource;
 46  
 
 47  
     private ThreadLocale _threadLocale;
 48  
 
 49  
     /**
 50  
      * Set from symbol org.apache.tapestry.accepted-locales, a comma-seperated list of locale names.
 51  
      * The first name is the default for requests that can't be matched against the other locale
 52  
      * names. May also be blank, in which case, whatever locale was provided in the request is
 53  
      * accepted (which is Tapestry 3.0 behavior).
 54  
      */
 55  
 
 56  
     private String _acceptedLocales;
 57  
 
 58  
     private Locale _defaultLocale;
 59  
 
 60  
     /**
 61  
      * Set of locale names. Incoming requests will be matched to one of these locales.
 62  
      */
 63  
 
 64  0
     private Set _acceptedLocaleNamesSet = new HashSet();
 65  
 
 66  
     /**
 67  
      * Cache of Locales, keyed on locale name.
 68  
      */
 69  
 
 70  0
     private Map _localeCache = new HashMap();
 71  
 
 72  
     /**
 73  
      * Reference to last persisted locale, if any.  Used to prevent
 74  
      * duplicate cookie writes of the same locale.
 75  
      */
 76  
     private Locale _lastPersisted;
 77  
 
 78  
     public void initializeService()
 79  
     {
 80  0
         String[] names = TapestryUtils.split(_acceptedLocales);
 81  
 
 82  0
         if (names.length == 0)
 83  0
             return;
 84  
 
 85  0
         _defaultLocale = getLocale(names[0]);
 86  
 
 87  0
         _acceptedLocaleNamesSet.addAll(Arrays.asList(names));
 88  0
     }
 89  
 
 90  
     public Locale extractLocaleForCurrentRequest()
 91  
     {
 92  0
         String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
 93  
 
 94  0
         String requestedLocale = (localeName != null) ? localeName : _request.getLocale().toString();
 95  
 
 96  0
         _requestLocale = filterRequestedLocale(requestedLocale);
 97  
 
 98  0
         _threadLocale.setLocale(_requestLocale);
 99  
 
 100  0
         return _requestLocale;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Converts the request locale name into a Locale instance; applies filters (based on
 105  
      * acceptedLocales) if enabled.
 106  
      */
 107  
 
 108  
     Locale filterRequestedLocale(String localeName)
 109  
     {
 110  0
         String requestLocaleName = localeName;
 111  0
         if (_acceptedLocaleNamesSet.isEmpty())
 112  0
             return getLocale(requestLocaleName);
 113  
 
 114  0
         while (requestLocaleName.length() > 0)
 115  
         {
 116  0
             if (_acceptedLocaleNamesSet.contains(requestLocaleName))
 117  0
                 return getLocale(requestLocaleName);
 118  
 
 119  0
             requestLocaleName = stripTerm(requestLocaleName);
 120  
         }
 121  
 
 122  
         // now try "best match"
 123  
 
 124  0
         for (Iterator it = _acceptedLocaleNamesSet.iterator(); it.hasNext();)
 125  
         {
 126  0
             String locale = (String) it.next();
 127  
 
 128  0
             if (locale.startsWith(localeName))
 129  0
                 return getLocale(locale);
 130  0
         }
 131  
 
 132  0
         return _defaultLocale;
 133  
     }
 134  
 
 135  
     private String stripTerm(String localeName)
 136  
     {
 137  0
         int scorex = localeName.lastIndexOf('_');
 138  
 
 139  0
         return scorex < 0 ? "" : localeName.substring(0, scorex);
 140  
     }
 141  
 
 142  
     public void persistLocale()
 143  
     {
 144  0
         Locale locale = _threadLocale.getLocale();
 145  
 
 146  0
         if (locale.equals(_requestLocale)
 147  
             || _lastPersisted != null && locale.equals(_lastPersisted))
 148  0
             return;
 149  
 
 150  0
         _cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString());
 151  0
         _lastPersisted = locale;
 152  0
     }
 153  
 
 154  
     Locale getLocale(String name)
 155  
     {
 156  0
         Locale result = (Locale) _localeCache.get(name);
 157  
 
 158  0
         if (result == null)
 159  
         {
 160  0
             result = constructLocale(name);
 161  0
             _localeCache.put(name, result);
 162  
         }
 163  
 
 164  0
         return result;
 165  
     }
 166  
 
 167  
     private Locale constructLocale(String name)
 168  
     {
 169  0
         String[] terms = TapestryUtils.split(name, '_');
 170  
 
 171  0
         switch (terms.length)
 172  
         {
 173  
             case 1:
 174  0
                 return new Locale(terms[0], "");
 175  
 
 176  
             case 2:
 177  0
                 return new Locale(terms[0], terms[1]);
 178  
 
 179  
             case 3:
 180  
 
 181  0
                 return new Locale(terms[0], terms[1], terms[2]);
 182  
 
 183  
             default:
 184  
 
 185  0
                 throw new IllegalArgumentException();
 186  
         }
 187  
     }
 188  
 
 189  
     public void setCookieSource(CookieSource source)
 190  
     {
 191  0
         _cookieSource = source;
 192  0
     }
 193  
 
 194  
     public void setRequest(WebRequest request)
 195  
     {
 196  0
         _request = request;
 197  0
     }
 198  
 
 199  
     public void setThreadLocale(ThreadLocale threadLocale)
 200  
     {
 201  0
         _threadLocale = threadLocale;
 202  0
     }
 203  
 
 204  
     public void setAcceptedLocales(String acceptedLocales)
 205  
     {
 206  0
         _acceptedLocales = acceptedLocales;
 207  0
     }
 208  
 }