001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import java.io.IOException;
019import java.util.concurrent.Callable;
020
021import javax.servlet.Filter;
022import javax.servlet.FilterChain;
023import javax.servlet.FilterConfig;
024import javax.servlet.ServletException;
025import javax.servlet.ServletRequest;
026import javax.servlet.ServletResponse;
027import javax.servlet.http.HttpServletRequest;
028
029/**
030 * Simple servlet filter used to monitor servlet request processing performance.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 * @version 2.4
034 */
035public class ProcessLoggingFilter implements Filter {
036
037    /**
038     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
039     */
040    @Override
041    public void init(FilterConfig filterConfig) throws ServletException {}
042
043    /**
044     * @see javax.servlet.Filter#destroy()
045     */
046    @Override
047    public void destroy() {}
048
049    /**
050     * Wrap filter chain execution in a process trace.
051     * 
052     * @param request The servlet request.
053     * @param response The servlet response.
054     * @param filterChain The filter chain.
055     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
056     * @see ProcessLogger#follow(String, String, java.util.concurrent.Callable)
057     */
058    @Override
059    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)
060            throws IOException,
061            ServletException {
062        HttpServletRequest httpRequest = (HttpServletRequest) request;
063        String requestPath = httpRequest.getServletPath();
064        if (httpRequest.getPathInfo() != null) {
065            requestPath += httpRequest.getPathInfo();
066        }
067        
068        try {
069            ProcessLogger.follow("request", "Servlet Request " + requestPath, true, new Callable<Void>() {
070                @Override
071                public Void call() throws Exception {
072                    filterChain.doFilter(request, response);
073                    return null;
074                }
075            });
076        } catch (Exception e) {
077            if (e instanceof IOException) {
078                throw (IOException) e;
079            } else if (e instanceof ServletException) {
080                throw (ServletException) e;
081            } else if (e instanceof RuntimeException) {
082                throw (RuntimeException) e;
083            } else {
084                throw new RuntimeException("Unexpected checked exception in servlet request", e);
085            }
086        }
087    }
088
089}