1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.servlets; 20 21 import java.io.IOException; 22 import java.util.Map; 23 import java.util.concurrent.ConcurrentHashMap; 24 import java.util.regex.Matcher; 25 import java.util.regex.Pattern; 26 import javax.servlet.Filter; 27 import javax.servlet.FilterChain; 28 import javax.servlet.FilterConfig; 29 import javax.servlet.ServletException; 30 import javax.servlet.ServletRequest; 31 import javax.servlet.ServletResponse; 32 import javax.servlet.http.HttpServletRequest; 33 34 /* ------------------------------------------------------------ */ 35 /** User Agent Filter. 36 * <p> 37 * This filter allows efficient matching of user agent strings for 38 * downstream or extended filters to use for browser specific logic. 39 * </p> 40 * <p> 41 * The filter is configured with the following init parameters: 42 * <dl> 43 * <dt>attribute</dt><dd>If set, then the request attribute of this name is set with the matched user agent string</dd> 44 * <dt>cacheSize</dt><dd>The size of the user-agent cache, used to avoid reparsing of user agent strings. The entire cache is flushed 45 * when this size is reached</dd> 46 * <dt>userAgent</dt><dd>A regex {@link Pattern} to extract the essential elements of the user agent. 47 * The concatenation of matched pattern groups is used as the user agent name</dd> 48 * <dl> 49 * An example value for pattern is <code>(?:Mozilla[^\(]*\(compatible;\s*+([^;]*);.*)|(?:.*?([^\s]+/[^\s]+).*)</code>. These two 50 * pattern match the common compatibility user-agent strings and extract the real user agent, failing that, the first 51 * element of the agent string is returned. 52 * 53 * 54 */ 55 public class UserAgentFilter implements Filter 56 { 57 private static final String __defaultPattern = "(?:Mozilla[^\\(]*\\(compatible;\\s*+([^;]*);.*)|(?:.*?([^\\s]+/[^\\s]+).*)"; 58 private Pattern _pattern = Pattern.compile(__defaultPattern); 59 private Map<String, String> _agentCache = new ConcurrentHashMap<String, String>(); 60 private int _agentCacheSize=1024; 61 private String _attribute; 62 63 /* ------------------------------------------------------------ */ 64 /* (non-Javadoc) 65 * @see javax.servlet.Filter#destroy() 66 */ 67 public void destroy() 68 { 69 } 70 71 /* ------------------------------------------------------------ */ 72 /* (non-Javadoc) 73 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 74 */ 75 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 76 { 77 if (_attribute!=null && _pattern!=null) 78 { 79 String ua=getUserAgent(request); 80 request.setAttribute(_attribute,ua); 81 } 82 chain.doFilter(request,response); 83 } 84 85 /* ------------------------------------------------------------ */ 86 /* (non-Javadoc) 87 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 88 */ 89 public void init(FilterConfig filterConfig) throws ServletException 90 { 91 _attribute=filterConfig.getInitParameter("attribute"); 92 93 String p=filterConfig.getInitParameter("userAgent"); 94 if (p!=null) 95 _pattern=Pattern.compile(p); 96 97 String size=filterConfig.getInitParameter("cacheSize"); 98 if (size!=null) 99 _agentCacheSize=Integer.parseInt(size); 100 } 101 102 /* ------------------------------------------------------------ */ 103 public String getUserAgent(ServletRequest request) 104 { 105 String ua=((HttpServletRequest)request).getHeader("User-Agent"); 106 return getUserAgent(ua); 107 } 108 109 /* ------------------------------------------------------------ */ 110 /** Get UserAgent. 111 * The configured agent patterns are used to match against the passed user agent string. 112 * If any patterns match, the concatenation of pattern groups is returned as the user agent 113 * string. Match results are cached. 114 * @param ua A user agent string 115 * @return The matched pattern groups or the original user agent string 116 */ 117 public String getUserAgent(String ua) 118 { 119 if (ua == null) 120 return null; 121 122 String tag = _agentCache.get(ua); 123 124 if (tag == null) 125 { 126 if (_pattern != null) 127 { 128 Matcher matcher = _pattern.matcher(ua); 129 if (matcher.matches()) 130 { 131 if (matcher.groupCount() > 0) 132 { 133 for (int g = 1; g <= matcher.groupCount(); g++) 134 { 135 String group = matcher.group(g); 136 if (group != null) 137 tag = tag == null ? group : tag + group; 138 } 139 } 140 else 141 { 142 tag = matcher.group(); 143 } 144 } 145 } 146 147 if (tag == null) 148 tag = ua; 149 150 if (_agentCache.size() >= _agentCacheSize) 151 _agentCache.clear(); 152 _agentCache.put(ua, tag); 153 } 154 155 return tag; 156 } 157 }