com.flyfish.oauth.wrapper.AuditingResponseWrapper Maven / Gradle / Ivy
package com.flyfish.oauth.wrapper;
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;
/**
* 用于审计的响应值包装
* 这里为了不影响实现,只是使用了一份软拷贝
*
* @author wangyu
*/
public class AuditingResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public AuditingResponseWrapper(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
}
@Override
public void flushBuffer() throws IOException {
flushExternalBuffer();
super.flushBuffer();
}
public byte[] getContent() throws IOException {
flushExternalBuffer();
return buffer.toByteArray();
}
private void flushExternalBuffer() throws IOException {
if (out != null) {
out.flush();
}
}
/**
* 内置包装输出流
*/
class WrapperOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos;
private WrapperOutputStream(ByteArrayOutputStream bos) {
this.bos = bos;
}
@Override
public void write(int b) {
bos.write(b);
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}
}