org.eclipse.jetty.servlets

## Class UserAgentFilter

- java.lang.Object
- - org.eclipse.jetty.servlets.UserAgentFilter

- All Implemented Interfaces:
    javax.servlet.Filter
  Direct Known Subclasses:
    GzipFilter

      public class UserAgentFilter
      extends Object
      implements javax.servlet.Filter

  User Agent Filter.

  This filter allows efficient matching of user agent strings for downstream or extended filters to use for browser specific logic.

  The filter is configured with the following init parameters:

  attribute
    If set, then the request attribute of this name is set with the matched user agent string
  cacheSize
    The size of the user-agent cache, used to avoid reparsing of user agent strings. The entire cache is flushed when this size is reached
  userAgent
    A regex `Pattern` to extract the essential elements of the user agent. The concatenation of matched pattern groups is used as the user agent name
  An example value for pattern is `(?:Mozilla[^\(]*\(compatible;\s*+([^;]*);.*)|(?:.*?([^\s]+/[^\s]+).*)`. These two pattern match the common compatibility user-agent strings and extract the real user agent, failing that, the first element of the agent string is returned.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `UserAgentFilter()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `destroy()`
    `void` | `doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)`
    `String` | `getUserAgent(javax.servlet.ServletRequest request)`
    `String` | `getUserAgent(String ua)` Get UserAgent.
    `void` | `init(javax.servlet.FilterConfig filterConfig)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### UserAgentFilter

          public UserAgentFilter()

  - ### Method Detail

    - #### destroy

          public void destroy()

      Specified by:
        `destroy` in interface `javax.servlet.Filter`

    - #### doFilter

          public void doFilter(javax.servlet.ServletRequest request,
                      javax.servlet.ServletResponse response,
                      javax.servlet.FilterChain chain)
                        throws IOException,
                               javax.servlet.ServletException

      Specified by:
        `doFilter` in interface `javax.servlet.Filter`
      Throws:
        `IOException`
        `javax.servlet.ServletException`

    - #### init

          public void init(javax.servlet.FilterConfig filterConfig)
                    throws javax.servlet.ServletException

      Specified by:
        `init` in interface `javax.servlet.Filter`
      Throws:
        `javax.servlet.ServletException`

    - #### getUserAgent

          public String getUserAgent(javax.servlet.ServletRequest request)

    - #### getUserAgent

          public String getUserAgent(String ua)

      Get UserAgent. The configured agent patterns are used to match against the passed user agent string. If any patterns match, the concatenation of pattern groups is returned as the user agent string. Match results are cached.
      Parameters:
        `ua` - A user agent string
      Returns:
        The matched pattern groups or the original user agent string

Copyright © 1995-2015 Mort Bay Consulting. All Rights Reserved.