/** http://www-6.ibm.com/jp/developerworks/java/011116/j_j-tomcat.html */ package ushi.svt; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; class ReplaceTextStream extends ServletOutputStream { private OutputStream intStream; private ByteArrayOutputStream baStream; private boolean closed = false; private String origText; private String newText; public ReplaceTextStream(OutputStream outStream, String searchText, String replaceText) { intStream = outStream; baStream = new ByteArrayOutputStream(); origText = searchText; newText = replaceText; } public void write(int i) throws java.io.IOException { baStream.write(i); } public void close() throws java.io.IOException { if (!closed) { processStream(); intStream.close(); closed = true; } } public void flush() throws java.io.IOException { if (baStream.size() != 0) { if (! closed) { processStream(); // need to synchronize the flush! baStream = new ByteArrayOutputStream(); } } } public void processStream() throws java.io.IOException { intStream.write(replaceContent(baStream.toByteArray())); intStream.flush(); } public byte [] replaceContent(byte [] inBytes) { String retVal =""; String firstPart=""; String tpString = new String(inBytes); //String srchString = (new String(inBytes)).toLowerCase(); String srchString = new String(inBytes); int endBody = srchString.indexOf(origText); if (endBody != -1) { firstPart = tpString.substring(0, endBody); retVal = firstPart + newText + tpString.substring(endBody + origText.length()); } else { retVal=tpString; } return retVal.getBytes(); } } class ReplaceTextWrapper extends HttpServletResponseWrapper { private PrintWriter tpWriter; private ReplaceTextStream tpStream; public ReplaceTextWrapper(ServletResponse inResp, String searchText, String replaceText) throws java.io.IOException { super((HttpServletResponse) inResp); tpStream = new ReplaceTextStream(inResp.getOutputStream(), searchText, replaceText); tpWriter = new PrintWriter(tpStream); } public ServletOutputStream getOutputStream() throws java.io.IOException { return tpStream; } public PrintWriter getWriter() throws java.io.IOException { return tpWriter; } } public final class ReplaceTextFilter implements Filter { private ServletContext sc = null; private FilterConfig filterConfig = null; private String searchText = "."; private String replaceText = "."; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ReplaceTextWrapper myWrappedResp = new ReplaceTextWrapper( response, searchText, replaceText); sc.log("ReplaceTextFilterが変換を行います。"); chain.doFilter(request, myWrappedResp); myWrappedResp.getOutputStream().close(); } public void destroy() { sc.log("ReplaceTextFilterが停止しようとしています。"); } public void init(FilterConfig fc) { String tpString; if (( tpString = fc.getInitParameter("search") ) != null) searchText = tpString; if (( tpString = fc.getInitParameter("replace") ) != null) replaceText = tpString; this.filterConfig = fc; this.sc = fc.getServletContext(); sc.log("ReplaceTextFilterが初期化されました。"); } } /* end. */