001/**
002 * Copyright 2005-2016 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.web.filter;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
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 java.io.IOException;
028
029/**
030 * This class can be used to increase the response buffer size so that if there is a late exception, Tomcat's ErrorReportValve can
031 * display it on the page and return an error status in the HTTP reponse header. This filter must be configured early in the chain
032 * in web.xml, before another filter writes anything to the response.
033 * 
034 * There's no configuration option for this already?
035 * 
036 * 
037 */
038public class SetResponseBufferSizeFilter implements Filter {
039
040    private Log log;
041    private int bufferSize;
042
043    /**
044     * Initializes this Filter with the required parameter, bufferSize.
045     * 
046     * @throws ServletException if the bufferSize parameter is missing or does not contain an integer.
047     * @see Filter#init
048     */
049    public void init(FilterConfig filterConfig) throws ServletException {
050        log = LogFactory.getLog(SetResponseBufferSizeFilter.class);
051        String bufferSizeParam = filterConfig.getInitParameter("bufferSize");
052        if (log.isDebugEnabled()) {
053            log.debug("bufferSizeParam=" + bufferSizeParam);
054        }
055        if (bufferSizeParam == null) {
056            throw new ServletException("bufferSize parameter is required");
057        }
058        try {
059            bufferSize = Integer.parseInt(bufferSizeParam);
060        }
061        catch (NumberFormatException e) {
062            throw new ServletException("bufferSize parameter is not an integer", e);
063        }
064        log.info("Filter initialized. Response buffer size is " + bufferSize);
065    }
066
067    /**
068     * Sets the bufferSize of the response.
069     * 
070     * @see Filter#doFilter
071     */
072    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
073        if (log.isDebugEnabled()) {
074            log.debug("setting response buffer size to " + bufferSize);
075        }
076        servletResponse.setBufferSize(bufferSize);
077        filterChain.doFilter(servletRequest, servletResponse);
078    }
079
080    /**
081     * Does nothing.
082     * 
083     * @see Filter#destroy
084     */
085    public void destroy() {
086        // nothing to destroy
087    }
088}