Coverage Report - org.apache.tapestry.services.impl.RequestLocaleManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestLocaleManagerImpl
96% 
100% 
2.818
 
 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  10
 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  10
     private Set _acceptedLocaleNamesSet = new HashSet();
 65  
 
 66  
     /**
 67  
      * Cache of Locales, keyed on locale name.
 68  
      */
 69  
 
 70  10
     private Map _localeCache = new HashMap();
 71  
 
 72  
     public void initializeService()
 73  
     {
 74  2
         String[] names = TapestryUtils.split(_acceptedLocales);
 75  
 
 76  2
         if (names.length == 0)
 77  0
             return;
 78  
 
 79  2
         _defaultLocale = getLocale(names[0]);
 80  
 
 81  2
         _acceptedLocaleNamesSet.addAll(Arrays.asList(names));
 82  
 
 83  2
     }
 84  
 
 85  
     public Locale extractLocaleForCurrentRequest()
 86  
     {
 87  5
         String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
 88  
 
 89  5
         String requestedLocale = (localeName != null) ? localeName : _request.getLocale().toString();
 90  
         
 91  5
         _requestLocale = filterRequestedLocale(requestedLocale);
 92  
 
 93  5
         _threadLocale.setLocale(_requestLocale);
 94  
 
 95  5
         return _requestLocale;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Converts the request locale name into a Locale instance; applies filters (based on
 100  
      * acceptedLocales) if enabled.
 101  
      */
 102  
 
 103  
     Locale filterRequestedLocale(String localeName)
 104  
     {
 105  15
         String requestLocaleName = localeName;
 106  15
         if (_acceptedLocaleNamesSet.isEmpty())
 107  6
             return getLocale(requestLocaleName);
 108  
 
 109  18
         while (requestLocaleName.length() > 0)
 110  
         {
 111  14
             if (_acceptedLocaleNamesSet.contains(requestLocaleName))
 112  5
                 return getLocale(requestLocaleName);
 113  
 
 114  9
             requestLocaleName = stripTerm(requestLocaleName);
 115  
         }
 116  
 
 117  
         // now try "best match"
 118  
 
 119  4
         for (Iterator it = _acceptedLocaleNamesSet.iterator(); it.hasNext();) {
 120  
 
 121  7
             String locale = (String) it.next();
 122  7
             if (locale.startsWith(localeName))
 123  2
                 return getLocale(locale);
 124  5
         }
 125  
 
 126  2
         return _defaultLocale;
 127  
     }
 128  
 
 129  
     private String stripTerm(String localeName)
 130  
     {
 131  9
         int scorex = localeName.lastIndexOf('_');
 132  
 
 133  9
         return scorex < 0 ? "" : localeName.substring(0, scorex);
 134  
     }
 135  
 
 136  
     public void persistLocale()
 137  
     {
 138  2
         Locale locale = _threadLocale.getLocale();
 139  
 
 140  2
         if (locale.equals(_requestLocale))
 141  1
             return;
 142  
 
 143  1
         _cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString());
 144  1
     }
 145  
 
 146  
     Locale getLocale(String name)
 147  
     {
 148  17
         Locale result = (Locale) _localeCache.get(name);
 149  
 
 150  17
         if (result == null)
 151  
         {
 152  11
             result = constructLocale(name);
 153  11
             _localeCache.put(name, result);
 154  
         }
 155  
 
 156  17
         return result;
 157  
     }
 158  
 
 159  
     private Locale constructLocale(String name)
 160  
     {
 161  11
         String[] terms = TapestryUtils.split(name, '_');
 162  
 
 163  11
         switch (terms.length)
 164  
         {
 165  
             case 1:
 166  7
                 return new Locale(terms[0], "");
 167  
 
 168  
             case 2:
 169  3
                 return new Locale(terms[0], terms[1]);
 170  
 
 171  
             case 3:
 172  
 
 173  1
                 return new Locale(terms[0], terms[1], terms[2]);
 174  
 
 175  
             default:
 176  
 
 177  0
                 throw new IllegalArgumentException();
 178  
         }
 179  
     }
 180  
 
 181  
     public void setCookieSource(CookieSource source)
 182  
     {
 183  6
         _cookieSource = source;
 184  6
     }
 185  
 
 186  
     public void setRequest(WebRequest request)
 187  
     {
 188  2
         _request = request;
 189  2
     }
 190  
 
 191  
     public void setThreadLocale(ThreadLocale threadLocale)
 192  
     {
 193  6
         _threadLocale = threadLocale;
 194  6
     }
 195  
 
 196  
     public void setAcceptedLocales(String acceptedLocales)
 197  
     {
 198  2
         _acceptedLocales = acceptedLocales;
 199  2
     }
 200  
 }