package jp.esoro.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
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;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PostFilter implements Filter {
protected static Logger logger = LogManager.getLogger();
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
//要求
HttpServletRequest req = (HttpServletRequest) request;
BufferedReader body = new BufferedReader(req.getReader());
String ret = body.readLine();
//要求ロギング
logger.info(req.getRemoteAddr()+"->"+req.getLocalAddr()+":"+req.getLocalPort()+req.getContextPath() + ":" + req.getMethod() + " RCV:"+ret);
OutputStream out = response.getOutputStream();
GenericResponseWrapper wrapper =
new GenericResponseWrapper((HttpServletResponse) response);
//この後、サーブレット側処理に移ります
chain.doFilter(request, wrapper);
//ラップしたwrapperからサーブレットの処理結果を取得
byte[] data = wrapper.getData();
//OutputStreamに書き込んで応答
out.write(data);
out.close();
//Shift-JISでString変換
String result = new String(data, "Shift-JIS");
//応答ロギング
logger.info(req.getRemoteAddr()+"<-"+req.getLocalAddr()+ ":"+req.getLocalPort()+ req.getContextPath() + ":" + req.getMethod() + " SND:"+result);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}