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.servlet;
24  
25  import com.liferay.portal.kernel.servlet.BrowserSniffer;
26  import com.liferay.portal.kernel.servlet.HttpHeaders;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  /**
31   * <a href="BrowserSnifferImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class BrowserSnifferImpl implements BrowserSniffer {
39  
40      public boolean acceptsGzip(HttpServletRequest request) {
41          String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
42  
43          if ((acceptEncoding != null) &&
44              (acceptEncoding.indexOf(_GZIP) != -1)) {
45  
46              return true;
47          }
48          else {
49              return false;
50          }
51      }
52  
53      public boolean is_ie(HttpServletRequest request) {
54          if (request == null) {
55              return false;
56          }
57  
58          String agent = request.getHeader(HttpHeaders.USER_AGENT);
59  
60          if (agent == null) {
61              return false;
62          }
63  
64          agent = agent.toLowerCase();
65  
66          if (agent.indexOf("msie") != -1) {
67              return true;
68          }
69          else {
70              return false;
71          }
72      }
73  
74      public boolean is_ie_4(HttpServletRequest request) {
75          if (request == null) {
76              return false;
77          }
78  
79          String agent = request.getHeader(HttpHeaders.USER_AGENT);
80  
81          if (agent == null) {
82              return false;
83          }
84  
85          agent = agent.toLowerCase();
86  
87          if (is_ie(request) && (agent.indexOf("msie 4") != -1)) {
88              return true;
89          }
90          else {
91              return false;
92          }
93      }
94  
95      public boolean is_ie_5(HttpServletRequest request) {
96          if (request == null) {
97              return false;
98          }
99  
100         String agent = request.getHeader(HttpHeaders.USER_AGENT);
101 
102         if (agent == null) {
103             return false;
104         }
105 
106         agent = agent.toLowerCase();
107 
108         if (is_ie(request) && (agent.indexOf("msie 5.0") != -1)) {
109             return true;
110         }
111         else {
112             return false;
113         }
114     }
115 
116     public boolean is_ie_5_5(HttpServletRequest request) {
117         if (request == null) {
118             return false;
119         }
120 
121         String agent = request.getHeader(HttpHeaders.USER_AGENT);
122 
123         if (agent == null) {
124             return false;
125         }
126 
127         agent = agent.toLowerCase();
128 
129         if (is_ie(request) && (agent.indexOf("msie 5.5") != -1)) {
130             return true;
131         }
132         else {
133             return false;
134         }
135     }
136 
137     public boolean is_ie_5_5_up(HttpServletRequest request) {
138         if (is_ie(request) && !is_ie_4(request) && !is_ie_5(request)) {
139             return true;
140         }
141         else {
142             return false;
143         }
144     }
145 
146     public boolean is_ie_6(HttpServletRequest request) {
147         if (request == null) {
148             return false;
149         }
150 
151         String agent = request.getHeader(HttpHeaders.USER_AGENT);
152 
153         if (agent == null) {
154             return false;
155         }
156 
157         agent = agent.toLowerCase();
158 
159         if (is_ie(request) && (agent.indexOf("msie 6.0") != -1)) {
160             return true;
161         }
162         else {
163             return false;
164         }
165     }
166 
167     public boolean is_ie_7(HttpServletRequest request) {
168         if (request == null) {
169             return false;
170         }
171 
172         String agent = request.getHeader(HttpHeaders.USER_AGENT);
173 
174         if (agent == null) {
175             return false;
176         }
177 
178         agent = agent.toLowerCase();
179 
180         if (is_ie(request) && (agent.indexOf("msie 7.0") != -1)) {
181             return true;
182         }
183         else {
184             return false;
185         }
186     }
187 
188     public boolean is_linux(HttpServletRequest request) {
189         String agent = request.getHeader(HttpHeaders.USER_AGENT);
190 
191         if (agent == null) {
192             return false;
193         }
194 
195         agent = agent.toLowerCase();
196 
197         if (agent.matches(".*linux.*")) {
198             return true;
199         }
200         else {
201             return false;
202         }
203     }
204 
205     public boolean is_mozilla(HttpServletRequest request) {
206         if (request == null) {
207             return false;
208         }
209 
210         String agent = request.getHeader(HttpHeaders.USER_AGENT);
211 
212         if (agent == null) {
213             return false;
214         }
215 
216         agent = agent.toLowerCase();
217 
218         if ((agent.indexOf("mozilla") != -1) &&
219             (agent.indexOf("spoofer") == -1) &&
220             (agent.indexOf("compatible") == -1) &&
221             (agent.indexOf("opera") == -1) &&
222             (agent.indexOf("webtv") == -1) &&
223             (agent.indexOf("hotjava") == -1)) {
224 
225             return true;
226         }
227         else {
228             return false;
229         }
230     }
231 
232     public boolean is_mozilla_1_3_up(HttpServletRequest request) {
233         if (request == null) {
234             return false;
235         }
236 
237         String agent = request.getHeader(HttpHeaders.USER_AGENT);
238 
239         if (agent == null) {
240             return false;
241         }
242 
243         agent = agent.toLowerCase();
244 
245         if (is_mozilla(request)) {
246             int pos = agent.indexOf("gecko/");
247 
248             if (pos == -1) {
249                 return false;
250             }
251             else {
252                 String releaseDate = agent.substring(pos + 6, agent.length());
253 
254                 if (releaseDate.compareTo("20030210") > 0) {
255                     return true;
256                 }
257             }
258         }
259 
260         return false;
261     }
262 
263     public boolean is_ns_4(HttpServletRequest request) {
264         if (request == null) {
265             return false;
266         }
267 
268         String agent = request.getHeader(HttpHeaders.USER_AGENT);
269 
270         if (agent == null) {
271             return false;
272         }
273 
274         agent = agent.toLowerCase();
275 
276         if (!is_ie(request) && (agent.indexOf("mozilla/4.") != -1)) {
277             return true;
278         }
279         else {
280             return false;
281         }
282     }
283 
284     public boolean is_rtf(HttpServletRequest request) {
285         if (is_ie_5_5_up(request) || is_mozilla_1_3_up(request) ||
286             (is_safari_3(request) && !is_safari_mobile(request))) {
287 
288             return true;
289         }
290         else {
291             return false;
292         }
293     }
294 
295     public boolean is_safari(HttpServletRequest request) {
296         if (request == null) {
297             return false;
298         }
299 
300         String agent = request.getHeader(HttpHeaders.USER_AGENT);
301 
302         if (agent == null) {
303             return false;
304         }
305 
306         agent = agent.toLowerCase();
307 
308         if (agent.indexOf("safari") != -1) {
309             return true;
310         }
311         else {
312             return false;
313         }
314     }
315 
316     public boolean is_safari_3(HttpServletRequest request) {
317         if (request == null) {
318             return false;
319         }
320 
321         String agent = request.getHeader(HttpHeaders.USER_AGENT);
322 
323         if (agent == null) {
324             return false;
325         }
326 
327         agent = agent.toLowerCase();
328 
329         if (is_safari(request) && (agent.indexOf("version/3.") != -1)) {
330             return true;
331         }
332         else {
333             return false;
334         }
335     }
336 
337     public boolean is_safari_mobile(HttpServletRequest request) {
338         if (request == null) {
339             return false;
340         }
341 
342         String agent = request.getHeader(HttpHeaders.USER_AGENT);
343 
344         if (agent == null) {
345             return false;
346         }
347 
348         agent = agent.toLowerCase();
349 
350         if (is_safari(request) && (agent.indexOf("mobile") != -1)) {
351             return true;
352         }
353         else {
354             return false;
355         }
356     }
357 
358     public boolean is_wap(HttpServletRequest request) {
359         return is_wap_xhtml(request);
360     }
361 
362     public boolean is_wap_xhtml(HttpServletRequest request) {
363         if (request == null) {
364             return false;
365         }
366 
367         String accept = request.getHeader(HttpHeaders.ACCEPT);
368 
369         if (accept == null) {
370             return false;
371         }
372 
373         accept = accept.toLowerCase();
374 
375         if (accept.indexOf("wap.xhtml") != -1) {
376             return true;
377         }
378         else {
379             return false;
380         }
381     }
382 
383     public boolean is_wml(HttpServletRequest request) {
384         if (request == null) {
385             return false;
386         }
387 
388         String accept = request.getHeader(HttpHeaders.ACCEPT);
389 
390         if (accept == null) {
391             return false;
392         }
393 
394         accept = accept.toLowerCase();
395 
396         if (accept.indexOf("wap.wml") != -1) {
397             return true;
398         }
399         else {
400             return false;
401         }
402     }
403 
404     private static final String _GZIP = "gzip";
405 
406 }