View Javadoc
1 package com.atlassian.util.profiling.filters; 2 3 import com.atlassian.util.profiling.UtilTimerStack; 4 5 import javax.servlet.*; 6 import javax.servlet.http.HttpServletRequest; 7 8 /*** 9 * <p>Filter that will intercept requests & time how long it takes for them to return. It stores 10 * this information in the ProfilingTimerBean. 11 * 12 * <p>Install the filter in your web.xml file as follows: 13 * <pre> 14 * <filter> 15 * <filter-name>profiling</filter-name> 16 * <filter-class>com.atlassian.util.profiling.filters.ProfilingFilter</filter-class> 17 * <init-param> 18 * <param-name>activate.param</param-name> 19 * <param-value>profilingfilter</param-value> 20 * </init-param> 21 * <init-param> 22 * <param-name>autostart</param-name> 23 * <param-value>false</param-value> 24 * </init-param> 25 * </filter> 26 * 27 * <filter-mapping> 28 * <filter-name>profiling</filter-name> 29 * <url-pattern>/*</url-pattern> 30 * </filter-mapping> 31 * </pre> 32 * 33 * <p>With the above settings you can turn the filter on by accessing any URL with the 34 * parameter <code>profilingfilter=on</code>.eg: 35 * 36 * <pre> http://mywebsite.com/a.jsp?<b><i>profilingfilter=on</i></b></pre> 37 * 38 * <p>The above settings also sets the filter to not start automatically upon startup. This 39 * may be useful for production, but you will most likely want to set this true in 40 * development. 41 * 42 * @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a> 43 * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a> 44 */ 45 public class ProfilingFilter implements javax.servlet.Filter 46 { 47 48 49 /*** 50 * This is the parameter you pass to the init parameter & specify in the web.xml file: 51 * eg. 52 * <pre> 53 * <filter> 54 * <filter-name>profile</filter-name> 55 * <filter-class>com.atlassian.util.profiling.filters.ProfilingFilter</filter-class> 56 * <init-param> 57 * <param-name>activate.param</param-name> 58 * <param-value>filter.changestatus</param-value> 59 * </init-param> 60 * </filter> 61 * </pre> 62 */ 63 private static final String ON_OFF_INIT_PARAM = "activate.param"; 64 65 /*** 66 * This is the parameter you pass to the init parameter & specify in the web.xml file: 67 * eg. 68 * <pre> 69 * <filter> 70 * <filter-name>profile</filter-name> 71 * <filter-class>com.atlassian.util.profiling.filters.ProfilingFilter</filter-class> 72 * <init-param> 73 * <param-name>autostart</param-name> 74 * <param-value>true</param-value> 75 * </init-param> 76 * </filter> 77 * </pre> 78 * 79 */ 80 private static final String START_STOP_PARAM = "autostart"; 81 82 83 /*** 84 * Default parameter for turning the filter on and off. 85 * eg. 86 * <pre> 87 * http://mywebsite.com/a.jsp?profile.filter=on 88 * http://mywebsite.com/a.jsp?profile.filter=off 89 * </pre> 90 * Most often you will want to change this default value by passing an init 91 * parameter to the filter. This is configurable as it could be a potential 92 * security hole if users can randomly start and stop the profiling. 93 * <p> 94 * Security through obscurity - yes I know its bad! 95 */ 96 private static final String DEFAULT_ON_OFF_PARAM = "profile.filter"; 97 98 /*** 99 * This is static as there may be more than one instance of a servlet filter. 100 */ 101 private static String onOffParameter = DEFAULT_ON_OFF_PARAM; 102 103 private FilterConfig filterConfig; 104 105 106 /*** 107 * Check for parameters to turn the filter on or off. If parameters are given, change 108 * the current state of the filter. If current state is off then pass to filter chain. 109 * If current state is on - record start time, pass to filter chain, and then record 110 * total time on the return. 111 */ 112 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, javax.servlet.ServletException 113 { 114 String paramValue = request.getParameter(onOffParameter); 115 116 if (paramValue != null) 117 { 118 if ("on".equals(paramValue) || "true".equals(paramValue)) 119 { 120 turnFilterOn(); 121 } 122 //turn filter off 123 else if ("off".equals(paramValue) || "false".equals(paramValue)) 124 { 125 turnFilterOff(); 126 } 127 } 128 129 //if filter is not on - then don't do any processing. 130 if (!isFilterOn()) 131 { 132 chain.doFilter(request, response); 133 return; 134 } 135 136 String resource = null; 137 //if an include file then get the proper resource name. 138 if (request.getAttribute("javax.servlet.include.request_uri") != null) 139 resource = (String) request.getAttribute("javax.servlet.include.request_uri"); 140 else 141 resource = ((HttpServletRequest) request).getRequestURI(); 142 143 UtilTimerStack.push(resource); 144 145 try 146 { 147 // time and perform the request 148 chain.doFilter(request, response); 149 } 150 finally 151 { 152 UtilTimerStack.pop(resource); 153 } 154 155 } 156 157 public void setFilterConfig(FilterConfig filterConfig) 158 { 159 this.filterConfig = filterConfig; 160 if (filterConfig.getInitParameter(ON_OFF_INIT_PARAM) != null) 161 { 162 System.out.println("[Filter: " + filterConfig.getFilterName() + "] Using parameter [" + filterConfig.getInitParameter(ON_OFF_INIT_PARAM) + "]"); 163 onOffParameter = filterConfig.getInitParameter(ON_OFF_INIT_PARAM); 164 } 165 if ("true".equals(filterConfig.getInitParameter(START_STOP_PARAM))) 166 { 167 System.out.println("[Filter: " + filterConfig.getFilterName() + "] defaulting to on [" + START_STOP_PARAM + "=true]"); 168 turnFilterOn(); 169 } 170 else if ("false".equals(filterConfig.getInitParameter(START_STOP_PARAM))) 171 { 172 System.out.println("[Filter: " + filterConfig.getFilterName() + "] defaulting to off [" + START_STOP_PARAM + "=false]"); 173 turnFilterOff(); 174 } 175 } 176 177 public void init(FilterConfig filterConfig) throws ServletException 178 { 179 //some servlet containers set this to be null 180 if (filterConfig != null) 181 setFilterConfig(filterConfig); 182 } 183 184 public void destroy() 185 { 186 187 } 188 189 private boolean isFilterOn() 190 { 191 return UtilTimerStack.isActive(); 192 } 193 194 private void turnFilterOn() 195 { 196 System.out.println("[Filter: " + filterConfig.getFilterName() + "] Turning filter on [" + onOffParameter + "=on]"); 197 UtilTimerStack.setActive(true); 198 } 199 200 private void turnFilterOff() 201 { 202 System.out.println("[Filter: " + filterConfig.getFilterName() + "] Turning filter off [" + onOffParameter + "=off]"); 203 UtilTimerStack.setActive(false); 204 } 205 } 206

This page was automatically generated by Maven