Package org.internna.iwebmvc.core.jee.gzip

Source Code of org.internna.iwebmvc.core.jee.gzip.GZIPFilter

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.core.jee.gzip;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.internna.iwebmvc.utils.StringUtils;

/**
* A servlet filter that compresses the response contents.
*
* @author Jayson Falkner [http://www.jspbook.com/]
* @author Jose Noheda
*/
public class GZIPFilter implements Filter {

    protected final String GZIPPED_RESPONSE = "gzip";
    private Set<String> excludedURLs = new HashSet<String>();

    protected boolean excludedPath(String pathInfo) {
        boolean excluded = false;
        if (pathInfo != null)
            for (String exclude : excludedURLs)
                excluded |= pathInfo.contains(exclude);
        return excluded;
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        boolean uncompressed = true;
        if (req instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) req;
            if (request.getAttribute(GZIPPED_RESPONSE) == null) {
                request.setAttribute(GZIPPED_RESPONSE, true);
                HttpServletResponse response = (HttpServletResponse) res;
                String ae = request.getHeader("accept-encoding");
                String pathInfo = request.getPathInfo();
                if ((ae != null) && (ae.indexOf("gzip") != -1) && (!GZIPResponseWrapper.class.isInstance(res))) {
                    if (!excludedPath(pathInfo)) {
                        uncompressed = false;
                        GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response);
                        chain.doFilter(req, wrappedResponse);
                        wrappedResponse.finishResponse();
                    }
                }
            }
        }
        if (uncompressed) chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {
        String exclude = filterConfig.getInitParameter("excluded-urls");
        if (StringUtils.hasText(exclude))
            for (String url : exclude.split(","))
                excludedURLs.add(url.trim());
    }

    public void destroy() {
        excludedURLs.clear();
        return;
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.jee.gzip.GZIPFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.