KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > BrowserBean


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26 import java.io.Serializable JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 public final class BrowserBean implements Serializable JavaDoc
31 {
32     private HttpServletRequest JavaDoc request = null;
33     private String JavaDoc useragent = null;
34     private String JavaDoc languages = null;
35     private boolean netEnabled = false;
36
37     private boolean ie = false;
38     private boolean ns7 = false;
39     private boolean ns6 = false;
40     private boolean ns4 = false;
41     private boolean opera = false;
42     private boolean mozilla = false;
43     private boolean safari = false;
44     private boolean firefox = false;
45     
46     private String JavaDoc os;
47     private boolean isWindows = false;
48     private boolean isMac = false;
49     private boolean isLinux = false;
50     
51     
52     public void setRequest(HttpServletRequest JavaDoc req)
53     {
54         this.request = req;
55         this.useragent = request.getHeader("User-Agent");
56         this.languages = request.getHeader("Accept-Language");
57
58         if(this.languages != null)
59             this.languages = this.languages.toLowerCase();
60
61         if(useragent != null)
62         {
63             inituseragent();
64             initOs();
65         }
66     }
67
68     private void inituseragent()
69     {
70         String JavaDoc user = useragent.toLowerCase();
71         
72         if(user.indexOf("opera") != -1)
73         {
74             this.opera = true;
75         }
76         else if(user.indexOf("msie") != -1)
77         {
78             this.ie = true;
79         }
80         else if(user.indexOf("safari") != -1)
81         {
82             this.safari = true;
83         }
84         else if(user.indexOf("gecko") != -1)
85         {
86             this.mozilla = true;
87             if(user.indexOf("firefox") != -1)
88                 this.firefox = true;
89         }
90         else if(user.indexOf("netscape/7") != -1)
91         {
92             this.ns7 = true;
93         }
94         else if(user.indexOf("netscape6") != -1)
95         {
96             this.ns6 = true;
97         }
98         else if(user.indexOf("mozilla") != -1)
99         {
100             this.ns4 = true;
101         }
102
103         if(user.indexOf(".net clr") != -1)
104             this.netEnabled = true;
105     }
106
107     private void initOs()
108     {
109         String JavaDoc user = useragent.toLowerCase();
110         if (user.indexOf("win") > -1)
111         {
112             this.isWindows = true;
113             
114             if (user.indexOf("windows 95") > -1 || user.indexOf("win95") > -1)
115             {
116                 this.os = "Windows 95";
117             }
118             else if (user.indexOf("windows 98") > -1 || user.indexOf("win98") > -1)
119             {
120                 this.os = "Windows 98";
121             }
122             else if (user.indexOf("windows nt 5.1") > -1 || user.indexOf("winnt") > -1)
123             {
124                 this.os = "Windows XP";
125             }
126             else if (user.indexOf("windows nt") > -1 || user.indexOf("winnt") > -1)
127             {
128                 this.os = "Windows NT";
129             }
130             else if (user.indexOf("win16") > -1 || user.indexOf("windows 3.") > -1)
131             {
132                 this.os = "Windows 3.x";
133             }
134             else
135                 this.os = "Windows";
136
137         }
138         else if (user.indexOf("mac") > -1)
139         {
140             this.isMac = true;
141             
142             if (user.indexOf("mac_powerpc") > -1 || user.indexOf("mac_ppc") > -1)
143             {
144                 this.os = "macintosh power pc";
145             }
146             else if (user.indexOf("macintosh") > -1)
147             {
148                 this.os = "Macintosh";
149             }
150             else
151             {
152                 this.os = "Mac";
153             }
154         }
155         else if (user.indexOf("inux") > -1)
156         {
157             this.isLinux = true;
158             this.os = "Linux";
159         }
160         else
161         {
162             this.os = "Other";
163         }
164     }
165     
166     public String JavaDoc getUseragent()
167     {
168         return useragent;
169     }
170
171     public String JavaDoc getLanguageCode()
172     {
173         return this.languages;
174     }
175
176     public boolean isNetEnabled()
177     {
178         return netEnabled;
179     }
180
181     public boolean isIE()
182     {
183         return ie;
184     }
185
186     public boolean isNS7()
187     {
188         return ns7;
189     }
190
191     public boolean isNS6()
192     {
193         return ns6;
194     }
195
196     public boolean isNS4()
197     {
198         return ns4;
199     }
200     
201     public String JavaDoc getOs()
202     {
203         return os;
204     }
205     
206     public boolean isMozilla()
207     {
208         return mozilla;
209     }
210     
211     public boolean isNs4()
212     {
213         return ns4;
214     }
215     
216     public boolean isNs6()
217     {
218         return ns6;
219     }
220     
221     public boolean isNs7()
222     {
223         return ns7;
224     }
225     
226     public boolean isOpera()
227     {
228         return opera;
229     }
230     
231     public boolean isSafari()
232     {
233         return safari;
234     }
235     
236     public boolean isLinux()
237     {
238         return isLinux;
239     }
240     
241     public boolean isMac()
242     {
243         return isMac;
244     }
245     
246     public boolean isWindows()
247     {
248         return isWindows;
249     }
250     
251     public boolean isFirefox()
252     {
253         return firefox;
254     }
255     
256     public String JavaDoc toString()
257     {
258         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
259
260         sb.append("useragent=" + useragent + "\n");
261         sb.append("languages=" + languages + "\n");
262         sb.append("netEnabled=" + netEnabled + "\n");
263         sb.append("ie=" + ie + "\n");
264         sb.append("ns7=" + ns7 + "\n");
265         sb.append("ns6=" + ns6 + "\n");
266         sb.append("ns4=" + ns4 + "\n");
267         sb.append("opera=" + opera + "\n");
268         sb.append("mozilla=" + mozilla + "\n");
269         sb.append("safari=" + safari + "\n");
270         sb.append("firefox=" + firefox + "\n");
271         sb.append("os=" + os + "\n");
272         sb.append("isWindows=" + isWindows + "\n");
273         sb.append("isMac=" + isMac + "\n");
274         sb.append("isLinux=" + isLinux + "\n");
275         
276         return sb.toString();
277     }
278 }
Popular Tags