View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.web.servlet;
18  
19  import org.apache.commons.monitoring.Role;
20  import org.apache.commons.monitoring.counters.Counter;
21  import org.apache.commons.monitoring.repositories.Repository;
22  import org.apache.commons.monitoring.stopwatches.StopWatch;
23  
24  import javax.servlet.Filter;
25  import javax.servlet.FilterChain;
26  import javax.servlet.FilterConfig;
27  import javax.servlet.ServletException;
28  import javax.servlet.ServletRequest;
29  import javax.servlet.ServletResponse;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.io.IOException;
33  
34  public class MonitoringFilter implements Filter {
35      @Override
36      public void init(final FilterConfig filterConfig) throws ServletException {
37          // no-op
38      }
39  
40      @Override
41      public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
42          if (HttpServletRequest.class.isInstance(request)) {
43              final HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
44              final HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);
45              doFilter(httpRequest, httpResponse, chain);
46          } else {
47              // Not an HTTP request...
48              chain.doFilter(request, response);
49          }
50      }
51  
52      @Override
53      public void destroy() {
54          // no-op
55      }
56  
57      protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
58          final String uri = getRequestedUri(request);
59          final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, uri)));
60          try {
61              chain.doFilter(request, response);
62          } finally {
63              stopWatch.stop();
64          }
65      }
66  
67      protected String getRequestedUri(final HttpServletRequest request) {
68          final String uri = request.getRequestURI();
69          final String context = request.getContextPath();
70          return uri.substring(context.length());
71      }
72  }