okey, this is my code on my own jar.
this my class TomcatValve
public class TomcatValve implements ContainerListener {
private static final Logger LOG = Logger.getLogger(TomcatValve.class.getName());
private FilterDef filterDef;
private FilterMap filterMap;
public TomcatValve(Container con) {
LOG.info("Tomcat Valve Started");
try {
this.filterDef = new FilterDef();
this.filterDef.setFilterClass("com.monitor.agent.connector.servlet.MonitorFilter");
this.filterDef.setFilterName("MonitorAgent");
this.filterMap = new FilterMap();
this.filterMap.addURLPattern("/*");
this.filterMap.setFilterName("MonitorAgent");
LOG.info("Tomcat name " + con.getName());
addFilterToContainer(con);
con.addContainerListener(this);
}catch (Exception e){
LOG.info("Something err in Tomcat Valve " + e.getStackTrace().toString());
}
}
public void containerEvent(ContainerEvent containerEvent) {
try {
if ((!containerEvent.getType().equalsIgnoreCase("removeValve"))
&& (!containerEvent.getType().equalsIgnoreCase("removeChild"))) {
addFilterToContainer(containerEvent.getContainer());
}
}catch (Exception e){
LOG.info("Something error on containerEvent" + e.getMessage().toString());
}
}
private void addFilterToContainer(Container con) {
try {
StandardContext cx;
if ((con instanceof StandardContext)) {
cx = (StandardContext) con;
if (cx.findFilterDef("MonitorAgent") == null) {
cx.addFilterDef(this.filterDef);
cx.addFilterMapBefore(this.filterMap);
cx.filterStop();
cx.filterStart();
}
} else {
for (Container child : con.findChildren()) {
if (!hasMeListening(con.findContainerListeners())) {
con.addContainerListener(this);
}
addFilterToContainer(child);
}
}
}catch (Exception e){
LOG.info("Something error on addFilterToContainer " + e.getMessage().toString());
}
}
my class MonitorFilter
public class MonitorFilter implements Filter {
private static final Logger LOG = Logger.getLogger(MonitorFilter.class.getName());
private MonitorAgent agent;
private AppConfig appConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
try {
this.agent = MonitorAgent.INSTANCE;
}catch (Exception e){
LOG.info("Error INIT " +e.getMessage());
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
boolean isSecure = httpRequest.isSecure();
String method = httpRequest.getMethod();
String queryString = httpRequest.getQueryString();
String pathInfo = WebUtil.getRequestPathInfo(httpRequest);
String remoteAddr = WebUtil.getClientIpAddress(httpRequest);
String headerSeeProfile = httpRequest.getHeader("X-SeeProfile");
String reqPage = WebUtil.getCookieByName(httpRequest, "REQPAGE");
int serverPort = httpRequest.getServerPort();
RequestThread thread = RequestThread.getCurrentRequest();
if (thread == null) {
RequestInfo info = new RequestInfo(remoteAddr, httpRequest.getRemoteHost(), httpRequest.getServerName(), httpRequest.getRequestURI(), pathInfo, method, queryString, headerSeeProfile, reqPage, isSecure, serverPort);
thread = new RequestThread(info);
this.agent.addRequestToList(thread);
}
RequestWrapper requestWrapper = new RequestWrapper(thread, httpRequest);
ResponseWrapper responseWrapper = new ResponseWrapper(thread, httpResponse);
try {
chain.doFilter((ServletRequest)requestWrapper, (ServletResponse)responseWrapper);
thread.getRequestInfo().setResponseCode(responseWrapper.getStatus());
} catch (IOException e) {
LOG.log(Level.SEVERE, "IOException", e);
} catch (ServletException e) {
LOG.log(Level.SEVERE, "ServletException", (Throwable)e);
} finally {
this.agent.removeRequestFromList(thread);
}
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() { }
}
this is my class Monitor Valve
public class MonitorValve extends ValveBase implements Lifecycle, ContainerListener {
private static final Logger LOG = Logger.getLogger(MonitorValve.class.getName());
private ContainerListener valve;
private MonitorAgent agent;
public void initInternal() throws LifecycleException {
String version = ServerInfo.getServerNumber();
LOG.info("MonitorValve: Loading Tomcat Valve for Tomcat " + version);
try {
this.valve = new TomcatValve(getContainer());
super.initInternal();
} catch (Exception e) {
LOG.info("Something went wrong Loading Tomcat Valve" + e.getMessage());
}
}
public void invoke(Request request, Response response) throws IOException, ServletException {
LOG.info("Invoke Request " + request.getContext().getName());
getNext().invoke(request, response);
}
public void containerEvent(ContainerEvent containerEvent) {
LOG.info("To Container Event " + containerEvent.toString());
this.valve.containerEvent(containerEvent);
}
}
**for this case, im not using with my own jar. i test it with SeeFusion to capture log request on servlet tomcat.