1   /**
2    * Copyright (c) 2000-2009 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 String getBrowserId(HttpServletRequest request) {
60          if (isIe(request)) {
61              return BROWSER_ID_IE;
62          }
63          else if (isFirefox(request)) {
64              return BROWSER_ID_FIREFOX;
65          }
66          else {
67              return BROWSER_ID_OTHER;
68          }
69      }
70  
71      public float getMajorVersion(HttpServletRequest request) {
72          float majorVersion = 0;
73  
74          String version = getVersion(request);
75  
76          Matcher matcher = _majorVersionPattern.matcher(version);
77  
78          if (matcher.find()) {
79              majorVersion = GetterUtil.getFloat(matcher.group(1));
80          }
81  
82          return majorVersion;
83      }
84  
85      public String getRevision(HttpServletRequest request) {
86          String revision = StringPool.BLANK;
87  
88          String userAgent = getUserAgent(request);
89  
90          Matcher matcher = _revisionPattern.matcher(userAgent);
91  
92          while (matcher.find()) {
93              for (int i = 1; i <= matcher.groupCount(); i++) {
94                  revision = matcher.group(i);
95              }
96          }
97  
98          return revision;
99      }
100 
101     public String getVersion(HttpServletRequest request) {
102         String userAgent = getUserAgent(request);
103 
104         String version = StringPool.BLANK;
105 
106         Matcher matcher = _versionPattern.matcher(userAgent);
107 
108         if (matcher.find()) {
109             version = matcher.group(1);
110         }
111         else if (isFirefox(request)) {
112             Matcher versionFirefoxMatcher = _versionFirefoxPattern.matcher(
113                 userAgent);
114 
115             if (versionFirefoxMatcher.find()) {
116                 version = versionFirefoxMatcher.group(1);
117             }
118         }
119         else if (isChrome(request)) {
120             Matcher versionChromeMatcher = _versionChromePattern.matcher(
121                 userAgent);
122 
123             if (versionChromeMatcher.find()) {
124                 version = versionChromeMatcher.group(1);
125             }
126         }
127         else {
128             version = getRevision(request);
129         }
130 
131         return version;
132     }
133 
134     public boolean isAir(HttpServletRequest request) {
135         String userAgent = getUserAgent(request);
136 
137         if (userAgent.indexOf("adobeair") != -1) {
138             return true;
139         }
140 
141         return false;
142     }
143 
144     public boolean isChrome(HttpServletRequest request) {
145         String userAgent = getUserAgent(request);
146 
147         if (userAgent.indexOf("chrome") != -1) {
148             return true;
149         }
150 
151         return false;
152     }
153 
154     public boolean isFirefox(HttpServletRequest request) {
155         if (!isMozilla(request)) {
156             return false;
157         }
158 
159         String userAgent = getUserAgent(request);
160 
161         Matcher matcher = _firefoxPattern.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 accept = getAccept(request);
298 
299         if (accept.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         Matcher matcher = _webKitPattern.matcher(userAgent);
310 
311         if (matcher.find()) {
312             return true;
313         }
314 
315         return false;
316     }
317 
318     public boolean isWindows(HttpServletRequest request) {
319         String userAgent = getUserAgent(request);
320 
321         Matcher matcher = _windowsPattern.matcher(userAgent);
322 
323         if (matcher.find()) {
324             return true;
325         }
326 
327         return false;
328     }
329 
330     public boolean isWml(HttpServletRequest request) {
331         String accept = getAccept(request);
332 
333         if (accept.indexOf("wap.wml") != -1) {
334             return true;
335         }
336 
337         return false;
338     }
339 
340     protected String getAccept(HttpServletRequest request) {
341         String accept = StringPool.BLANK;
342 
343         if (request != null) {
344             String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
345 
346             if (acceptHeader != null) {
347                 accept = acceptHeader.toLowerCase();
348             }
349         }
350 
351         return accept;
352     }
353 
354     protected String getUserAgent(HttpServletRequest request) {
355         String userAgent = StringPool.BLANK;
356 
357         if (request != null) {
358             String userAgentHeader = request.getHeader(HttpHeaders.USER_AGENT);
359 
360             if (userAgentHeader != null) {
361                 userAgent = userAgentHeader.toLowerCase();
362             }
363         }
364 
365         return userAgent;
366     }
367 
368     private static Pattern _firefoxPattern = Pattern.compile(
369         "(firefox|minefield|granparadiso|bonecho|firebird|phoenix|camino)");
370 
371     private static Pattern _majorVersionPattern = Pattern.compile(
372         "(\\d+[.]\\d+)");
373 
374     private static Pattern _revisionPattern = Pattern.compile(
375         ".+(?:rv|it|ra|ie)[\\/: ]([\\d.]+)");
376 
377     private static Pattern _versionChromePattern = Pattern.compile(
378         "(?:chrome)[\\/]([\\d.]+)");
379 
380     private static Pattern _versionFirefoxPattern = Pattern.compile(
381         "(?:firefox|minefield)[\\/]([\\d.]+)");
382 
383     private static Pattern _versionPattern = Pattern.compile(
384         "(?:version)[\\/]([\\d.]+)");
385 
386     private static Pattern _webKitPattern = Pattern.compile(
387         "(khtml|applewebkit)");
388 
389     private static Pattern _windowsPattern = Pattern.compile(
390         "(windows|win32|16bit)");
391 
392 }