Fixed NPE in case user agent pattern does not match.

This commit is contained in:
Simone Bordet 2012-03-08 11:42:14 +01:00
parent c66f3d2a9d
commit 0ab47e41bc
1 changed files with 19 additions and 24 deletions

View File

@ -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;
@ -52,7 +51,7 @@ 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<String, String> _agentCache = new ConcurrentHashMap<String, String>();
private int _agentCacheSize=1024;
private String _attribute;
@ -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;
}
}