通过Filter动态添加JS的内容

package io.purewind.web;


import org.springframework.util.ResizableByteArrayOutputStream;
import org.springframework.util.StreamUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

public class AssetFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 动态压缩
        AssertServletResponseWrapper wrappedResponse = new AssertServletResponseWrapper(response, request);
        try {
            chain.doFilter(request, wrappedResponse);
        } finally {
            wrappedResponse.finishResponse();
        }
    }

    public class AssertServletResponseWrapper extends HttpServletResponseWrapper {

        private CharArrayWriter charArrayWriter;

        private ResizableByteArrayOutputStream content;

        /**
         * Original response.
         */
        protected HttpServletResponse origResponse = null;

        /**
         * The request with which this servlet is associated.
         */
        protected HttpServletRequest origRequest;

        public AssertServletResponseWrapper(HttpServletResponse response, HttpServletRequest request) {
            super(response);
            origResponse = response;
            origRequest = request;
        }

        @Override
        public PrintWriter getWriter() throws IOException {
            charArrayWriter = new CharArrayWriter();
            return new PrintWriter(charArrayWriter);
        }

        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            content = new ResizableByteArrayOutputStream(1024);
            return new ResponseServletOutputStream(content);
        }

        public void finishResponse() {
            String append = ";window.console && console.log('添加成功');";
            try {
                if (null != content) {
                    this.content.write(append.getBytes());
                    this.origResponse.setContentLength(this.content.size());
                    StreamUtils.copy(this.content.toByteArray(), this.origResponse.getOutputStream());
                }
                if (null != charArrayWriter) {
                    PrintWriter out = this.origResponse.getWriter();
                    out.write(this.charArrayWriter.toString());
                    out.write(append);
                    out.close();
                }
            } catch (Exception e) {

            }
        }
    }

    private class ResponseServletOutputStream extends ServletOutputStream {
        private OutputStream content;

        public ResponseServletOutputStream(OutputStream content) {
            this.content = content;
        }

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

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            content.write(b, off, len);
        }
    }
}
通过Filter动态添加JS的内容
Share this