ClientInfo.java :  » Portal » stringbeans-3.5 » com » nabhinc » portal » core » Java Open Source

Java Open Source » Portal » stringbeans 3.5 
stringbeans 3.5 » com » nabhinc » portal » core » ClientInfo.java
/* 
 * (C) Copyright 2006 Nabh Information Systems, Inc. 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.nabhinc.portal.core;

import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nabhinc.core.MimeTypes;
import com.nabhinc.portal.model.PortalConfiguration;
import com.nabhinc.portal.model.UserPreferences;

/**
 * Maintains information about current client such as user name,
 * preferred locale, requested mime type, etc.
 *
 * @author Padmanabh Dabke
 * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
 */
public class ClientInfo {
  public static final String CLIENT_INFO_ATTRIBUTE = "stringbeans.client_info";
    
  public transient boolean isRTL = false;
  
  public transient String userName = null;
  
  public transient Locale preferredLocale = null;
  
  public transient Locale[] currentLocales = null;
  
  public transient String[] currentLocaleLabels = null;
  
  public transient Properties currentLocaleProperties = null;

  public transient int logoutType = LoginInterceptor.LOGOUT_SESSION_TIMED_OUT;

  public transient HttpSession session = null;

  public transient String remoteHost = null;

  public transient String remoteAddress = null;

  public transient boolean isMobileClient = false;

  public transient String mimeType = "text/html";

  public transient String sessionId = null;
  
  public static ClientInfo createClientInfo(HttpServletRequest req,
      UserPreferences userPrefs) {
    HttpSession session = req.getSession();
    ClientInfo clInfo = new ClientInfo();
    
    clInfo.session = session;
    clInfo.sessionId = session.getId();
    clInfo.remoteAddress = req.getRemoteAddr();
    clInfo.remoteHost = req.getRemoteHost();
    clInfo.userName = req.getRemoteUser();

    clInfo.setClientType(req);
    clInfo.setLocaleInfo(req, userPrefs);
    return clInfo;
  }
  
    private void setClientType(HttpServletRequest req) {
        String acceptHeader = req.getHeader("accept");
      this.mimeType = MimeTypes.HTML;
      this.isMobileClient = false;

        if (acceptHeader != null) {
            if (acceptHeader.indexOf(MimeTypes.XHTML_MP) != -1) {
                this.isMobileClient = true;
                this.mimeType = MimeTypes.XHTML_MP;
            } else if (acceptHeader.indexOf(MimeTypes.WML) != -1) {
              this.isMobileClient = true;
              this.mimeType = MimeTypes.WML;
            }
        }
    }
    
    @SuppressWarnings("unchecked")
  private void setLocaleInfo(HttpServletRequest req, UserPreferences userPrefs) {
          
      Enumeration localeList = req.getLocales();
      Vector localeVec = new Vector(5);
      if (localeList != null) {
          while (localeList.hasMoreElements()) {
            localeVec.addElement(localeList.nextElement());
          }
      }

        if (userPrefs != null && userPrefs.getPreferredLocale() != null) {
            if (localeVec.size() == 0) {
              this.currentLocales = new Locale[] { userPrefs.getPreferredLocale() };
            } else {
              localeVec.remove(userPrefs.getPreferredLocale());
              localeVec.add(0, userPrefs.getPreferredLocale());
              Locale[] localeArray = new Locale[localeVec.size()];
              localeVec.copyInto(localeArray);
              this.currentLocales = localeArray;
            }
        } else {
            if (localeVec.size() == 0) {
              this.currentLocales = new Locale[] { PortalConfiguration.getInstance().getDefaultLocale() };
            } else {
              Locale[] localeArray = new Locale[localeVec.size()];
              localeVec.copyInto(localeArray);
              this.currentLocales = localeArray;
            }

        }
            
        // Determine the best locale label array
        for (int i=0; i< this.currentLocales.length; i++) {
          String[] labels = (String[]) LocaleUtil.getLocaleMap().get(this.currentLocales[i].toString());
          if (labels != null) {
            this.currentLocaleLabels = labels;
            this.currentLocaleProperties = LocaleUtil.getLocaleProperties(this.currentLocales[i]);
              break;
          }
        }
        if (this.currentLocaleLabels == null) {
          this.currentLocaleLabels = (String[]) LocaleUtil.getLocaleMap().get("default");
          this.currentLocaleProperties = LocaleUtil.getDefaultLocaleProperties();
        }
        
        // Check if the current locale is right to left
        if ("rtl".equals(this.currentLocaleLabels[LocaleUtil.PAGE_DIRECTION])) this.isRTL = true;

    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.