|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RequestLocaleManagerImpl.java | 100% | 94.1% | 100% | 96.3% |
|
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 java.util.Locale;
|
|
18 |
|
|
19 |
import javax.servlet.http.HttpServletRequest;
|
|
20 |
|
|
21 |
import org.apache.hivemind.util.Defense;
|
|
22 |
import org.apache.tapestry.ApplicationServlet;
|
|
23 |
import org.apache.tapestry.TapestryConstants;
|
|
24 |
import org.apache.tapestry.services.CookieSource;
|
|
25 |
import org.apache.tapestry.services.RequestLocaleManager;
|
|
26 |
import org.apache.tapestry.util.StringSplitter;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Identifies the Locale provided by the client (either in a Tapestry-specific cookie, or
|
|
30 |
* interpolated from the HTTP header. TODO: Add the ability to "filter down" Locales down to a
|
|
31 |
* predifined set (specified using some form of HiveMInd configuration).
|
|
32 |
*
|
|
33 |
* @author Howard Lewis Ship
|
|
34 |
* @since 3.1
|
|
35 |
*/
|
|
36 |
public class RequestLocaleManagerImpl implements RequestLocaleManager |
|
37 |
{ |
|
38 |
private HttpServletRequest _request;
|
|
39 |
|
|
40 |
private Locale _requestLocale;
|
|
41 |
|
|
42 |
private CookieSource _cookieSource;
|
|
43 |
|
|
44 | 190 |
public Locale extractLocaleForCurrentRequest()
|
45 |
{ |
|
46 | 190 |
String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME); |
47 |
|
|
48 | 190 |
_requestLocale = (localeName != null) ? getLocale(localeName) : _request.getLocale();
|
49 |
|
|
50 | 190 |
return _requestLocale;
|
51 |
} |
|
52 |
|
|
53 | 197 |
public void persistLocale(Locale locale) |
54 |
{ |
|
55 | 197 |
Defense.notNull(locale, "locale");
|
56 |
|
|
57 | 197 |
if (locale.equals(_requestLocale))
|
58 | 184 |
return;
|
59 |
|
|
60 | 13 |
_cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString()); |
61 |
} |
|
62 |
|
|
63 | 13 |
private Locale getLocale(String name)
|
64 |
{ |
|
65 |
// There used to be a cache of Locale (keyed on name), but since this service is
|
|
66 |
// threaded, there's no point (short of making it static, which is too ugly for words).
|
|
67 |
// Instead, we should have a LocaleCache service for that purpose. Have to balance
|
|
68 |
// cost of invoking that service vs. the cost of creating new Locale instances all the time.
|
|
69 |
|
|
70 | 13 |
return constructLocale(name);
|
71 |
} |
|
72 |
|
|
73 | 13 |
private Locale constructLocale(String name)
|
74 |
{ |
|
75 | 13 |
StringSplitter splitter = new StringSplitter('_');
|
76 | 13 |
String[] terms = splitter.splitToArray(name); |
77 |
|
|
78 | 13 |
switch (terms.length)
|
79 |
{ |
|
80 |
case 1:
|
|
81 | 11 |
return new Locale(terms[0], ""); |
82 |
|
|
83 |
case 2:
|
|
84 | 1 |
return new Locale(terms[0], terms[1]); |
85 |
|
|
86 |
case 3:
|
|
87 |
|
|
88 | 1 |
return new Locale(terms[0], terms[1], terms[2]); |
89 |
|
|
90 |
default:
|
|
91 |
|
|
92 | 0 |
throw new IllegalArgumentException(); |
93 |
} |
|
94 |
} |
|
95 |
|
|
96 | 191 |
public void setCookieSource(CookieSource source) |
97 |
{ |
|
98 | 191 |
_cookieSource = source; |
99 |
} |
|
100 |
|
|
101 | 187 |
public void setRequest(HttpServletRequest request) |
102 |
{ |
|
103 | 187 |
_request = request; |
104 |
} |
|
105 |
} |
|