본문 바로가기
JSP

[ JSP ] Servlet Filter modify request body

by 정윤재 2014. 12. 5.

HttpServletRequest Object 는 request body 를 수정 할 수 없다.


set 으로 body 를 설정 할 수 없기 때문이다. 


그런데 일을 하다 보니 특정한 request body 가 들어올 경우 modify 해줘야


하는 경우가 생기고 말았다.


난감해 하고 있었는데 한가지 트릭이 생각 났다. 그래서 아래와 같이


Wrapper class 를 응용 해 보았는데 역시 잘 동작 된다.


ModifyFilter.java


package com.test.modify;


import java.io.IOException;


import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class ModifyFilter implements Filter{

public void doFilter(ServletRequest req, ServletResponse res, FilterChain filter) 

throws IOException, ServletException{

HttpServletRequest request = (HttpServletRequest)req;

HttpServletResponse response = (HttpServletResponse)res;

ModifyRequestWrapper requestWrapper = null;

try{

requestWrapper = new ModifyRequestWrapper(request);

}catch(Exception e){

e.printStackTrace();

}

filter.doFilter(requestWrapper, response);

}


@Override

public void destroy() {

// TODO Auto-generated method stub

}


@Override

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}



============================================


ModifyRequestWrapper.java


package com.test.modify;


import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;


import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequestWrapper;


import org.apache.commons.io.IOUtils;


public class ModifyRequestWrapper extends HttpServletRequestWrapper{

private byte[] b;

public ModifyRequestWrapper(HttpServletRequest request) throws IOException{

super(request);

InputStream is = super.getInputStream();

b = IOUtils.toByteArray(is);

String requestStringBody = new String(b);

// request body 를 변경 하는 부분   

// request body 에 해당 하는 byte[] 을 바꿔주고 getInputStream 2번이 안되는 문제를 해결   

if(requestStringBody.equals("<xml>xxxx</xml>")){

b = new String("<xml>yyyy</xml>").getBytes();

}

}

public ServletInputStream getInputStream() throws IOException {

final ByteArrayInputStream bis = new ByteArrayInputStream(b);

return new ServletInputStreamImpl(bis);

}

class ServletInputStreamImpl extends ServletInputStream{

private InputStream is;

public ServletInputStreamImpl(InputStream bis){

is = bis;

}

public int read() throws IOException {

return is.read();

}

public int read(byte[] b) throws IOException {

return is.read(b);

}

}

}



=========================================


web.xml 의  filter 설정


    <!-- filter definition -->

  <filter>

  <filter-name>ModifyFilter</filter-name>

  <filter-class>com.test.modify.ModifyFilter</filter-class>

  </filter>

  

  <!-- filter url mapping -->

  <filter-mapping>

  <filter-name>ModifyFilter</filter-name>

  <url-pattern>/*</url-pattern>

  </filter-mapping>



댓글