|
|
|
|
package com.fr.plugin.cpic.filter;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
import javax.servlet.WriteListener;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.servlet.http.HttpServletResponseWrapper;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
|
|
public class ResponseWrapper extends HttpServletResponseWrapper {
|
|
|
|
|
|
|
|
|
|
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
|
|
;
|
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
private PrintWriter pwrite;
|
|
|
|
|
|
|
|
|
|
public ResponseWrapper(HttpServletResponse response) throws IOException {
|
|
|
|
|
super(response);
|
|
|
|
|
this.response = response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ServletOutputStream getOutputStream() throws IOException {
|
|
|
|
|
return new MyServletOutputStream(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重写父类的 getWriter() 方法,将响应数据缓存在 PrintWriter 中
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public PrintWriter getWriter() throws IOException {
|
|
|
|
|
pwrite = new PrintWriter(new OutputStreamWriter(bytes, StandardCharsets.UTF_8));
|
|
|
|
|
return pwrite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取缓存在 PrintWriter 中的响应数据
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public byte[] getBytes() {
|
|
|
|
|
if (null != pwrite) {
|
|
|
|
|
pwrite.close();
|
|
|
|
|
return bytes.toByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null != bytes) {
|
|
|
|
|
try {
|
|
|
|
|
bytes.flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bytes.toByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyServletOutputStream extends ServletOutputStream {
|
|
|
|
|
private ByteArrayOutputStream ostream;
|
|
|
|
|
|
|
|
|
|
public MyServletOutputStream(ByteArrayOutputStream ostream) {
|
|
|
|
|
this.ostream = ostream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
|
ostream.write(b); // 将数据写到 stream 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isReady() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setWriteListener(WriteListener writeListener) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|