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  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  
30  import java.util.regex.Matcher;
31  import java.util.regex.Pattern;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  /**
36   * <a href="BrowserSnifferImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
39   *
40   * @author Eduardo Lundgren
41   * @author Nate Cavanaugh
42   *
43   */
44  public class BrowserSnifferImpl implements BrowserSniffer {
45  
46      public boolean acceptsGzip(HttpServletRequest request) {
47          String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
48  
49          if ((acceptEncoding != null) &&
50              (acceptEncoding.indexOf("gzip") != -1)) {
51  
52              return true;
53          }
54          else {
55              return false;
56          }
57      }
58  
59      public float getMajorVersion(HttpServletRequest request) {
60          float majorVersion = 0;
61  
62          String version = getVersion(request);
63  
64          Pattern pattern = Pattern.compile("(\\d+[.]\\d+)");
65  
66          Matcher matcher = pattern.matcher(version);
67  
68          if (matcher.find()) {
69              majorVersion = GetterUtil.getFloat(matcher.group(1));
70          }
71  
72          return majorVersion;
73      }
74  
75      public String getRevision(HttpServletRequest request) {
76          String revision = StringPool.BLANK;
77  
78          String userAgent = getUserAgent(request);
79  
80          Pattern pattern = Pattern.compile(".+(?:rv|it|ra|ie)[\\/: ]([\\d.]+)");
81  
82          Matcher matcher = pattern.matcher(userAgent);
83  
84          while (matcher.find()) {
85              for (int i = 1; i <= matcher.groupCount(); i++) {
86                  revision = matcher.group(i);
87              }
88          }
89  
90          return revision;
91      }
92  
93      public String getVersion(HttpServletRequest request) {
94          String userAgent = getUserAgent(request);
95  
96          String version = StringPool.BLANK;
97  
98          Pattern pattern = Pattern.compile("(?:version)[\\/]([\\d.]+)");
99  
100         Matcher matcher = pattern.matcher(userAgent);
101 
102         if (matcher.find()) {
103             version = matcher.group(1);
104         }
105         else if (isFirefox(request)) {
106             Pattern firefoxPattern = Pattern.compile(
107                 "(?:firefox|minefield)[\\/]([\\d.]+)");
108 
109             Matcher firefoxMatcher = firefoxPattern.matcher(userAgent);
110 
111             if (firefoxMatcher.find()) {
112                 version = firefoxMatcher.group(1);
113             }
114         }
115         else if (isChrome(request)) {
116             Pattern chromePattern = Pattern.compile("(?:chrome)[\\/]([\\d.]+)");
117 
118             Matcher chromeMatcher = chromePattern.matcher(userAgent);
119 
120             if (chromeMatcher.find()) {
121                 version = chromeMatcher.group(1);
122             }
123         }
124         else {
125             version = getRevision(request);
126         }
127 
128         return version;
129     }
130 
131     public boolean isAir(HttpServletRequest request) {
132         String userAgent = getUserAgent(request);
133 
134         if (userAgent.indexOf("adobeair") != -1) {
135             return true;
136         }
137 
138         return false;
139     }
140 
141     public boolean isChrome(HttpServletRequest request) {
142         String userAgent = getUserAgent(request);
143 
144         if (userAgent.indexOf("chrome") != -1) {
145             return true;
146         }
147 
148         return false;
149     }
150 
151     public boolean isFirefox(HttpServletRequest request) {
152         if (!isMozilla(request)) {
153             return false;
154         }
155 
156         String userAgent = getUserAgent(request);
157 
158         Pattern pattern = Pattern.compile(
159             "(firefox|minefield|granparadiso|bonecho|firebird|phoenix|camino)");
160 
161         Matcher matcher = pattern.matcher(userAgent);
162 
163         if (matcher.find()) {
164             return true;
165         }
166 
167         return false;
168     }
169 
170     public boolean isGecko(HttpServletRequest request) {
171         String userAgent = getUserAgent(request);
172 
173         if (userAgent.indexOf("gecko") != -1) {
174             return true;
175         }
176 
177         return false;
178     }
179 
180     public boolean isIe(HttpServletRequest request) {
181         String userAgent = getUserAgent(request);
182 
183         if ((userAgent.indexOf("msie") != -1) &&
184             (userAgent.indexOf("opera") == -1)) {
185 
186             return true;
187         }
188 
189         return false;
190     }
191 
192     public boolean isIphone(HttpServletRequest request) {
193         String userAgent = getUserAgent(request);
194 
195         if (userAgent.indexOf("iphone") != -1) {
196             return true;
197         }
198 
199         return false;
200     }
201 
202     public boolean isLinux(HttpServletRequest request) {
203         String userAgent = getUserAgent(request);
204 
205         if (userAgent.indexOf("linux") != -1) {
206             return true;
207         }
208 
209         return false;
210     }
211 
212     public boolean isMac(HttpServletRequest request) {
213         String userAgent = getUserAgent(request);
214 
215         if (userAgent.indexOf("mac") != -1) {
216             return true;
217         }
218 
219         return false;
220     }
221 
222     public boolean isMobile(HttpServletRequest request) {
223         String userAgent = getUserAgent(request);
224 
225         if (userAgent.indexOf("mobile") != -1) {
226             return true;
227         }
228 
229         return false;
230     }
231 
232     public boolean isMozilla(HttpServletRequest request) {
233         String userAgent = getUserAgent(request);
234 
235         if ((userAgent.indexOf("mozilla") != -1) &&
236             (!userAgent.matches("compatible|webkit"))) {
237 
238             return true;
239         }
240 
241         return false;
242     }
243 
244     public boolean isOpera(HttpServletRequest request) {
245         String userAgent = getUserAgent(request);
246 
247         if (userAgent.indexOf("opera") != -1) {
248             return true;
249         }
250 
251         return false;
252     }
253 
254     public boolean isRtf(HttpServletRequest request) {
255         float majorVersion = getMajorVersion(request);
256 
257         if (isIe(request) && (majorVersion >= 5.5)) {
258             return true;
259         }
260 
261         if (isMozilla(request) && (majorVersion >= 1.3)) {
262             return true;
263         }
264 
265         if (isSafari(request) && (majorVersion >= 3.0) && !isMobile(request)) {
266             return true;
267         }
268 
269         return false;
270     }
271 
272     public boolean isSafari(HttpServletRequest request) {
273         String userAgent = getUserAgent(request);
274 
275         if (isWebKit(request) && (userAgent.indexOf("safari") != -1)) {
276             return true;
277         }
278 
279         return false;
280     }
281 
282     public boolean isSun(HttpServletRequest request) {
283         String userAgent = getUserAgent(request);
284 
285         if (userAgent.indexOf("sunos") != -1) {
286             return true;
287         }
288 
289         return false;
290     }
291 
292     public boolean isWap(HttpServletRequest request) {
293         return isWapXhtml(request);
294     }
295 
296     public boolean isWapXhtml(HttpServletRequest request) {
297         String userAgent = getUserAgent(request);
298 
299         if (userAgent.indexOf("wap.xhtml") != -1) {
300             return true;
301         }
302 
303         return false;
304     }
305 
306     public boolean isWebKit(HttpServletRequest request) {
307         String userAgent = getUserAgent(request);
308 
309         Pattern pattern = Pattern.compile("(khtml|applewebkit)");
310 
311         Matcher matcher = pattern.matcher(userAgent);
312 
313         if (matcher.find()) {
314             return true;
315         }
316 
317         return false;
318     }
319 
320     public boolean isWindows(HttpServletRequest request) {
321         String userAgent = getUserAgent(request);
322 
323         Pattern pattern = Pattern.compile("(windows|win32|16bit)");
324 
325         Matcher matcher = pattern.matcher(userAgent);
326 
327         if (matcher.find()) {
328             return true;
329         }
330 
331         return false;
332     }
333 
334     public boolean isWml(HttpServletRequest request) {
335         String userAgent = getUserAgent(request);
336 
337         if (userAgent.indexOf("wap.wml") != -1) {
338             return true;
339         }
340 
341         return false;
342     }
343 
344     protected String getUserAgent(HttpServletRequest request) {
345         String userAgent = StringPool.BLANK;
346 
347         if (request != null) {
348             String userAgentHeader = request.getHeader(HttpHeaders.USER_AGENT);
349 
350             if (userAgentHeader != null) {
351                 userAgent = userAgentHeader.toLowerCase();
352             }
353         }
354 
355         return userAgent;
356     }
357 
358 }