01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.core.aspects.server;
23:
24: import org.jboss.portal.common.i18n.LocaleFormat;
25: import org.jboss.portal.common.invocation.InvocationException;
26: import org.jboss.portal.common.util.ConversionException;
27: import org.jboss.portal.identity.User;
28: import org.jboss.portal.server.ServerInterceptor;
29: import org.jboss.portal.server.ServerInvocation;
30: import org.jboss.portal.server.ServerRequest;
31:
32: import java.util.ArrayList;
33: import java.util.List;
34: import java.util.Locale;
35: import java.util.Map;
36:
37: /**
38: * If the user is authenticated and has a preferred locale then this one is chosen. Otherwhise the locale used is the
39: * one determined by the incoming HttpServletRequest provided by the servlet container.
40: * <p/>
41: * todo add cookie or session based locales
42: *
43: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
44: * @version $Revision: 8786 $
45: */
46: public class LocaleInterceptor extends ServerInterceptor {
47: protected void invoke(ServerInvocation invocation)
48: throws Exception, InvocationException {
49: // The locales
50: List locales = new ArrayList(4);
51:
52: //
53: Map profile = (Map) invocation.getAttribute(
54: ServerInvocation.PRINCIPAL_SCOPE,
55: UserInterceptor.PROFILE_KEY);
56:
57: //
58: if (profile != null) {
59: Locale locale = null;
60: Object lc = profile.get(User.INFO_USER_LOCALE);
61: if (lc instanceof Locale) {
62: locale = (Locale) lc;
63: } else if (lc != null) {
64: try {
65: locale = LocaleFormat.DEFAULT.getLocale(lc
66: .toString());
67: } catch (ConversionException e) {
68: //just to hide failure
69: }
70: }
71:
72: //
73: if (locale != null) {
74: locales.add(locale);
75: }
76: }
77:
78: // Get the locale from the user agent.
79: ServerRequest req = invocation.getRequest();
80: locales.add(invocation.getServerContext().getClientRequest()
81: .getLocale());
82:
83: try {
84: // Set the locale for the request
85: Locale[] tmp = (Locale[]) locales
86: .toArray(new Locale[locales.size()]);
87: req.setLocales(tmp);
88:
89: // Invokoe
90: invocation.invokeNext();
91: } finally {
92: // Clear the locale
93: req.setLocales(null);
94: }
95: }
96: }
|