GZIP Http Servlet Response

來源:互聯網
上載者:User

好久沒貼過代碼了,心血來潮。

/* * Copyright (c) 2009, 2010, B3log Team * * 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.b3log.latke.servlet.filter;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.logging.Level;import java.util.logging.Logger;import java.util.zip.GZIPOutputStream;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;/** * Abstract HTTP response GZIP filter. * * @author Liang Ding * @version 1.0.0.1, Dec 16, 2010 */public abstract class AbstractGZIPFilter implements Filter {    /**     * Logger.     */    private static final Logger LOGGER =            Logger.getLogger(AbstractGZIPFilter.class.getName());    @Override    public void init(final FilterConfig cfg) throws ServletException {    }    /**     * Wraps the http servlet response with GZIP if could.     *     * @param request the specified request     * @param response the specified response     * @param chain filter chain     * @throws IOException io exception     * @throws ServletException servlet exception     */    @Override    public void doFilter(final ServletRequest request,                         final ServletResponse response,                         final FilterChain chain) throws IOException,                                                         ServletException {        final HttpServletRequest httpServletRequest =                (HttpServletRequest) request;        final String requestURI = httpServletRequest.getRequestURI();        if (shouldSkip(requestURI)) {            LOGGER.log(Level.FINEST, "Skip GZIP filter request[URI={0}]",                       requestURI);            chain.doFilter(request, response);            return;        }        final String acceptEncoding =                httpServletRequest.getHeader("Accept-Encoding");        boolean supportGZIP = false;        if (null != acceptEncoding            && 0      *   Note: This method SHOULD be invoked for all filters with pattern     *   "/*".     *      *     * @param requestURI the specified request URI     * @return {@code true} if should be skipped, {@code false} otherwise     */    public abstract boolean shouldSkip(final String requestURI);    @Override    public void destroy() {    }    /**     * HTTP response wrapper for GZIP.     *     * @author Liang Ding     * @version 1.0.0.0, Dec 16, 2010     */    private class GZIPServletResponseWrapper extends HttpServletResponseWrapper {        /**         * GZIP output stream.         */        private GZIPOutputStream gzipStream;        /**         * Servlet output stream.         */        private ServletOutputStream servletOutputStream;        /**         * Print writer.         */        private PrintWriter printWriter;        /**         * Constructs an {@link GZIPServletResponseWrapper} object with the         * specified http servlet response.         *         * @param httpServletResponse the specified http servlet response         * @throws IOException io exception         */        GZIPServletResponseWrapper(final HttpServletResponse httpServletResponse)                throws IOException {            super(httpServletResponse);        }        @Override        public ServletOutputStream getOutputStream() throws IOException {            if (null == servletOutputStream) {                servletOutputStream = createOutputStream();            }            return servletOutputStream;        }        @Override        public PrintWriter getWriter() throws IOException {            if (null == printWriter) {                printWriter = new PrintWriter(                        new OutputStreamWriter(getOutputStream(),                                               getCharacterEncoding()));            }            return printWriter;        }        /**         * Creates output stream with GZIP delegation.         *         * @return servlet output stream         * @throws IOException io exception         */        private ServletOutputStream createOutputStream() throws IOException {            final ServletResponse servletResponse = this.getResponse();            gzipStream = new GZIPOutputStream(servletResponse.getOutputStream());            return new ServletOutputStream() {                @Override                public void write(final int b) throws IOException {                    gzipStream.write(b);                }                @Override                public void flush() throws IOException {                    gzipStream.flush();                }                @Override                public void close() throws IOException {                    gzipStream.close();                }                /*                 * These two are not absolutely needed. They are here simply                 * because they were overriden by GZIPOutputStream.                 */                @Override                public void write(final byte[] b) throws IOException {                    gzipStream.write(b);                }                @Override                public void write(final byte[] b, final int off, final int len)                        throws IOException {                    gzipStream.write(b, off, len);                }            };        }    }}
本文是使用 B3log Solo 從 簡約設計の藝術 進行同步發布的原文地址:http://88250.b3log.org/articles/2010/12/16/1292515963081.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.