Class BrowserTool

java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.FormatConfig
org.apache.velocity.tools.view.BrowserToolDeprecatedMethods
org.apache.velocity.tools.view.BrowserTool
All Implemented Interfaces:
Serializable

@DefaultKey("browser") @InvalidScope("application") public class BrowserTool extends BrowserToolDeprecatedMethods implements Serializable

Browser sniffing tool (session or request scope requested, session scope advised).

Usage:

BrowserTool defines properties that are used to test the client browser, operating system, device, language...

All properties are boolean, excpet those in italic which are strings (and major/minor versions which are integers)

The following properties are available:

  • Device: device robot mobile tablet desktop tv
  • Features:css3 dom3
  • Browser:browser.name browser.majorVersion browser.minorVersion
  • Rendering engine: renderingEngine.name renderingEngine.minorVersion renderingEngine.majorVersion
  • Operating system: operatingsystem.name operatingsystem.majorVersion operatingsystem.minorVersion
  • Specific browser tests:netscape firefox safari MSIE opera links mozilla konqueror chrome
  • Specific rendering engine tests:gecko webKit KHTML trident blink edgeHTML presto
  • Specific OS tests:windows OSX linux unix BSD android iOS symbian
  • Languages: preferredLanguageTag (a string like 'en', 'da', 'en-US', ...), preferredLocale (a java Locale)
  • IP address: IPAddress

Language properties are filtered by the languagesFilter tool param, if present, which is here to specify which languages are acceptable on the server side. If no matching language is found, or if there is no matching language, the tools defaut locale (or the first value of languagesFilter) is returned. Their value is guarantied to belong to the set provided in languagesFilter, if any.

Notes on implementation:

  • The parsing algorithm is mainly empirical. Used rules are rather generic, so shouldn't need recent updates to be accurate, but accuracy remains far from guaranteed for new devices.
  • Parsing should be fast, as the parser only uses a single regex iteration on the user agent string.
  • Game consoles, e-readers, etc... are for now classified as mobile devices (but can sometimes be identified by their operating system).
  • Needless to say, the frontier between different device types can be very thin...

Thanks to Lee Semel (lee@semel.net), the author of the HTTP::BrowserDetect Perl module.

See also:

  • http://www.zytrax.com/tech/web/browser_ids.htm
  • http://en.wikipedia.org/wiki/User_agent
  • http://www.user-agents.org/
  • https://github.com/OpenDDR
  • https://devicemap.apache.org/
  • http://www.useragentstring.com/pages/useragentstring.php?name=All
  • https://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Cascading_Style_Sheets)
  • https://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Document_Object_Model)
  • http://www.webapps-online.com/online-tools/user-agent-strings
  • https://whichbrowser.net/data/

TODO:

  • parse X-Wap-Profile header if present
  • parse X-Requested-With header if present
