/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.services.impl;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.tapestry.ApplicationServlet;
import org.apache.tapestry.Defense;
import org.apache.tapestry.services.CookieSource;
import org.apache.tapestry.services.RequestLocaleManager;
import org.apache.tapestry.util.StringSplitter;
/**
* Identifies the Locale provided by the client (either in a Tapestry-specific cookie, or
* interpolated from the HTTP header. TODO: Add the ability to "filter down" Locales down to a
* predifined set (specified using some form of HiveMInd configuration).
*
* @author Howard Lewis Ship
* @since 3.1
*/
public class RequestLocaleManagerImpl implements RequestLocaleManager
{public static com.cortexeb.tools.clover.d __CLOVER_498_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1097439627296L);
private HttpServletRequest _request;
private Locale _requestLocale;
private CookieSource _cookieSource;
public Locale extractLocaleForCurrentRequest()
{try { __CLOVER_498_0.M[3380]++;
__CLOVER_498_0.S[14203]++;String localeName = _cookieSource.readCookieValue(ApplicationServlet.LOCALE_COOKIE_NAME);
__CLOVER_498_0.S[14204]++;_requestLocale = ((((localeName != null) ) && (++__CLOVER_498_0.CT[2365] != 0)) || (++__CLOVER_498_0.CF[2365] == 0))? getLocale(localeName) : _request.getLocale();
__CLOVER_498_0.S[14205]++;return _requestLocale;
} finally { }}
public void persistLocale(Locale locale)
{try { __CLOVER_498_0.M[3381]++;
__CLOVER_498_0.S[14206]++;Defense.notNull(locale, "locale");
__CLOVER_498_0.S[14207]++;if ((((locale.equals(_requestLocale)) && (++__CLOVER_498_0.CT[2366] != 0)) || (++__CLOVER_498_0.CF[2366] == 0))){
__CLOVER_498_0.S[14208]++;return;}
__CLOVER_498_0.S[14209]++;_cookieSource.writeCookieValue(ApplicationServlet.LOCALE_COOKIE_NAME, locale.toString());
} finally { }}
private Locale getLocale(String name)
{try { __CLOVER_498_0.M[3382]++;
// There used to be a cache of Locale (keyed on name), but since this service is
// threaded, there's no point (short of making it static, which is too ugly for words).
// Instead, we should have a LocaleCache service for that purpose. Have to balance
// cost of invoking that service vs. the cost of creating new Locale instances all the time.
__CLOVER_498_0.S[14210]++;return constructLocale(name);
} finally { }}
private Locale constructLocale(String name)
{try { __CLOVER_498_0.M[3383]++;
__CLOVER_498_0.S[14211]++;StringSplitter splitter = new StringSplitter('_');
__CLOVER_498_0.S[14212]++;String[] terms = splitter.splitToArray(name);
__CLOVER_498_0.S[14213]++;switch (terms.length)
{
case 1:
__CLOVER_498_0.S[14214]++;return new Locale(terms[0], "");
case 2:
__CLOVER_498_0.S[14215]++;return new Locale(terms[0], terms[1]);
case 3:
__CLOVER_498_0.S[14216]++;return new Locale(terms[0], terms[1], terms[2]);
default:
__CLOVER_498_0.S[14217]++;throw new IllegalArgumentException();
}
} finally { }}
public void setCookieSource(CookieSource source)
{try { __CLOVER_498_0.M[3384]++;
__CLOVER_498_0.S[14218]++;_cookieSource = source;
} finally { }}
public void setRequest(HttpServletRequest request)
{try { __CLOVER_498_0.M[3385]++;
__CLOVER_498_0.S[14219]++;_request = request;
} finally { }}
}