001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.servlet;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: /**
026: * <a href="BrowserSniffer.java.html"><b><i>View Source</i></b></a>
027: *
028: * See http://www.zytrax.com/tech/web/browser_ids.htm for examples.
029: *
030: * @author Brian Wing Shun Chan
031: *
032: */
033: public class BrowserSniffer {
034:
035: public static boolean acceptsGzip(HttpServletRequest req) {
036: String acceptEncoding = req
037: .getHeader(HttpHeaders.ACCEPT_ENCODING);
038:
039: if ((acceptEncoding != null)
040: && (acceptEncoding.indexOf(_GZIP) != -1)) {
041:
042: return true;
043: } else {
044: return false;
045: }
046: }
047:
048: public static boolean is_ie(HttpServletRequest req) {
049: if (req == null) {
050: return false;
051: }
052:
053: String agent = req.getHeader(HttpHeaders.USER_AGENT);
054:
055: if (agent == null) {
056: return false;
057: }
058:
059: agent = agent.toLowerCase();
060:
061: if (agent.indexOf("msie") != -1) {
062: return true;
063: } else {
064: return false;
065: }
066: }
067:
068: public static boolean is_ie_4(HttpServletRequest req) {
069: if (req == null) {
070: return false;
071: }
072:
073: String agent = req.getHeader(HttpHeaders.USER_AGENT);
074:
075: if (agent == null) {
076: return false;
077: }
078:
079: agent = agent.toLowerCase();
080:
081: if (is_ie(req) && (agent.indexOf("msie 4") != -1)) {
082: return true;
083: } else {
084: return false;
085: }
086: }
087:
088: public static boolean is_ie_5(HttpServletRequest req) {
089: if (req == null) {
090: return false;
091: }
092:
093: String agent = req.getHeader(HttpHeaders.USER_AGENT);
094:
095: if (agent == null) {
096: return false;
097: }
098:
099: agent = agent.toLowerCase();
100:
101: if (is_ie(req) && (agent.indexOf("msie 5.0") != -1)) {
102: return true;
103: } else {
104: return false;
105: }
106: }
107:
108: public static boolean is_ie_5_5(HttpServletRequest req) {
109: if (req == null) {
110: return false;
111: }
112:
113: String agent = req.getHeader(HttpHeaders.USER_AGENT);
114:
115: if (agent == null) {
116: return false;
117: }
118:
119: agent = agent.toLowerCase();
120:
121: if (is_ie(req) && (agent.indexOf("msie 5.5") != -1)) {
122: return true;
123: } else {
124: return false;
125: }
126: }
127:
128: public static boolean is_ie_5_5_up(HttpServletRequest req) {
129: if (is_ie(req) && !is_ie_4(req) && !is_ie_5(req)) {
130: return true;
131: } else {
132: return false;
133: }
134: }
135:
136: public static boolean is_ie_6(HttpServletRequest req) {
137: if (req == null) {
138: return false;
139: }
140:
141: String agent = req.getHeader(HttpHeaders.USER_AGENT);
142:
143: if (agent == null) {
144: return false;
145: }
146:
147: agent = agent.toLowerCase();
148:
149: if (is_ie(req) && (agent.indexOf("msie 6.0") != -1)) {
150: return true;
151: } else {
152: return false;
153: }
154: }
155:
156: public static boolean is_ie_7(HttpServletRequest req) {
157: if (req == null) {
158: return false;
159: }
160:
161: String agent = req.getHeader(HttpHeaders.USER_AGENT);
162:
163: if (agent == null) {
164: return false;
165: }
166:
167: agent = agent.toLowerCase();
168:
169: if (is_ie(req) && (agent.indexOf("msie 7.0") != -1)) {
170: return true;
171: } else {
172: return false;
173: }
174: }
175:
176: public static boolean is_linux(HttpServletRequest req) {
177: String agent = req.getHeader(HttpHeaders.USER_AGENT);
178:
179: if (agent == null) {
180: return false;
181: }
182:
183: agent = agent.toLowerCase();
184:
185: if (agent.matches(".*linux.*")) {
186: return true;
187: } else {
188: return false;
189: }
190: }
191:
192: public static boolean is_mozilla(HttpServletRequest req) {
193: if (req == null) {
194: return false;
195: }
196:
197: String agent = req.getHeader(HttpHeaders.USER_AGENT);
198:
199: if (agent == null) {
200: return false;
201: }
202:
203: agent = agent.toLowerCase();
204:
205: if ((agent.indexOf("mozilla") != -1)
206: && (agent.indexOf("spoofer") == -1)
207: && (agent.indexOf("compatible") == -1)
208: && (agent.indexOf("opera") == -1)
209: && (agent.indexOf("webtv") == -1)
210: && (agent.indexOf("hotjava") == -1)) {
211:
212: return true;
213: } else {
214: return false;
215: }
216: }
217:
218: public static boolean is_mozilla_1_3_up(HttpServletRequest req) {
219: if (req == null) {
220: return false;
221: }
222:
223: String agent = req.getHeader(HttpHeaders.USER_AGENT);
224:
225: if (agent == null) {
226: return false;
227: }
228:
229: agent = agent.toLowerCase();
230:
231: if (is_mozilla(req)) {
232: int pos = agent.indexOf("gecko/");
233:
234: if (pos == -1) {
235: return false;
236: } else {
237: String releaseDate = agent.substring(pos + 6, agent
238: .length());
239:
240: if (releaseDate.compareTo("20030210") > 0) {
241: return true;
242: }
243: }
244: }
245:
246: return false;
247: }
248:
249: public static boolean is_ns_4(HttpServletRequest req) {
250: if (req == null) {
251: return false;
252: }
253:
254: String agent = req.getHeader(HttpHeaders.USER_AGENT);
255:
256: if (agent == null) {
257: return false;
258: }
259:
260: agent = agent.toLowerCase();
261:
262: if (!is_ie(req) && (agent.indexOf("mozilla/4.") != -1)) {
263: return true;
264: } else {
265: return false;
266: }
267: }
268:
269: public static boolean is_rtf(HttpServletRequest req) {
270: if (is_ie_5_5_up(req) || is_mozilla_1_3_up(req)) {
271: return true;
272: } else {
273: return false;
274: }
275: }
276:
277: public static boolean is_safari(HttpServletRequest req) {
278: if (req == null) {
279: return false;
280: }
281:
282: String agent = req.getHeader(HttpHeaders.USER_AGENT);
283:
284: if (agent == null) {
285: return false;
286: }
287:
288: agent = agent.toLowerCase();
289:
290: if (agent.indexOf("safari") != -1) {
291: return true;
292: } else {
293: return false;
294: }
295: }
296:
297: public static boolean is_wap_xhtml(HttpServletRequest req) {
298: if (req == null) {
299: return false;
300: }
301:
302: String accept = req.getHeader(HttpHeaders.ACCEPT);
303:
304: if (accept == null) {
305: return false;
306: }
307:
308: accept = accept.toLowerCase();
309:
310: if (accept.indexOf("wap.xhtml") != -1) {
311: return true;
312: } else {
313: return false;
314: }
315: }
316:
317: public static boolean is_wml(HttpServletRequest req) {
318: if (req == null) {
319: return false;
320: }
321:
322: String accept = req.getHeader(HttpHeaders.ACCEPT);
323:
324: if (accept == null) {
325: return false;
326: }
327:
328: accept = accept.toLowerCase();
329:
330: if (accept.indexOf("wap.wml") != -1) {
331: return true;
332: } else {
333: return false;
334: }
335: }
336:
337: private static final String _GZIP = "gzip";
338:
339: }
|