001: /*
002: * (C) Copyright 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.core;
020:
021: import java.util.Enumeration;
022: import java.util.Locale;
023: import java.util.Properties;
024: import java.util.Vector;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028:
029: import com.nabhinc.core.MimeTypes;
030: import com.nabhinc.portal.model.PortalConfiguration;
031: import com.nabhinc.portal.model.UserPreferences;
032:
033: /**
034: * Maintains information about current client such as user name,
035: * preferred locale, requested mime type, etc.
036: *
037: * @author Padmanabh Dabke
038: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
039: */
040: public class ClientInfo {
041: public static final String CLIENT_INFO_ATTRIBUTE = "stringbeans.client_info";
042:
043: public transient boolean isRTL = false;
044:
045: public transient String userName = null;
046:
047: public transient Locale preferredLocale = null;
048:
049: public transient Locale[] currentLocales = null;
050:
051: public transient String[] currentLocaleLabels = null;
052:
053: public transient Properties currentLocaleProperties = null;
054:
055: public transient int logoutType = LoginInterceptor.LOGOUT_SESSION_TIMED_OUT;
056:
057: public transient HttpSession session = null;
058:
059: public transient String remoteHost = null;
060:
061: public transient String remoteAddress = null;
062:
063: public transient boolean isMobileClient = false;
064:
065: public transient String mimeType = "text/html";
066:
067: public transient String sessionId = null;
068:
069: public static ClientInfo createClientInfo(HttpServletRequest req,
070: UserPreferences userPrefs) {
071: HttpSession session = req.getSession();
072: ClientInfo clInfo = new ClientInfo();
073:
074: clInfo.session = session;
075: clInfo.sessionId = session.getId();
076: clInfo.remoteAddress = req.getRemoteAddr();
077: clInfo.remoteHost = req.getRemoteHost();
078: clInfo.userName = req.getRemoteUser();
079:
080: clInfo.setClientType(req);
081: clInfo.setLocaleInfo(req, userPrefs);
082: return clInfo;
083: }
084:
085: private void setClientType(HttpServletRequest req) {
086: String acceptHeader = req.getHeader("accept");
087: this .mimeType = MimeTypes.HTML;
088: this .isMobileClient = false;
089:
090: if (acceptHeader != null) {
091: if (acceptHeader.indexOf(MimeTypes.XHTML_MP) != -1) {
092: this .isMobileClient = true;
093: this .mimeType = MimeTypes.XHTML_MP;
094: } else if (acceptHeader.indexOf(MimeTypes.WML) != -1) {
095: this .isMobileClient = true;
096: this .mimeType = MimeTypes.WML;
097: }
098: }
099: }
100:
101: @SuppressWarnings("unchecked")
102: private void setLocaleInfo(HttpServletRequest req,
103: UserPreferences userPrefs) {
104:
105: Enumeration localeList = req.getLocales();
106: Vector localeVec = new Vector(5);
107: if (localeList != null) {
108: while (localeList.hasMoreElements()) {
109: localeVec.addElement(localeList.nextElement());
110: }
111: }
112:
113: if (userPrefs != null && userPrefs.getPreferredLocale() != null) {
114: if (localeVec.size() == 0) {
115: this .currentLocales = new Locale[] { userPrefs
116: .getPreferredLocale() };
117: } else {
118: localeVec.remove(userPrefs.getPreferredLocale());
119: localeVec.add(0, userPrefs.getPreferredLocale());
120: Locale[] localeArray = new Locale[localeVec.size()];
121: localeVec.copyInto(localeArray);
122: this .currentLocales = localeArray;
123: }
124: } else {
125: if (localeVec.size() == 0) {
126: this .currentLocales = new Locale[] { PortalConfiguration
127: .getInstance().getDefaultLocale() };
128: } else {
129: Locale[] localeArray = new Locale[localeVec.size()];
130: localeVec.copyInto(localeArray);
131: this .currentLocales = localeArray;
132: }
133:
134: }
135:
136: // Determine the best locale label array
137: for (int i = 0; i < this .currentLocales.length; i++) {
138: String[] labels = (String[]) LocaleUtil.getLocaleMap().get(
139: this .currentLocales[i].toString());
140: if (labels != null) {
141: this .currentLocaleLabels = labels;
142: this .currentLocaleProperties = LocaleUtil
143: .getLocaleProperties(this .currentLocales[i]);
144: break;
145: }
146: }
147: if (this .currentLocaleLabels == null) {
148: this .currentLocaleLabels = (String[]) LocaleUtil
149: .getLocaleMap().get("default");
150: this .currentLocaleProperties = LocaleUtil
151: .getDefaultLocaleProperties();
152: }
153:
154: // Check if the current locale is right to left
155: if ("rtl"
156: .equals(this .currentLocaleLabels[LocaleUtil.PAGE_DIRECTION]))
157: this .isRTL = true;
158:
159: }
160:
161: }
|