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 27 import javax.servlet.Filter; 28 import javax.servlet.FilterChain; 29 import javax.servlet.FilterConfig; 30 import javax.servlet.ServletException; 31 import javax.servlet.ServletRequest; 32 import javax.servlet.ServletResponse; 33 import javax.servlet.http.HttpServletRequest; 34 35 /* ------------------------------------------------------------ */ 36 /** User Agent Filter. 37 * <p> 38 * This filter allows efficient matching of user agent strings for 39 * downstream or extended filters to use for browser specific logic. 40 * </p> 41 * <p> 42 * The filter is configured with the following init parameters: 43 * <dl> 44 * <dt>attribute</dt><dd>If set, then the request attribute of this name is set with the matched user agent string</dd> 45 * <dt>cacheSize</dt><dd>The size of the user-agent cache, used to avoid reparsing of user agent strings. The entire cache is flushed 46 * when this size is reached</dd> 47 * <dt>userAgent</dt><dd>A regex {@link Pattern} to extract the essential elements of the user agent. 48 * The concatenation of matched pattern groups is used as the user agent name</dd> 49 * <dl> 50 * An example value for pattern is <code>(?:Mozilla[^\(]*\(compatible;\s*+([^;]*);.*)|(?:.*?([^\s]+/[^\s]+).*)</code>. These two 51 * pattern match the common compatibility user-agent strings and extract the real user agent, failing that, the first 52 * element of the agent string is returned. 53 * 54 * 55 */ 56 public class UserAgentFilter implements Filter 57 { 58 private static final String __defaultPattern = "(?:Mozilla[^\\(]*\\(compatible;\\s*+([^;]*);.*)|(?:.*?([^\\s]+/[^\\s]+).*)"; 59 private Pattern _pattern = Pattern.compile(__defaultPattern); 60 private Map<String, String> _agentCache = new ConcurrentHashMap<String, String>(); 61 private int _agentCacheSize=1024; 62 private String _attribute; 63 64 /* ------------------------------------------------------------ */ 65 /* (non-Javadoc) 66 * @see javax.servlet.Filter#destroy() 67 */ 68 public void destroy() 69 { 70 } 71 72 /* ------------------------------------------------------------ */ 73 /* (non-Javadoc) 74 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 75 */ 76 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 77 { 78 if (_attribute!=null && _pattern!=null) 79 { 80 String ua=getUserAgent(request); 81 request.setAttribute(_attribute,ua); 82 } 83 chain.doFilter(request,response); 84 } 85 86 /* ------------------------------------------------------------ */ 87 /* (non-Javadoc) 88 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 89 */ 90 public void init(FilterConfig filterConfig) throws ServletException 91 { 92 _attribute=filterConfig.getInitParameter("attribute"); 93 94 String p=filterConfig.getInitParameter("userAgent"); 95 if (p!=null) 96 _pattern=Pattern.compile(p); 97 98 String size=filterConfig.getInitParameter("cacheSize"); 99 if (size!=null) 100 _agentCacheSize=Integer.parseInt(size); 101 } 102 103 /* ------------------------------------------------------------ */ 104 public String getUserAgent(ServletRequest request) 105 { 106 String ua=((HttpServletRequest)request).getHeader("User-Agent"); 107 return getUserAgent(ua); 108 } 109 110 /* ------------------------------------------------------------ */ 111 /** Get UserAgent. 112 * The configured agent patterns are used to match against the passed user agent string. 113 * If any patterns match, the concatenation of pattern groups is returned as the user agent 114 * string. Match results are cached. 115 * @param ua A user agent string 116 * @return The matched pattern groups or the original user agent string 117 */ 118 public String getUserAgent(String ua) 119 { 120 if (ua == null) 121 return null; 122 123 String tag = _agentCache.get(ua); 124 125 if (tag == null) 126 { 127 if (_pattern != null) 128 { 129 Matcher matcher = _pattern.matcher(ua); 130 if (matcher.matches()) 131 { 132 if (matcher.groupCount() > 0) 133 { 134 for (int g = 1; g <= matcher.groupCount(); g++) 135 { 136 String group = matcher.group(g); 137 if (group != null) 138 tag = tag == null ? group : tag + group; 139 } 140 } 141 else 142 { 143 tag = matcher.group(); 144 } 145 } 146 } 147 148 if (tag == null) 149 tag = ua; 150 151 if (_agentCache.size() >= _agentCacheSize) 152 _agentCache.clear(); 153 _agentCache.put(ua, tag); 154 } 155 156 return tag; 157 } 158 }