servlet filter 를 이용 해서 request 와 response 를 찍어보도록 하겠다.
1. response 를 찍기 위해 class 를 하나 더 생성 한다.
package com.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
public class ServletOutputStreamCopier extends ServletOutputStream{
private OutputStream outputStream;
private ByteArrayOutputStream copy;
public ServletOutputStreamCopier(OutputStream outputStream){
this.outputStream = outputStream;
this.copy = new ByteArrayOutputStream(1024);
}
@Override
public void write(int b) throws IOException {
outputStream.write(b);
copy.write(b);
}
public byte[] getCopy(){
return copy.toByteArray();
}
}
2. request wrapper 를 작성 한다.
package com.test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class LogRequestWrapper extends HttpServletRequestWrapper{
private final String body;
public LogRequestWrapper(HttpServletRequest request) throws IOException{
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try{
inputStream = request.getInputStream();
if(inputStream != null){
bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
char[] charBuffer = new char[128];
int bytesRead = -1;
while((bytesRead = bufferedReader.read(charBuffer)) > 0){
stringBuilder.append(charBuffer, 0, bytesRead);
}
}else{
stringBuilder.append("");
}
}catch(IOException ex){
throw ex;
}finally{
if(bufferedReader != null){
try{
bufferedReader.close();
}catch(IOException ex){
throw ex;
}
}
}
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException{
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody(){
return this.body;
}
}
package com.test;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class LogResponseWrapper extends HttpServletResponseWrapper{
private ServletOutputStream outputStream;
private PrintWriter writer;
private ServletOutputStreamCopier copier;
private HttpServletResponse res;
public LogResponseWrapper(HttpServletResponse response){
super(response);
res = response;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if(writer != null){
throw new IllegalStateException("getWriter() has already been called on this response");
}
if(outputStream == null){
outputStream = getResponse().getOutputStream();
copier = new ServletOutputStreamCopier(outputStream);
}
return copier;
}
@Override
public PrintWriter getWriter() throws IOException{
if(outputStream != null){
throw new IllegalStateException("getOutputStream() has already been called on this response");
}
if(writer == null){
copier = new ServletOutputStreamCopier(getResponse().getOutputStream());
writer = new PrintWriter(new OutputStreamWriter(copier,getResponse().getCharacterEncoding()),true);
}
return writer;
}
@Override
public void flushBuffer() throws IOException {
if(writer != null){
writer.flush();
}else if(outputStream != null){
copier.flush();
}
}
public byte[] getCopy(){
if(copier != null){
return copier.getCopy();
}else{
return new byte[0];
}
}
public String getBody(){
String responseBody = "";
try{
this.flushBuffer();
byte[] copy = this.getCopy();
responseBody = new String(copy,res.getCharacterEncoding());
}catch(IOException ie){
ie.printStackTrace();
}finally{
return responseBody;
}
}
}
<!-- filter definition -->
<filter>
<filter-name>DumpFilter</filter-name>
<filter-class>com.test.DumpFilter</filter-class>
</filter>
<!-- filter url mapping -->
<filter-mapping>
<filter-name>DumpFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
'JSP' 카테고리의 다른 글
[ JSP ] xml 출력 (0) | 2018.01.28 |
---|---|
[ JSP ] Servlet Filter modify request body (0) | 2014.12.05 |
[ JSP ] Servlet Filter 사용한 접근 제한 구현 (0) | 2013.05.21 |
[ Servlet ] Servlet Filter 사용법 (1) | 2013.05.14 |
[ JSP ] JSTL null , equal 관련 문법 (5) | 2012.11.14 |
댓글