S
Steve Sobol
So I have this filter I'm playing around with. It's a simple one, replaces all
instances of my daughter's name with the word "Princess" wrapped in a couple
html tags.
It captures the output of any filters after it in a CharArray, and then does a
regex search and replace.
It works perfectly *if* there is a filter sitting after it in the chain. If
not, however, chain.doFilter results in an empty CharArray. Is there any way to
make this thing work if this filter is the only one installed in my webapp?
(Ignore the package and class names - I'm eventually going to use this filter
for something else but didn't feel like changing the names. The code obviously
doesn't have anything to do with logging in to anything. :>)
package net.justthe.csfilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFilter implements Filter {
public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
throws IOException, ServletException {
LoginRewriteWrapper ourRs = new
LoginRewriteWrapper((HttpServletResponse)rs);
chain.doFilter(rq,ourRs);
String ourOutput=ourRs.toString();
if (ourOutput.length==0) {
ourOutput=rs.
}
Pattern p=Pattern.compile("eryne");
Matcher m=p.matcher(ourOutput);
String massagedOutput=m.replaceAll("<B><FONT
COLOR=RED>Princess</FONT></B>");
rs.setContentLength(massagedOutput.length());
ServletOutputStream out = rs.getOutputStream();
out.write(massagedOutput.getBytes());
out.close();
}
public void destroy() {}
public void init(FilterConfig filterConfig) {}
}
package net.justthe.csfilter;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
class LoginRewriteWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;
public String toString() {
return output.toString();
}
public LoginRewriteWrapper(HttpServletResponse response) {
super(response);
output = new CharArrayWriter();
}
public PrintWriter getWriter() {
return new PrintWriter(output);
}
}
instances of my daughter's name with the word "Princess" wrapped in a couple
html tags.
It captures the output of any filters after it in a CharArray, and then does a
regex search and replace.
It works perfectly *if* there is a filter sitting after it in the chain. If
not, however, chain.doFilter results in an empty CharArray. Is there any way to
make this thing work if this filter is the only one installed in my webapp?
(Ignore the package and class names - I'm eventually going to use this filter
for something else but didn't feel like changing the names. The code obviously
doesn't have anything to do with logging in to anything. :>)
package net.justthe.csfilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFilter implements Filter {
public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
throws IOException, ServletException {
LoginRewriteWrapper ourRs = new
LoginRewriteWrapper((HttpServletResponse)rs);
chain.doFilter(rq,ourRs);
String ourOutput=ourRs.toString();
if (ourOutput.length==0) {
ourOutput=rs.
}
Pattern p=Pattern.compile("eryne");
Matcher m=p.matcher(ourOutput);
String massagedOutput=m.replaceAll("<B><FONT
COLOR=RED>Princess</FONT></B>");
rs.setContentLength(massagedOutput.length());
ServletOutputStream out = rs.getOutputStream();
out.write(massagedOutput.getBytes());
out.close();
}
public void destroy() {}
public void init(FilterConfig filterConfig) {}
}
package net.justthe.csfilter;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
class LoginRewriteWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;
public String toString() {
return output.toString();
}
public LoginRewriteWrapper(HttpServletResponse response) {
super(response);
output = new CharArrayWriter();
}
public PrintWriter getWriter() {
return new PrintWriter(output);
}
}