UserTerminal.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.common.context;
00030 
00031 import java.util.StringTokenizer;
00032 
00033 import javax.servlet.http.HttpServletRequest;
00034 
00049 public class UserTerminal  {
00050 
00051   public static final int SYNCHRO_TYPE_INDIRECT = 0;
00052   public static final int SYNCHRO_TYPE_DIRECT = 1;
00053 
00054   private boolean plugIns;
00055   private String clientOS;
00056   private String clientOSVersion;
00057   private String clientPlugInsVersion;
00058   private int synchroType;
00059   private String language;
00060   private String useragent;
00061   private String clientPlugInName;
00062   
00063   public static String getUserAgentFromRequest(HttpServletRequest request)  {
00064     String ua = (String) request.getHeader("User-Agent");
00065     if (ua == null) {
00066       ua = (String) request.getHeader("user-agent");
00067     }
00068     return ua;
00069   }
00070 
00071 
00072   public UserTerminal(String browserUserAgent) throws Exception {
00073     if (browserUserAgent == null) browserUserAgent="";
00074         // user agent
00075     //OpenMSPPlug-<OpenMobileISMajorVersion>.<OpenMobileISMinorVersion>-<OS>[<OSVersion>]-<user language>-<synchro type if present>
00076     this.useragent = browserUserAgent;
00077     
00078                 if (browserUserAgent.indexOf("OpenMSPPlug")!= -1) {
00079                         plugIns = true;         
00080       clientPlugInName = "OpenMSPPlug";
00081     } 
00082     if (plugIns)  {
00083 
00084       StringTokenizer strToken = new StringTokenizer(browserUserAgent, "-");
00085 
00086       String token = null;
00087       if (strToken.hasMoreTokens())  { //OpenMSPPlug
00088          token = strToken.nextToken();
00089       }
00090       if (strToken.hasMoreTokens())  { //OpenMobile IS Version
00091         token = strToken.nextToken();
00092         this.clientPlugInsVersion=token;
00093        }
00094        if (strToken.hasMoreTokens())  { //OS and OSVersion
00095           token = strToken.nextToken();
00096           int index  = token.indexOf('[');
00097           this.clientOS = token.substring(0, index);
00098           this.clientOSVersion = token.substring(index+1, token.length()-1);
00099        }
00100        if (strToken.hasMoreTokens())  { //language
00101          token = strToken.nextToken();
00102          this.language=token;
00103         }
00104       // manage synchro type
00105       if (strToken.hasMoreTokens())  {
00106         token = strToken.nextToken();
00107         if (token.equals("PC")) {
00108           synchroType = UserTerminal.SYNCHRO_TYPE_INDIRECT;
00109         } else  {
00110           synchroType = UserTerminal.SYNCHRO_TYPE_DIRECT;
00111         }
00112       }
00113                 }
00114   }
00115 
00119   public String getClientPlateformType() {
00120     return this.clientOS;
00121   }
00122 
00126   public int getSynchroType() {
00127     return this.synchroType;
00128   }
00129 
00133   public boolean isOPenMISClient() {
00134     return plugIns;
00135   }
00136 
00137   public String getClientLanguage() {
00138     return this.language;
00139   }
00140 
00144   public String getClientPlugInsVersion() {
00145     return clientPlugInsVersion;
00146   }
00147 
00151   public String getUserAgent() {
00152     return this.useragent;
00153   }
00154 
00155    public String getClientOS() {
00156     return this.clientOS;
00157   }
00158 
00159   public boolean equals(Object o) {
00160     if (o instanceof UserTerminal)  {
00161       return (((UserTerminal)o).getUserAgent().equals(useragent));
00162     }
00163     return false;
00164   }
00165 
00166   public int hashCode() {
00167     return useragent.hashCode();
00168   }
00169 
00170 
00171   public String getClientOSVersion() {
00172     return clientOSVersion;
00173   }
00174 
00175 
00176   public String getClientPlugInName() {
00177     return clientPlugInName;
00178   }
00179 
00180 
00181 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4