mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
WebStatFilter support exclustions
This commit is contained in:
@@ -42,6 +42,8 @@ public class WebStatFilter implements Filter {
|
||||
private Set<String> excludesPattern;
|
||||
private boolean createSession = false;
|
||||
|
||||
private String contextPath;
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
|
||||
ServletException {
|
||||
@@ -49,13 +51,9 @@ public class WebStatFilter implements Filter {
|
||||
|
||||
final String requestURI = getRequestURI(httpRequest);
|
||||
|
||||
if (excludesPattern != null) {
|
||||
for (String pattern : excludesPattern) {
|
||||
if (pathMatcher.matches(pattern, requestURI)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isExclusion(requestURI)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
long startNano = System.nanoTime();
|
||||
@@ -138,7 +136,21 @@ public class WebStatFilter implements Filter {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
public boolean isExclusion(String uri) {
|
||||
public boolean isExclusion(String requestURI) {
|
||||
if (excludesPattern == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (contextPath != null && requestURI.startsWith(contextPath)) {
|
||||
requestURI = requestURI.substring(contextPath.length());
|
||||
}
|
||||
|
||||
for (String pattern : excludesPattern) {
|
||||
if (pathMatcher.matches(pattern, requestURI)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -156,7 +168,8 @@ public class WebStatFilter implements Filter {
|
||||
|
||||
StatFilterContext.getInstance().addContextListener(statFilterContextListener);
|
||||
|
||||
webAppStat = new WebAppStat();
|
||||
this.contextPath = DruidWebUtils.getContextPath(config.getServletContext());
|
||||
webAppStat = new WebAppStat(contextPath);
|
||||
|
||||
WebAppStatManager.getInstance().addWebAppStatSet(webAppStat);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class DruidWebUtils {
|
||||
@@ -18,4 +19,26 @@ public class DruidWebUtils {
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
private static String getContextPath_2_5(ServletContext context) {
|
||||
String contextPath = context.getContextPath();
|
||||
|
||||
if (contextPath == null || contextPath.length() == 0) {
|
||||
contextPath = "/";
|
||||
}
|
||||
|
||||
return contextPath;
|
||||
}
|
||||
|
||||
public static String getContextPath(ServletContext context) {
|
||||
if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return getContextPath_2_5(context);
|
||||
} catch (NoSuchMethodError error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user