001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.monitoring.web.servlet;
018    
019    import org.apache.commons.monitoring.Role;
020    import org.apache.commons.monitoring.counters.Counter;
021    import org.apache.commons.monitoring.repositories.Repository;
022    import org.apache.commons.monitoring.stopwatches.StopWatch;
023    
024    import javax.servlet.Filter;
025    import javax.servlet.FilterChain;
026    import javax.servlet.FilterConfig;
027    import javax.servlet.ServletException;
028    import javax.servlet.ServletRequest;
029    import javax.servlet.ServletResponse;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    import java.io.IOException;
033    
034    public class MonitoringFilter implements Filter {
035        @Override
036        public void init(final FilterConfig filterConfig) throws ServletException {
037            // no-op
038        }
039    
040        @Override
041        public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
042            if (HttpServletRequest.class.isInstance(request)) {
043                final HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
044                final HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);
045                doFilter(httpRequest, httpResponse, chain);
046            } else {
047                // Not an HTTP request...
048                chain.doFilter(request, response);
049            }
050        }
051    
052        @Override
053        public void destroy() {
054            // no-op
055        }
056    
057        protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
058            final String uri = getRequestedUri(request);
059            final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getCounter(new Counter.Key(Role.WEB, uri)));
060            try {
061                chain.doFilter(request, response);
062            } finally {
063                stopWatch.stop();
064            }
065        }
066    
067        protected String getRequestedUri(final HttpServletRequest request) {
068            final String uri = request.getRequestURI();
069            final String context = request.getContextPath();
070            return uri.substring(context.length());
071        }
072    }