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.security.Principal;
020: import java.util.Enumeration;
021: import java.util.Locale;
022: import java.util.prefs.Preferences;
023:
024: import javax.security.auth.Subject;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.jetspeed.Jetspeed;
029: import org.apache.jetspeed.PortalReservedParameters;
030: import org.apache.jetspeed.i18n.CurrentLocale;
031: import org.apache.jetspeed.pipeline.PipelineException;
032: import org.apache.jetspeed.pipeline.valve.AbstractValve;
033: import org.apache.jetspeed.pipeline.valve.LocalizationValve;
034: import org.apache.jetspeed.pipeline.valve.ValveContext;
035: import org.apache.jetspeed.request.RequestContext;
036: import org.apache.jetspeed.security.SecurityException;
037: import org.apache.jetspeed.security.SecurityHelper;
038: import org.apache.jetspeed.security.User;
039: import org.apache.jetspeed.security.UserManager;
040: import org.apache.jetspeed.security.UserPrincipal;
041: import org.apache.jetspeed.util.JetspeedLocale;
042:
043: /**
044: * LocalizationValveImpl
045: *
046: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
047: * @version $Id: LocalizationValveImpl.java 516448 2007-03-09 16:25:47Z ate $
048: */
049: public class LocalizationValveImpl extends AbstractValve implements
050: LocalizationValve {
051: private static final Log log = LogFactory
052: .getLog(LocalizationValveImpl.class);
053: private Locale defaultLocale = null;
054:
055: public LocalizationValveImpl() {
056: }
057:
058: public LocalizationValveImpl(String defaultLanguage) {
059: String language = defaultLanguage != null ? defaultLanguage
060: .trim() : "";
061: if (language.length() > 0) {
062: // Code taken from LocaleSelectorPorltet
063: String country = "";
064: String variant = "";
065: int countryIndex = language.indexOf('_');
066: if (countryIndex > -1) {
067: country = language.substring(countryIndex + 1).trim();
068: language = language.substring(0, countryIndex).trim();
069: int vDash = country.indexOf("_");
070: if (vDash > 0) {
071: String cTemp = country.substring(0, vDash);
072: variant = country.substring(vDash + 1);
073: country = cTemp;
074: }
075: }
076:
077: defaultLocale = new Locale(language, country, variant);
078: if (defaultLocale.getLanguage().length() == 0) {
079: // not a valid language
080: defaultLocale = null;
081: log.warn("Invalid or unrecognized default language: "
082: + language);
083: } else {
084: log.info("Default language set: " + defaultLocale);
085: }
086:
087: }
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
094: * org.apache.jetspeed.pipeline.valve.ValveContext)
095: */
096: public void invoke(RequestContext request, ValveContext context)
097: throws PipelineException {
098: Locale locale = (Locale) request
099: .getRequest()
100: .getSession()
101: .getAttribute(
102: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
103: if (null == locale) {
104: // Get the prefered locale from user's preferences(persistent storage) if not anon user
105: Subject subject = request.getSubject();
106: if (null != subject) {
107: Principal userPrincipal = SecurityHelper.getPrincipal(
108: subject, UserPrincipal.class);
109: if (null != userPrincipal) {
110: log.debug("Got user principal: "
111: + userPrincipal.getName());
112: UserManager userMgr = (UserManager) Jetspeed
113: .getComponentManager().getComponent(
114: UserManager.class);
115: try {
116: if (!userMgr.getAnonymousUser().equals(
117: userPrincipal.getName())
118: && userMgr.userExists(userPrincipal
119: .getName())) {
120: User user = userMgr.getUser(userPrincipal
121: .getName());
122: // TODO if preferred lang or locale is defined in PLT.D, it's better to use it
123: Preferences prefs = user.getPreferences();
124: String localeString = prefs
125: .get(
126: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
127: null);
128: if (localeString != null) {
129: locale = JetspeedLocale
130: .convertStringToLocale(localeString);
131: }
132: }
133: } catch (SecurityException e) {
134: log
135: .warn(
136: "Unexpected SecurityException in UserInfoManager",
137: e);
138: }
139: }
140: }
141: }
142: if (locale == null) {
143: locale = (Locale) request
144: .getSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
145: }
146:
147: if (locale == null && defaultLocale != null) {
148: locale = defaultLocale;
149: }
150:
151: if (locale == null) {
152: locale = request.getRequest().getLocale();
153: }
154:
155: if (locale == null) {
156: Enumeration preferedLocales = request.getRequest()
157: .getLocales();
158: while (preferedLocales.hasMoreElements() && locale == null) {
159: locale = (Locale) preferedLocales.nextElement();
160: }
161: }
162:
163: if (locale == null) {
164: locale = Locale.getDefault();
165: }
166:
167: request.setLocale(locale);
168: request.getRequest().setAttribute(
169: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
170: locale);
171: request.getRequest().getSession().setAttribute(
172: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
173: locale);
174: CurrentLocale.set(locale);
175:
176: // Pass control to the next Valve in the Pipeline
177: context.invokeNext(request);
178:
179: }
180:
181: public String toString() {
182: return "LocalizationValve";
183: }
184:
185: }
|