001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.servlet.BrowserSniffer;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
031     *
032     * @author Eduardo Lundgren
033     * @author Nate Cavanaugh
034     */
035    @DoPrivileged
036    public class BrowserSnifferImpl implements BrowserSniffer {
037    
038            public boolean acceptsGzip(HttpServletRequest request) {
039                    String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
040    
041                    if ((acceptEncoding != null) && acceptEncoding.contains("gzip")) {
042                            return true;
043                    }
044                    else {
045                            return false;
046                    }
047            }
048    
049            public String getBrowserId(HttpServletRequest request) {
050                    if (isIe(request)) {
051                            return BROWSER_ID_IE;
052                    }
053                    else if (isFirefox(request)) {
054                            return BROWSER_ID_FIREFOX;
055                    }
056                    else {
057                            return BROWSER_ID_OTHER;
058                    }
059            }
060    
061            public float getMajorVersion(HttpServletRequest request) {
062                    float majorVersion = 0;
063    
064                    String version = getVersion(request);
065    
066                    Matcher matcher = _majorVersionPattern.matcher(version);
067    
068                    if (matcher.find()) {
069                            majorVersion = GetterUtil.getFloat(matcher.group(1));
070                    }
071    
072                    return majorVersion;
073            }
074    
075            public String getRevision(HttpServletRequest request) {
076                    String revision = StringPool.BLANK;
077    
078                    String userAgent = getUserAgent(request);
079    
080                    Matcher matcher = _revisionPattern.matcher(userAgent);
081    
082                    if (matcher.find()) {
083                            revision = matcher.group(1);
084                    }
085    
086                    return revision;
087            }
088    
089            public String getVersion(HttpServletRequest request) {
090                    String userAgent = getUserAgent(request);
091    
092                    String version = StringPool.BLANK;
093    
094                    Matcher matcher = _versionPattern.matcher(userAgent);
095    
096                    if (matcher.find()) {
097                            version = matcher.group(1);
098                    }
099                    else if (isFirefox(request)) {
100                            Matcher versionFirefoxMatcher = _versionFirefoxPattern.matcher(
101                                    userAgent);
102    
103                            if (versionFirefoxMatcher.find()) {
104                                    version = versionFirefoxMatcher.group(1);
105                            }
106                    }
107                    else if (isChrome(request)) {
108                            Matcher versionChromeMatcher = _versionChromePattern.matcher(
109                                    userAgent);
110    
111                            if (versionChromeMatcher.find()) {
112                                    version = versionChromeMatcher.group(1);
113                            }
114                    }
115                    else {
116                            version = getRevision(request);
117                    }
118    
119                    return version;
120            }
121    
122            public boolean isAir(HttpServletRequest request) {
123                    String userAgent = getUserAgent(request);
124    
125                    if (userAgent.contains("adobeair")) {
126                            return true;
127                    }
128    
129                    return false;
130            }
131    
132            public boolean isChrome(HttpServletRequest request) {
133                    String userAgent = getUserAgent(request);
134    
135                    if (userAgent.contains("chrome")) {
136                            return true;
137                    }
138    
139                    return false;
140            }
141    
142            public boolean isFirefox(HttpServletRequest request) {
143                    if (!isMozilla(request)) {
144                            return false;
145                    }
146    
147                    String userAgent = getUserAgent(request);
148    
149                    for (String firefoxAlias : _FIREFOX_ALIASES) {
150                            if (userAgent.contains(firefoxAlias)) {
151                                    return true;
152                            }
153                    }
154    
155                    return false;
156            }
157    
158            public boolean isGecko(HttpServletRequest request) {
159                    String userAgent = getUserAgent(request);
160    
161                    if (userAgent.contains("gecko")) {
162                            return true;
163                    }
164    
165                    return false;
166            }
167    
168            public boolean isIe(HttpServletRequest request) {
169                    String userAgent = getUserAgent(request);
170    
171                    if (userAgent.contains("msie") && !userAgent.contains("opera")) {
172                            return true;
173                    }
174    
175                    return false;
176            }
177    
178            public boolean isIphone(HttpServletRequest request) {
179                    String userAgent = getUserAgent(request);
180    
181                    if (userAgent.contains("iphone")) {
182                            return true;
183                    }
184    
185                    return false;
186            }
187    
188            public boolean isLinux(HttpServletRequest request) {
189                    String userAgent = getUserAgent(request);
190    
191                    if (userAgent.contains("linux")) {
192                            return true;
193                    }
194    
195                    return false;
196            }
197    
198            public boolean isMac(HttpServletRequest request) {
199                    String userAgent = getUserAgent(request);
200    
201                    if (userAgent.contains("mac")) {
202                            return true;
203                    }
204    
205                    return false;
206            }
207    
208            public boolean isMobile(HttpServletRequest request) {
209                    String userAgent = getUserAgent(request);
210    
211                    if (userAgent.contains("mobile")) {
212                            return true;
213                    }
214    
215                    return false;
216            }
217    
218            public boolean isMozilla(HttpServletRequest request) {
219                    String userAgent = getUserAgent(request);
220    
221                    if (userAgent.contains("mozilla") &&
222                            !(userAgent.contains("compatible") ||
223                              userAgent.contains("webkit"))) {
224    
225                            return true;
226                    }
227    
228                    return false;
229            }
230    
231            public boolean isOpera(HttpServletRequest request) {
232                    String userAgent = getUserAgent(request);
233    
234                    if (userAgent.contains("opera")) {
235                            return true;
236                    }
237    
238                    return false;
239            }
240    
241            public boolean isRtf(HttpServletRequest request) {
242                    float majorVersion = getMajorVersion(request);
243    
244                    if (isIe(request) && (majorVersion >= 5.5)) {
245                            return true;
246                    }
247    
248                    if (isMozilla(request) && (majorVersion >= 1.3)) {
249                            return true;
250                    }
251    
252                    if (!isMobile(request)) {
253                            if (isOpera(request) && (majorVersion >= 10.0)) {
254                                    return true;
255                            }
256    
257                            if (isSafari(request) && (majorVersion >= 3.0)) {
258                                    return true;
259                            }
260                    }
261    
262                    return false;
263            }
264    
265            public boolean isSafari(HttpServletRequest request) {
266                    String userAgent = getUserAgent(request);
267    
268                    if (isWebKit(request) && userAgent.contains("safari")) {
269                            return true;
270                    }
271    
272                    return false;
273            }
274    
275            public boolean isSun(HttpServletRequest request) {
276                    String userAgent = getUserAgent(request);
277    
278                    if (userAgent.contains("sunos")) {
279                            return true;
280                    }
281    
282                    return false;
283            }
284    
285            public boolean isWap(HttpServletRequest request) {
286                    return isWapXhtml(request);
287            }
288    
289            public boolean isWapXhtml(HttpServletRequest request) {
290                    String accept = getAccept(request);
291    
292                    if (accept.contains("wap.xhtml")) {
293                            return true;
294                    }
295    
296                    return false;
297            }
298    
299            public boolean isWebKit(HttpServletRequest request) {
300                    String userAgent = getUserAgent(request);
301    
302                    for (String webKitAlias : _WEBKIT_ALIASES) {
303                            if (userAgent.contains(webKitAlias)) {
304                                    return true;
305                            }
306                    }
307    
308                    return false;
309            }
310    
311            public boolean isWindows(HttpServletRequest request) {
312                    String userAgent = getUserAgent(request);
313    
314                    for (String windowsAlias : _WINDOWS_ALIASES) {
315                            if (userAgent.contains(windowsAlias)) {
316                                    return true;
317                            }
318                    }
319    
320                    return false;
321            }
322    
323            public boolean isWml(HttpServletRequest request) {
324                    String accept = getAccept(request);
325    
326                    if (accept.contains("wap.wml")) {
327                            return true;
328                    }
329    
330                    return false;
331            }
332    
333            protected String getAccept(HttpServletRequest request) {
334                    String accept = StringPool.BLANK;
335    
336                    if (request == null) {
337                            return accept;
338                    }
339    
340                    accept = String.valueOf(request.getAttribute(HttpHeaders.ACCEPT));
341    
342                    if (Validator.isNotNull(accept)) {
343                            return accept;
344                    }
345    
346                    accept = request.getHeader(HttpHeaders.ACCEPT);
347    
348                    if (accept != null) {
349                            accept = accept.toLowerCase();
350                    }
351                    else {
352                            accept = StringPool.BLANK;
353                    }
354    
355                    request.setAttribute(HttpHeaders.ACCEPT, accept);
356    
357                    return accept;
358            }
359    
360            protected String getUserAgent(HttpServletRequest request) {
361                    String userAgent = StringPool.BLANK;
362    
363                    if (request == null) {
364                            return userAgent;
365                    }
366    
367                    userAgent = String.valueOf(
368                            request.getAttribute(HttpHeaders.USER_AGENT));
369    
370                    if (Validator.isNotNull(userAgent)) {
371                            return userAgent;
372                    }
373    
374                    userAgent = request.getHeader(HttpHeaders.USER_AGENT);
375    
376                    if (userAgent != null) {
377                            userAgent = userAgent.toLowerCase();
378                    }
379                    else {
380                            userAgent = StringPool.BLANK;
381                    }
382    
383                    request.setAttribute(HttpHeaders.USER_AGENT, userAgent);
384    
385                    return userAgent;
386            }
387    
388            private static final String[] _FIREFOX_ALIASES = {
389                    "firefox", "minefield", "granparadiso", "bonecho", "firebird",
390                    "phoenix", "camino"
391            };
392    
393            private static final String[] _WEBKIT_ALIASES = {"khtml", "applewebkit"};
394    
395            private static final String[] _WINDOWS_ALIASES = {
396                    "windows", "win32", "16bit"
397            };
398    
399            private static Pattern _majorVersionPattern = Pattern.compile(
400                    "(\\d+[.]\\d+)");
401            private static Pattern _revisionPattern = Pattern.compile(
402                    "(?:rv|it|ra|ie)[\\/: ]([\\d.]+)");
403            private static Pattern _versionChromePattern = Pattern.compile(
404                    "(?:chrome)[\\/]([\\d.]+)");
405            private static Pattern _versionFirefoxPattern = Pattern.compile(
406                    "(?:firefox|minefield)[\\/]([\\d.]+)");
407            private static Pattern _versionPattern = Pattern.compile(
408                    "(?:version)[\\/]([\\d.]+)");
409    
410    }