首页 > 其他 > 详细

Filter拦截器

时间:2017-09-15 22:23:17      阅读:332      评论:0      收藏:0      [点我收藏+]

Filter执行顺序: 

技术分享

 

GZipFilter应用:

web.xml配置

<filter>
        <filter-name>GzipFilter</filter-name>
        <filter-class>com.citi.icg.portal.compositepageservice.server.filter.GzipFilter</filter-class>
        <init-param>
            <param-name>mimeTypes</param-name>
            <param-value>application/json</param-value>
        </init-param>
    </filter>
    <filter-mapping>
            <filter-name>GzipFilter</filter-name>
            <url-pattern>/mobile/mobile/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
            <filter-name>GzipFilter</filter-name>
            <url-pattern>/page/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
            <filter-name>GzipFilter</filter-name>
            <url-pattern>/portlet/*</url-pattern>
    </filter-mapping>
GzipFilter.java
package com.citi.icg.portal.compositepageservice.server.filter;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Enumeration;
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;

/**
 * Gzip Filter.
 */
public class GzipFilter implements Filter
{
    public void init(FilterConfig cfg) throws ServletException
    {
    }

    public void destroy()
    {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException
    {

        boolean supportsGzip = false;
        @SuppressWarnings("unchecked")
        Enumeration<String> respHeaders = ((HttpServletRequest) req).getHeaders("Accept-Encoding");
        while (respHeaders.hasMoreElements())
        {
            String name = respHeaders.nextElement();
            if (name.matches("(?i).*gzip.*"))
            {
                supportsGzip = true;
                break;
            }
        }

        if (supportsGzip)
        {
            HttpServletResponse httpResp = (HttpServletResponse) resp;
            HttpServletResponseWrapper wrapper = new GzipResponseWrapper(httpResp);
            chain.doFilter(req, wrapper);
            if (!resp.isCommitted())
            {
                wrapper.getOutputStream().close();
            }
        }
        else
        {
            chain.doFilter(req, resp);
        }
    }

    /**
     * Gzip Response Wrapper.
     */
    static class GzipResponseWrapper extends HttpServletResponseWrapper
    {
        private GZIPOutputStream gzipStream;
        private ServletOutputStream servletOutputStream;
        private PrintWriter printWriter;

        GzipResponseWrapper(HttpServletResponse resp) throws IOException
        {
            super(resp);
        }

        @Override
        public ServletOutputStream getOutputStream() throws IOException
        {
            if (servletOutputStream == null)
            {
                servletOutputStream = createOutputStream();
            }
            return servletOutputStream;
        }

        @Override
        public PrintWriter getWriter() throws IOException
        {
            if (printWriter == null)
            {
                printWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), getCharacterEncoding()))
                {
                    public void write(char[] cb, int off, int len)
                    {
                        super.write(cb, off, len);
                        super.flush();
                    }
                };
            }
            return printWriter;
        }

        private ServletOutputStream createOutputStream() throws IOException
        {
            ServletResponse resp = this.getResponse();
            gzipStream = new GZIPOutputStream(resp.getOutputStream());
            addHeader("Content-Encoding", "gzip");
            return new ServletOutputStream()
            {
                @Override
                public void write(int b) throws IOException
                {
                    gzipStream.write(b);
                }
                @Override
                public void write(byte[] b) throws IOException
                {
                    gzipStream.write(b);
                }
                @Override
                public void write(byte[] b, int off, int len) throws IOException
                {
                    gzipStream.write(b, off, len);
                }

                @Override
                public void flush() throws IOException
                {
                    gzipStream.flush();
                }

                @Override
                public void close() throws IOException
                {
                    gzipStream.close();
                }
            };
        }
    }
}

 

Filter拦截器

原文:http://www.cnblogs.com/IvySue/p/7528691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!