001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.localization.impl;
018:
019: import java.util.Enumeration;
020: import java.util.Locale;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.jetspeed.PortalReservedParameters;
025: import org.apache.jetspeed.i18n.CurrentLocale;
026: import org.apache.jetspeed.pipeline.PipelineException;
027: import org.apache.jetspeed.pipeline.valve.AbstractValve;
028: import org.apache.jetspeed.pipeline.valve.LocalizationValve;
029: import org.apache.jetspeed.pipeline.valve.ValveContext;
030: import org.apache.jetspeed.request.RequestContext;
031:
032: /**
033: * LocalizationValveImpl
034: *
035: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
036: * @version $Id: LocalizationValveImpl.java 378091 2006-02-15 21:12:28Z taylor $
037: */
038: public class SimplifiedLocalizationValveImpl extends AbstractValve
039: implements LocalizationValve {
040: private static final Log log = LogFactory
041: .getLog(LocalizationValveImpl.class);
042: private Locale defaultLocale = null;
043:
044: public SimplifiedLocalizationValveImpl() {
045: }
046:
047: public SimplifiedLocalizationValveImpl(String defaultLanguage) {
048: String language = defaultLanguage != null ? defaultLanguage
049: .trim() : "";
050: if (language.length() > 0) {
051: // Code taken from LocaleSelectorPorltet
052: String country = "";
053: String variant = "";
054: int countryIndex = language.indexOf('_');
055: if (countryIndex > -1) {
056: country = language.substring(countryIndex + 1).trim();
057: language = language.substring(0, countryIndex).trim();
058: int vDash = country.indexOf("_");
059: if (vDash > 0) {
060: String cTemp = country.substring(0, vDash);
061: variant = country.substring(vDash + 1);
062: country = cTemp;
063: }
064: }
065:
066: defaultLocale = new Locale(language, country, variant);
067: if (defaultLocale.getLanguage().length() == 0) {
068: // not a valid language
069: defaultLocale = null;
070: log.warn("Invalid or unrecognized default language: "
071: + language);
072: } else {
073: log.info("Default language set: " + defaultLocale);
074: }
075:
076: }
077: }
078:
079: /*
080: * (non-Javadoc)
081: *
082: * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
083: * org.apache.jetspeed.pipeline.valve.ValveContext)
084: */
085: public void invoke(RequestContext request, ValveContext context)
086: throws PipelineException {
087: Locale locale = (Locale) request
088: .getRequest()
089: .getSession()
090: .getAttribute(
091: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
092:
093: if (locale == null && defaultLocale != null) {
094: locale = defaultLocale;
095: }
096:
097: if (locale == null) {
098: locale = request.getRequest().getLocale();
099: }
100:
101: if (locale == null) {
102: Enumeration preferedLocales = request.getRequest()
103: .getLocales();
104: while (preferedLocales.hasMoreElements() && locale == null) {
105: locale = (Locale) preferedLocales.nextElement();
106: }
107: }
108:
109: if (locale == null) {
110: locale = Locale.getDefault();
111: }
112:
113: request.setLocale(locale);
114: request.getRequest().setAttribute(
115: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
116: locale);
117: request.getRequest().getSession().setAttribute(
118: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
119: locale);
120: CurrentLocale.set(locale);
121:
122: // Pass control to the next Valve in the Pipeline
123: context.invokeNext(request);
124:
125: }
126:
127: public String toString() {
128: return "LocalizationValve";
129: }
130:
131: }
|