1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  /**
28   * <a href="BrowserSniffer.java.html"><b><i>View Source</i></b></a>
29   *
30   * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class BrowserSniffer {
36  
37      public static boolean acceptsGzip(HttpServletRequest req) {
38          String acceptEncoding = req.getHeader(HttpHeaders.ACCEPT_ENCODING);
39  
40          if ((acceptEncoding != null) &&
41              (acceptEncoding.indexOf(_GZIP) != -1)) {
42  
43              return true;
44          }
45          else {
46              return false;
47          }
48      }
49  
50      public static boolean is_ie(HttpServletRequest req) {
51          if (req == null) {
52              return false;
53          }
54  
55          String agent = req.getHeader(HttpHeaders.USER_AGENT);
56  
57          if (agent == null) {
58              return false;
59          }
60  
61          agent = agent.toLowerCase();
62  
63          if (agent.indexOf("msie") != -1) {
64              return true;
65          }
66          else {
67              return false;
68          }
69      }
70  
71      public static boolean is_ie_4(HttpServletRequest req) {
72          if (req == null) {
73              return false;
74          }
75  
76          String agent = req.getHeader(HttpHeaders.USER_AGENT);
77  
78          if (agent == null) {
79              return false;
80          }
81  
82          agent = agent.toLowerCase();
83  
84          if (is_ie(req) && (agent.indexOf("msie 4") != -1)) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      public static boolean is_ie_5(HttpServletRequest req) {
93          if (req == null) {
94              return false;
95          }
96  
97          String agent = req.getHeader(HttpHeaders.USER_AGENT);
98  
99          if (agent == null) {
100             return false;
101         }
102 
103         agent = agent.toLowerCase();
104 
105         if (is_ie(req) && (agent.indexOf("msie 5.0") != -1)) {
106             return true;
107         }
108         else {
109             return false;
110         }
111     }
112 
113     public static boolean is_ie_5_5(HttpServletRequest req) {
114         if (req == null) {
115             return false;
116         }
117 
118         String agent = req.getHeader(HttpHeaders.USER_AGENT);
119 
120         if (agent == null) {
121             return false;
122         }
123 
124         agent = agent.toLowerCase();
125 
126         if (is_ie(req) && (agent.indexOf("msie 5.5") != -1)) {
127             return true;
128         }
129         else {
130             return false;
131         }
132     }
133 
134     public static boolean is_ie_5_5_up(HttpServletRequest req) {
135         if (is_ie(req) && !is_ie_4(req) && !is_ie_5(req)) {
136             return true;
137         }
138         else {
139             return false;
140         }
141     }
142 
143     public static boolean is_ie_6(HttpServletRequest req) {
144         if (req == null) {
145             return false;
146         }
147 
148         String agent = req.getHeader(HttpHeaders.USER_AGENT);
149 
150         if (agent == null) {
151             return false;
152         }
153 
154         agent = agent.toLowerCase();
155 
156         if (is_ie(req) && (agent.indexOf("msie 6.0") != -1)) {
157             return true;
158         }
159         else {
160             return false;
161         }
162     }
163 
164     public static boolean is_ie_7(HttpServletRequest req) {
165         if (req == null) {
166             return false;
167         }
168 
169         String agent = req.getHeader(HttpHeaders.USER_AGENT);
170 
171         if (agent == null) {
172             return false;
173         }
174 
175         agent = agent.toLowerCase();
176 
177         if (is_ie(req) && (agent.indexOf("msie 7.0") != -1)) {
178             return true;
179         }
180         else {
181             return false;
182         }
183     }
184 
185     public static boolean is_linux(HttpServletRequest req) {
186         String agent = req.getHeader(HttpHeaders.USER_AGENT);
187 
188         if (agent == null) {
189             return false;
190         }
191 
192         agent = agent.toLowerCase();
193 
194         if (agent.matches(".*linux.*")) {
195             return true;
196         }
197         else {
198             return false;
199         }
200     }
201 
202     public static boolean is_mozilla(HttpServletRequest req) {
203         if (req == null) {
204             return false;
205         }
206 
207         String agent = req.getHeader(HttpHeaders.USER_AGENT);
208 
209         if (agent == null) {
210             return false;
211         }
212 
213         agent = agent.toLowerCase();
214 
215         if ((agent.indexOf("mozilla") != -1) &&
216             (agent.indexOf("spoofer") == -1) &&
217             (agent.indexOf("compatible") == -1) &&
218             (agent.indexOf("opera") == -1) &&
219             (agent.indexOf("webtv") == -1) &&
220             (agent.indexOf("hotjava") == -1)) {
221 
222             return true;
223         }
224         else {
225             return false;
226         }
227     }
228 
229     public static boolean is_mozilla_1_3_up(HttpServletRequest req) {
230         if (req == null) {
231             return false;
232         }
233 
234         String agent = req.getHeader(HttpHeaders.USER_AGENT);
235 
236         if (agent == null) {
237             return false;
238         }
239 
240         agent = agent.toLowerCase();
241 
242         if (is_mozilla(req)) {
243             int pos = agent.indexOf("gecko/");
244 
245             if (pos == -1) {
246                 return false;
247             }
248             else {
249                 String releaseDate = agent.substring(pos + 6, agent.length());
250 
251                 if (releaseDate.compareTo("20030210") > 0) {
252                     return true;
253                 }
254             }
255         }
256 
257         return false;
258     }
259 
260     public static boolean is_ns_4(HttpServletRequest req) {
261         if (req == null) {
262             return false;
263         }
264 
265         String agent = req.getHeader(HttpHeaders.USER_AGENT);
266 
267         if (agent == null) {
268             return false;
269         }
270 
271         agent = agent.toLowerCase();
272 
273         if (!is_ie(req) && (agent.indexOf("mozilla/4.") != -1)) {
274             return true;
275         }
276         else {
277             return false;
278         }
279     }
280 
281     public static boolean is_rtf(HttpServletRequest req) {
282         if (is_ie_5_5_up(req) || is_mozilla_1_3_up(req) ||
283             (is_safari_3(req) && !is_safari_mobile(req))) {
284 
285             return true;
286         }
287         else {
288             return false;
289         }
290     }
291 
292     public static boolean is_safari(HttpServletRequest req) {
293         if (req == null) {
294             return false;
295         }
296 
297         String agent = req.getHeader(HttpHeaders.USER_AGENT);
298 
299         if (agent == null) {
300             return false;
301         }
302 
303         agent = agent.toLowerCase();
304 
305         if (agent.indexOf("safari") != -1) {
306             return true;
307         }
308         else {
309             return false;
310         }
311     }
312 
313     public static boolean is_safari_3(HttpServletRequest req) {
314         if (req == null) {
315             return false;
316         }
317 
318         String agent = req.getHeader(HttpHeaders.USER_AGENT);
319 
320         if (agent == null) {
321             return false;
322         }
323 
324         agent = agent.toLowerCase();
325 
326         if (is_safari(req) && (agent.indexOf("version/3.") != -1)) {
327             return true;
328         }
329         else {
330             return false;
331         }
332     }
333 
334     public static boolean is_safari_mobile(HttpServletRequest req) {
335         if (req == null) {
336             return false;
337         }
338 
339         String agent = req.getHeader(HttpHeaders.USER_AGENT);
340 
341         if (agent == null) {
342             return false;
343         }
344 
345         agent = agent.toLowerCase();
346 
347         if (is_safari(req) && (agent.indexOf("mobile") != -1)) {
348             return true;
349         }
350         else {
351             return false;
352         }
353     }
354 
355     public static boolean is_wap_xhtml(HttpServletRequest req) {
356         if (req == null) {
357             return false;
358         }
359 
360         String accept = req.getHeader(HttpHeaders.ACCEPT);
361 
362         if (accept == null) {
363             return false;
364         }
365 
366         accept = accept.toLowerCase();
367 
368         if (accept.indexOf("wap.xhtml") != -1) {
369             return true;
370         }
371         else {
372             return false;
373         }
374     }
375 
376     public static boolean is_wml(HttpServletRequest req) {
377         if (req == null) {
378             return false;
379         }
380 
381         String accept = req.getHeader(HttpHeaders.ACCEPT);
382 
383         if (accept == null) {
384             return false;
385         }
386 
387         accept = accept.toLowerCase();
388 
389         if (accept.indexOf("wap.wml") != -1) {
390             return true;
391         }
392         else {
393             return false;
394         }
395     }
396 
397     private static final String _GZIP = "gzip";
398 
399 }