是否可以通过单个gzipstream init来实现gzip过滤器?

时间:2018-10-29 18:51:19

标签: java servlets

我有一个问题。我使用的gzip过滤器可以将我的页面编码为gzip格式。为此,我使用GZIPResponseStream和GZIPResponseWrapper。使用来自Internet的示例,我在getOutputStream ()中创建了两个init,并在getWriter ()中用代码标记了它。我有一个问题,是否可以在类构造函数中创建一次initGzip()? 我尝试过,但在这种情况下,并非所有页面都能正常工作。为什么我听不懂? 可以使用一个initGzip()来实现这一点吗?

MyWrapper:

public class GZIPFilterResponseWrapper extends HttpServletResponseWrapper implements Closeable {

    private PrintWriter printWriter;
    private GZIPFilterResponseStream gzipStream;
    private ServletOutputStream outputStream;

    public GZIPFilterResponseWrapper(HttpServletResponse response) throws IOException {
        super(response);
        response.addHeader(CONTENT_ENCODING, GZIP);
    }

    @Override
    public void flushBuffer() throws IOException {
        if (nonNull(printWriter)) {
            printWriter.flush();
        }
        if (nonNull(outputStream)) {
            outputStream.flush();
        }
        super.flushBuffer();
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (nonNull(printWriter)) {
            throw new IllegalStateException(GZIP_CANNOT_WRITE);
        }
        if (isNull(outputStream)) {
            initGzip(); // here
            outputStream = gzipStream;
        }
        return outputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (nonNull(outputStream)) {
            throw new IllegalStateException(GZIP_WRITER_ALREADY_HAS_CALLING);
        }
        if (isNull(printWriter)) {
            initGzip(); // here 
            printWriter = new PrintWriter(new OutputStreamWriter(gzipStream, getResponse().getCharacterEncoding()));
        }
        return printWriter;
    }

    private void initGzip() throws IOException {
        gzipStream = new GZIPFilterResponseStream(getResponse().getOutputStream());
    }

    @Override
    public void close() throws IOException {
        if (nonNull(printWriter)) {
            printWriter.close();
        }
        if (nonNull(outputStream)) {
            try {
                outputStream.close();
            } catch (IOException e) {
                throw new IOException(e.getMessage());
            }
        }
    }
}

MyStream:

public class GZIPFilterResponseStream extends ServletOutputStream {

    private OutputStream output;
    private GZIPOutputStream gzipStream;

    public GZIPFilterResponseStream(OutputStream output) throws IOException {
        this.output = output;
        gzipStream = new GZIPOutputStream(output);
    }

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

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

    @Override
    public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }

    @Override
    public void write(byte b[], int off, int len) throws IOException {
        gzipStream.write(b, off, len);
    }

    @Override
    public void write(int b) throws IOException {
        gzipStream.write(b);
    }
}

MyFilter:

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String acceptEncoding = req.getHeader(ACCEPT_ENCODING);

        if (nonNull(acceptEncoding) && acceptEncoding.indexOf(GZIP) != -1) {
            GZIPFilterResponseWrapper gzipWrapper = new GZIPFilterResponseWrapper(resp);
            filterChain.doFilter(req, gzipWrapper);
            gzipWrapper.close();
            return;
        }
        filterChain.doFilter(req, resp);
    }

0 个答案:

没有答案