본문 바로가기
JSP

[ JSP ] Servlet Filter request , response dump

by 정윤재 2014. 8. 6.

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;

}

}



3. response wrapper  를 작성한다.

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;

}

}

}


4. filter class 를 작성 한다.

package com.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class DumpFilter implements Filter{


    public void destroy() {



    }



    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
   
    try{
    LogRequestWrapper logRequestWrapper = new LogRequestWrapper((HttpServletRequest)request);
   
    System.out.println("=========request===========");
   
    System.out.println(logRequestWrapper.getBody());
   
   
   
    System.out.println("=========response===========");
   
    LogResponseWrapper logResponseWrapper = new LogResponseWrapper((HttpServletResponse)response);
   
    chain.doFilter(logRequestWrapper, logResponseWrapper);
   
    System.out.println(logResponseWrapper.getBody());
   
    }catch(Exception e){
    e.printStackTrace();
    }
   
   
   
  
    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {



    }
    

5. web.xml 에 filter 를 설정 한다.

  <!-- 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>




댓글