C:\Documents and Settings\ran488\My Documents\NetBeansProjects\ScriptedFilter\src\java\com\redneck\scriptedfilter\servlet\BeanShellServletFilter.java |
package com.redneck.scriptedfilter.servlet;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import bsh.Interpreter;
public class BeanShellServletFilter implements javax.servlet.Filter {
private javax.servlet.FilterConfig filterConfig;
private Interpreter bshEngine;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
filterConfig.getServletContext().log("BeanShellServletFilter:: doFilter()");
String preChainBshScript = "BSHServletFilter_PRE.bsh";
String postChainBshScript = "BSHServletFilter_POST.bsh";
try {
bshEngine.set("request", request);
bshEngine.source(preChainBshScript);
request = (ServletRequest) bshEngine.get("request");
} catch (bsh.EvalError e) {
filterConfig.getServletContext().log(preChainBshScript + " BSH script failed to execute properly: " + e.toString());
e.printStackTrace();
} catch (Throwable t) {
filterConfig.getServletContext().log("BeanShellServletFilter failed to execute properly, passing through: " + t.toString());
t.printStackTrace();
}
chain.doFilter(request, response);
try {
bshEngine.set("response", response);
bshEngine.source(postChainBshScript);
response = (ServletResponse) bshEngine.get("response");
} catch (bsh.EvalError e) {
filterConfig.getServletContext().log(postChainBshScript + " BSH script failed to execute properly: " + e.toString());
e.printStackTrace();
} catch (Throwable t) {
filterConfig.getServletContext().log("BeanShellServletFilter failed to execute properly, passing through: " + t.toString());
t.printStackTrace();
}
}
public void init(FilterConfig cfg) throws ServletException {
this.filterConfig = cfg;
filterConfig.getServletContext().log("BeanShellServletFilter:: init(): Creating BSH Interpreter.");
bshEngine = new Interpreter();
filterConfig.getServletContext().log("BeanShellServletFilter:: init(): Created BSH Interpreter.");
}
public void destroy() {
bshEngine = null;
}
}