001    /**
002     * Copyright (c) 2000-2011 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.servlet.BrowserSniffer;
018    import com.liferay.portal.kernel.servlet.HttpHeaders;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
029     *
030     * @author Eduardo Lundgren
031     * @author Nate Cavanaugh
032     */
033    public class BrowserSnifferImpl implements BrowserSniffer {
034    
035            public boolean acceptsGzip(HttpServletRequest request) {
036                    String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
037    
038                    if ((acceptEncoding != null) &&
039                            (acceptEncoding.indexOf("gzip") != -1)) {
040    
041                            return true;
042                    }
043                    else {
044                            return false;
045                    }
046            }
047    
048            public String getBrowserId(HttpServletRequest request) {
049                    if (isIe(request)) {
050                            return BROWSER_ID_IE;
051                    }
052                    else if (isFirefox(request)) {
053                            return BROWSER_ID_FIREFOX;
054                    }
055                    else {
056                            return BROWSER_ID_OTHER;
057                    }
058            }
059    
060            public float getMajorVersion(HttpServletRequest request) {
061                    float majorVersion = 0;
062    
063                    String version = getVersion(request);
064    
065                    Matcher matcher = _majorVersionPattern.matcher(version);
066    
067                    if (matcher.find()) {
068                            majorVersion = GetterUtil.getFloat(matcher.group(1));
069                    }
070    
071                    return majorVersion;
072            }
073    
074            public String getRevision(HttpServletRequest request) {
075                    String revision = StringPool.BLANK;
076    
077                    String userAgent = getUserAgent(request);
078    
079                    Matcher matcher = _revisionPattern.matcher(userAgent);
080    
081                    if (matcher.find()) {
082                            revision = matcher.group(1);
083                    }
084    
085                    return revision;
086            }
087    
088            public String getVersion(HttpServletRequest request) {
089                    String userAgent = getUserAgent(request);
090    
091                    String version = StringPool.BLANK;
092    
093                    Matcher matcher = _versionPattern.matcher(userAgent);
094    
095                    if (matcher.find()) {
096                            version = matcher.group(1);
097                    }
098                    else if (isFirefox(request)) {
099                            Matcher versionFirefoxMatcher = _versionFirefoxPattern.matcher(
100                                    userAgent);
101    
102                            if (versionFirefoxMatcher.find()) {
103                                    version = versionFirefoxMatcher.group(1);
104                            }
105                    }
106                    else if (isChrome(request)) {
107                            Matcher versionChromeMatcher = _versionChromePattern.matcher(
108                                    userAgent);
109    
110                            if (versionChromeMatcher.find()) {
111                                    version = versionChromeMatcher.group(1);
112                            }
113                    }
114                    else {
115                            version = getRevision(request);
116                    }
117    
118                    return version;
119            }
120    
121            public boolean isAir(HttpServletRequest request) {
122                    String userAgent = getUserAgent(request);
123    
124                    if (userAgent.indexOf("adobeair") != -1) {
125                            return true;
126                    }
127    
128                    return false;
129            }
130    
131            public boolean isChrome(HttpServletRequest request) {
132                    String userAgent = getUserAgent(request);
133    
134                    if (userAgent.indexOf("chrome") != -1) {
135                            return true;
136                    }
137    
138                    return false;
139            }
140    
141            public boolean isFirefox(HttpServletRequest request) {
142                    if (!isMozilla(request)) {
143                            return false;
144                    }
145    
146                    String userAgent = getUserAgent(request);
147    
148                    Matcher matcher = _firefoxPattern.matcher(userAgent);
149    
150                    if (matcher.find()) {
151                            return true;
152                    }
153    
154                    return false;
155            }
156    
157            public boolean isGecko(HttpServletRequest request) {
158                    String userAgent = getUserAgent(request);
159    
160                    if (userAgent.indexOf("gecko") != -1) {
161                            return true;
162                    }
163    
164                    return false;
165            }
166    
167            public boolean isIe(HttpServletRequest request) {
168                    String userAgent = getUserAgent(request);
169    
170                    if ((userAgent.indexOf("msie") != -1) &&
171                            (userAgent.indexOf("opera") == -1)) {
172    
173                            return true;
174                    }
175    
176                    return false;
177            }
178    
179            public boolean isIphone(HttpServletRequest request) {
180                    String userAgent = getUserAgent(request);
181    
182                    if (userAgent.indexOf("iphone") != -1) {
183                            return true;
184                    }
185    
186                    return false;
187            }
188    
189            public boolean isLinux(HttpServletRequest request) {
190                    String userAgent = getUserAgent(request);
191    
192                    if (userAgent.indexOf("linux") != -1) {
193                            return true;
194                    }
195    
196                    return false;
197            }
198    
199            public boolean isMac(HttpServletRequest request) {
200                    String userAgent = getUserAgent(request);
201    
202                    if (userAgent.indexOf("mac") != -1) {
203                            return true;
204                    }
205    
206                    return false;
207            }
208    
209            public boolean isMobile(HttpServletRequest request) {
210                    String userAgent = getUserAgent(request);
211    
212                    if (userAgent.indexOf("mobile") != -1) {
213                            return true;
214                    }
215    
216                    return false;
217            }
218    
219            public boolean isMozilla(HttpServletRequest request) {
220                    String userAgent = getUserAgent(request);
221    
222                    if ((userAgent.indexOf("mozilla") != -1) &&
223                            (!userAgent.matches("compatible|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.indexOf("opera") != -1) {
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 (isSafari(request) && (majorVersion >= 3.0) && !isMobile(request)) {
253                            return true;
254                    }
255    
256                    return false;
257            }
258    
259            public boolean isSafari(HttpServletRequest request) {
260                    String userAgent = getUserAgent(request);
261    
262                    if (isWebKit(request) && (userAgent.indexOf("safari") != -1)) {
263                            return true;
264                    }
265    
266                    return false;
267            }
268    
269            public boolean isSun(HttpServletRequest request) {
270                    String userAgent = getUserAgent(request);
271    
272                    if (userAgent.indexOf("sunos") != -1) {
273                            return true;
274                    }
275    
276                    return false;
277            }
278    
279            public boolean isWap(HttpServletRequest request) {
280                    return isWapXhtml(request);
281            }
282    
283            public boolean isWapXhtml(HttpServletRequest request) {
284                    String accept = getAccept(request);
285    
286                    if (accept.indexOf("wap.xhtml") != -1) {
287                            return true;
288                    }
289    
290                    return false;
291            }
292    
293            public boolean isWebKit(HttpServletRequest request) {
294                    String userAgent = getUserAgent(request);
295    
296                    Matcher matcher = _webKitPattern.matcher(userAgent);
297    
298                    if (matcher.find()) {
299                            return true;
300                    }
301    
302                    return false;
303            }
304    
305            public boolean isWindows(HttpServletRequest request) {
306                    String userAgent = getUserAgent(request);
307    
308                    Matcher matcher = _windowsPattern.matcher(userAgent);
309    
310                    if (matcher.find()) {
311                            return true;
312                    }
313    
314                    return false;
315            }
316    
317            public boolean isWml(HttpServletRequest request) {
318                    String accept = getAccept(request);
319    
320                    if (accept.indexOf("wap.wml") != -1) {
321                            return true;
322                    }
323    
324                    return false;
325            }
326    
327            protected String getAccept(HttpServletRequest request) {
328                    String accept = StringPool.BLANK;
329    
330                    if (request != null) {
331                            String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
332    
333                            if (acceptHeader != null) {
334                                    accept = acceptHeader.toLowerCase();
335                            }
336                    }
337    
338                    return accept;
339            }
340    
341            protected String getUserAgent(HttpServletRequest request) {
342                    String userAgent = StringPool.BLANK;
343    
344                    if (request != null) {
345                            String userAgentHeader = request.getHeader(HttpHeaders.USER_AGENT);
346    
347                            if (userAgentHeader != null) {
348                                    userAgent = userAgentHeader.toLowerCase();
349                            }
350                    }
351    
352                    return userAgent;
353            }
354    
355            private static Pattern _firefoxPattern = Pattern.compile(
356                    "(firefox|minefield|granparadiso|bonecho|firebird|phoenix|camino)");
357            private static Pattern _majorVersionPattern = Pattern.compile(
358                    "(\\d+[.]\\d+)");
359            private static Pattern _revisionPattern = Pattern.compile(
360                    "(?:rv|it|ra|ie)[\\/: ]([\\d.]+)");
361            private static Pattern _versionChromePattern = Pattern.compile(
362                    "(?:chrome)[\\/]([\\d.]+)");
363            private static Pattern _versionFirefoxPattern = Pattern.compile(
364                    "(?:firefox|minefield)[\\/]([\\d.]+)");
365            private static Pattern _versionPattern = Pattern.compile(
366                    "(?:version)[\\/]([\\d.]+)");
367            private static Pattern _webKitPattern = Pattern.compile(
368                    "(khtml|applewebkit)");
369            private static Pattern _windowsPattern = Pattern.compile(
370                    "(windows|win32|16bit)");
371    
372    }