1. 程式人生 > >Filter攔截器

Filter攔截器

servle oid tps sta port 應用 mob err app

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攔截器