Since:
VelocityTools 2.0
Version:
$Revision$ $Date$
Author:
Claude Brisson
See Also:
  • Constructor Details

    • BrowserTool

      public BrowserTool()
  • Method Details

    • setRequest

      public void setRequest(javax.servlet.http.HttpServletRequest request)
      Retrieves the User-Agent header from the request (if any).
      Parameters:
      request - servlet request
      See Also:
    • setUserAgentString

      public void setUserAgentString(String ua)
      Sets the User-Agent string to be parsed for info. If null, the string will be empty and everything will return false or null. Otherwise, it will set the whole string to lower case before storing to simplify parsing.
      Parameters:
      ua - user agent string
    • setAcceptLanguage

      public void setAcceptLanguage(String al)
    • setLanguagesFilter

      public void setLanguagesFilter(String filter)
    • getLanguagesFilter

      public String getLanguagesFilter()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • get

      public boolean get(String key)
    • getUserAgentString

      public String getUserAgentString()
      Specified by:
      getUserAgentString in class BrowserToolDeprecatedMethods
    • getAcceptLanguage

      public String getAcceptLanguage()
    • getIPAddress

      public String getIPAddress()

      Get the client browser IP address. In the session scope, which is the default, it corresponds to the first seen IP adress.

      The tool tries to get the real IP address whenever the request has been proxied.

      Please note that the result may be null.

      Returns:
      the IP address as a string
      Since:
      VelocityTools 3.0
    • getDevice

      public String getDevice()
      Returns:
      found device
      Since:
      VelocityTools 3.0
    • isRobot

      public boolean isRobot()
    • isTablet

      public boolean isTablet()
      Returns:
      whether found device is a tablet
      Since:
      VelocityTools 3.0
    • isMobile

      public boolean isMobile()
      Returns:
      whether found device is a mobile device
      Since:
      VelocityTools 3.0
    • isDesktop

      public boolean isDesktop()
      Returns:
      whether found device is a desktop device
      Since:
      VelocityTools 3.0
    • isTV

      public boolean isTV()
      Returns:
      whether found device is a TV
      Since:
      VelocityTools 3.0
    • getBrowser

      public UAParser.UAEntity getBrowser()
      Specified by:
      getBrowser in class BrowserToolDeprecatedMethods
      Returns:
      parsed browser
      Since:
      VelocityTools 3.0
    • getRenderingEngine

      public UAParser.UAEntity getRenderingEngine()
      Specified by:
      getRenderingEngine in class BrowserToolDeprecatedMethods
      Returns:
      parsed rendering engine
      Since:
      VelocityTools 3.0
    • getOperatingSystem

      public UAParser.UAEntity getOperatingSystem()
      Specified by:
      getOperatingSystem in class BrowserToolDeprecatedMethods
      Returns:
      parsed operating system
      Since:
      VelocityTools 3.0
    • isGecko

      public boolean isGecko()
      Specified by:
      isGecko in class BrowserToolDeprecatedMethods
    • isWebKit

      public boolean isWebKit()
    • isKHTML

      public boolean isKHTML()
    • isTrident

      public boolean isTrident()
    • isBlink

      public boolean isBlink()
    • isEdgeHTML

      public boolean isEdgeHTML()
    • isPresto

      public boolean isPresto()
    • isChrome

      public boolean isChrome()
      Specified by:
      isChrome in class BrowserToolDeprecatedMethods
    • isMSIE

      public boolean isMSIE()
      Specified by:
      isMSIE in class BrowserToolDeprecatedMethods
    • isFirefox

      public boolean isFirefox()
      Specified by:
      isFirefox in class BrowserToolDeprecatedMethods
    • isOpera

      public boolean isOpera()
      Specified by:
      isOpera in class BrowserToolDeprecatedMethods
    • isSafari

      public boolean isSafari()
      Specified by:
      isSafari in class BrowserToolDeprecatedMethods
    • isNetscape

      public boolean isNetscape()
      Specified by:
      isNetscape in class BrowserToolDeprecatedMethods
    • isKonqueror

      public boolean isKonqueror()
      Specified by:
      isKonqueror in class BrowserToolDeprecatedMethods
    • isLinks

      public boolean isLinks()
      Specified by:
      isLinks in class BrowserToolDeprecatedMethods
    • isMozilla

      public boolean isMozilla()
      Specified by:
      isMozilla in class BrowserToolDeprecatedMethods
    • isWindows

      public boolean isWindows()
      Specified by:
      isWindows in class BrowserToolDeprecatedMethods
    • isOSX

      public boolean isOSX()
      Specified by:
      isOSX in class BrowserToolDeprecatedMethods
    • isLinux

      public boolean isLinux()
      Specified by:
      isLinux in class BrowserToolDeprecatedMethods
    • isBSD

      public boolean isBSD()
    • isUnix

      public boolean isUnix()
    • isAndroid

      public boolean isAndroid()
    • isIOS

      public boolean isIOS()
    • isSymbian

      public boolean isSymbian()
    • isBlackberry

      public boolean isBlackberry()
    • getCss3

      public boolean getCss3()
    • getDom3

      public boolean getDom3()
    • getPreferredLanguage

      public String getPreferredLanguage()
    • getPreferredLocale

      public Locale getPreferredLocale()
    • test

      protected boolean test(String key)
      Specified by:
      test in class BrowserToolDeprecatedMethods