Filtering JSP page failed
paszti May 19, 2004 7:39 AMHi all!
I use JBoss 3.2.3.
I'd like to write a filter that removes the new lines from the result of a JSP page.
Found an excelent article concerning this topic ( http://www-106.ibm.com/developerworks/java/library/j-tomcat/?open&l=101,t=grj,p=TomcatTricks).
The filter code is really simple:
package com.ibm.devworks.filters;
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();
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 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);
chain.doFilter(request, myWrappedResp);
myWrappedResp.getOutputStream().close();
}
public void destroy() {
}
public void init(FilterConfig filterConfig) {
String tpString;
if (( tpString = filterConfig.getInitParameter("search") ) != null)
searchText = tpString;
if (( tpString = filterConfig.getInitParameter("replace") ) != null)
replaceText = tpString;
this.filterConfig = filterConfig;
}
}The filter works only when the requested page is a html. If is is renamed to
jsp extension, this filter is failed. I get a
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"></HEAD> <BODY></BODY></HTML>
content.
I suppose that filters work that first perform the jsp page and its result content is filtered.
What's wrong?
Tibor