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     // user agent
00074     //OpenMSPPlug-<OpenMobileISMajorVersion>.<OpenMobileISMinorVersion>-<OS>[<OSVersion>]-<user language>-<synchro type if present>
00075     this.useragent = browserUserAgent;
00076     
00077                 if (browserUserAgent.indexOf("OpenMSPPlug")!= -1) {
00078                         plugIns = true;         
00079       clientPlugInName = "OpenMSPPlug";
00080     } 
00081     if (plugIns)  {
00082 
00083       StringTokenizer strToken = new StringTokenizer(browserUserAgent, "-");
00084 
00085       String token = null;
00086       if (strToken.hasMoreTokens())  { //OpenMSPPlug
00087          token = strToken.nextToken();
00088       }
00089       if (strToken.hasMoreTokens())  { //OpenMobile IS Version
00090         token = strToken.nextToken();
00091         this.clientPlugInsVersion=token;
00092        }
00093        if (strToken.hasMoreTokens())  { //OS and OSVersion
00094           token = strToken.nextToken();
00095           int index  = token.indexOf('[');
00096           this.clientOS = token.substring(0, index);
00097           this.clientOSVersion = token.substring(index+1, token.length()-1);
00098        }
00099        if (strToken.hasMoreTokens())  { //language
00100          token = strToken.nextToken();
00101          this.language=token;
00102         }
00103       // manage synchro type
00104       if (strToken.hasMoreTokens())  {
00105         token = strToken.nextToken();
00106         if (token.equals("PC")) {
00107           synchroType = UserTerminal.SYNCHRO_TYPE_INDIRECT;
00108         } else  {
00109           synchroType = UserTerminal.SYNCHRO_TYPE_DIRECT;
00110         }
00111       }
00112                 }
00113   }
00114 
00118   public String getClientPlateformType() {
00119     return this.clientOS;
00120   }
00121 
00125   public int getSynchroType() {
00126     return this.synchroType;
00127   }
00128 
00132   public boolean isOPenMISClient() {
00133     return plugIns;
00134   }
00135 
00136   public String getClientLanguage() {
00137     return this.language;
00138   }
00139 
00143   public String getClientPlugInsVersion() {
00144     return clientPlugInsVersion;
00145   }
00146 
00150   public String getUserAgent() {
00151     return this.useragent;
00152   }
00153 
00154    public String getClientOS() {
00155     return this.clientOS;
00156   }
00157 
00158   public boolean equals(Object o) {
00159     if (o instanceof UserTerminal)  {
00160       return (((UserTerminal)o).getUserAgent().equals(useragent));
00161     }
00162     return false;
00163   }
00164 
00165   public int hashCode() {
00166     return useragent.hashCode();
00167   }
00168 
00169 
00170   public String getClientOSVersion() {
00171     return clientOSVersion;
00172   }
00173 
00174 
00175   public String getClientPlugInName() {
00176     return clientPlugInName;
00177   }
00178 
00179 
00180 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1