From 0ab47e41bc7a59d4814ca720a9009af03a3fc27f Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 8 Mar 2012 11:42:14 +0100 Subject: [PATCH] Fixed NPE in case user agent pattern does not match. --- .../jetty/servlets/UserAgentFilter.java | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/UserAgentFilter.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/UserAgentFilter.java index 71d60024c62..c489c9c21de 100644 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/UserAgentFilter.java +++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/UserAgentFilter.java @@ -4,11 +4,11 @@ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at +// The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. +// You may elect to redistribute this code under either of these licenses. // ======================================================================== package org.eclipse.jetty.servlets; @@ -18,7 +18,6 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -39,20 +38,20 @@ import javax.servlet.http.HttpServletRequest; *
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 {@link Pattern} to extract the essential elements of the user agent. + *
userAgent
A regex {@link 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. - * + * element of the agent string is returned. + * * */ public class UserAgentFilter implements Filter { private static final String __defaultPattern = "(?:Mozilla[^\\(]*\\(compatible;\\s*+([^;]*);.*)|(?:.*?([^\\s]+/[^\\s]+).*)"; private Pattern _pattern = Pattern.compile(__defaultPattern); - private Map _agentCache = new ConcurrentHashMap(); + private Map _agentCache = new ConcurrentHashMap(); private int _agentCacheSize=1024; private String _attribute; @@ -71,7 +70,7 @@ public class UserAgentFilter implements Filter public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (_attribute!=null && _pattern!=null) - { + { String ua=getUserAgent(request); request.setAttribute(_attribute,ua); } @@ -85,11 +84,11 @@ public class UserAgentFilter implements Filter public void init(FilterConfig filterConfig) throws ServletException { _attribute=filterConfig.getInitParameter("attribute"); - + String p=filterConfig.getInitParameter("userAgent"); if (p!=null) _pattern=Pattern.compile(p); - + String size=filterConfig.getInitParameter("cacheSize"); if (size!=null) _agentCacheSize=Integer.parseInt(size); @@ -101,7 +100,7 @@ public class UserAgentFilter implements Filter String ua=((HttpServletRequest)request).getHeader("User-Agent"); return getUserAgent(ua); } - + /* ------------------------------------------------------------ */ /** Get UserAgent. * The configured agent patterns are used to match against the passed user agent string. @@ -112,11 +111,10 @@ public class UserAgentFilter implements Filter */ public String getUserAgent(String ua) { - if (ua==null) + if (ua == null) return null; - - String tag = (String)_agentCache.get(ua); - + + String tag = _agentCache.get(ua); if (tag == null) { @@ -131,7 +129,7 @@ public class UserAgentFilter implements Filter { String group = matcher.group(g); if (group != null) - tag = tag == null?group:(tag + group); + tag = tag == null ? group : tag + group; } } else @@ -140,18 +138,15 @@ public class UserAgentFilter implements Filter } } } - else - { + + if (tag == null) tag = ua; - } if (_agentCache.size() >= _agentCacheSize) - { _agentCache.clear(); - } - _agentCache.put(ua,tag); - + _agentCache.put(ua, tag); } + return tag; } }