Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.beans; import java.io.Serializable; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import nl.bitwalker.useragentutils.UserAgent; /** * The Class DetectBrowserBean. */ public final class DetectBrowserBean implements Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The browser name. */ private String browserName; /** The browser version summary - the default is Mozilla/Firefox. */ private String version = "mozilla"; /** * Sets the request. * * @param req the new request */ public void setRequest(final HttpServletRequest req) { final String ua = req.getHeader("User-Agent"); final UserAgent userAgent = UserAgent.parseUserAgentString(ua); this.browserName = userAgent.getBrowser().getName(); if (StringUtils.contains(this.browserName, "Safari")) { this.version = "safari"; } if (StringUtils.contains(this.browserName, "Chrome")) { this.version = "chrome"; } if (StringUtils.contains(this.browserName, "Internet Explorer")) { // Assume IE8 unless proven otherwise later this.version = "ie8"; } if (StringUtils.contains(this.browserName, "Internet Explorer 7")) { this.version = "ie7"; } if (StringUtils.contains(this.browserName, "Internet Explorer 6") || StringUtils.contains(this.browserName, "Internet Explorer 5")) { this.version = "ie6"; } } /** * Returns a short string for the browser version. * * @return the browser version */ public String getVersion() { return this.version; } /** * Returns a boolean indicating whether the browser is a legacy version of IE. * * @return the boolean for the legacy IE flag */ public boolean isLegacyIE() { boolean legacy = false; if (StringUtils.equals(this.version, "ie6") || StringUtils.equals(this.version, "ie7")) { legacy = true; } return legacy; } /** * Returns a boolean indicating whether the browser is any version of IE. * * @return the boolean for the IE flag */ public boolean isIE() { boolean ie = false; if (StringUtils.startsWith(this.version, "ie")) { ie = true; } return ie; } }