package org.qfox.jestful.server;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Method;

/**
 * <p>
 * Description:
 * </p>
 * <p>
 * <p>
 * Company: 广州市俏狐信息科技有限公司
 * </p>
 *
 * @author Payne 646742615@qq.com
 * @date 2016年6月1日 下午10:03:56
 * @since 1.0.0
 */
public class JestfulHeadResponse extends HttpServletResponseWrapper {
    private ServletOutputStream servletOutputStream;
    private PrintWriter printWriter;

    public JestfulHeadResponse(HttpServletResponse response) {
        super(response);
    }

    public void finish() throws Exception {
        Method method = servletOutputStream.getClass().getMethod("getContentLength");
        method.setAccessible(true);
        Integer contentLength = (Integer) method.invoke(servletOutputStream);
        this.setContentLength(contentLength);
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (servletOutputStream == null) {
            try {
                servletOutputStream = (ServletOutputStream) Class.forName("javax.servlet.http.NoBodyOutputStream").newInstance();
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
        return servletOutputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (printWriter == null) {
            String charset = getCharacterEncoding();
            ServletOutputStream sos = getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(sos, charset);
            printWriter = new PrintWriter(osw, true);
        }
        return printWriter;
    }

}

@@ -757,32 +757,114 @@
 ows Exception %7B%0A
+        Integer contentLength = 0;%0A        if (servletOutputStream != null) %7B%0A    
         Method m
@@ -941,16 +941,20 @@
         
+    
 method.s
@@ -973,39 +973,35 @@
 (true);%0A        
-Integer
+   
  contentLength =
@@ -1047,16 +1047,26 @@
 tream);%0A
+        %7D%0A
         

Make Framework Supports Servlet-API 3.0.1 or Later And JDK 1.6 or Later