Index: lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseCaptureFilter.java =================================================================== diff -u --- lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseCaptureFilter.java (revision 0) +++ lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseCaptureFilter.java (revision bda8157a824fd430b5fbc860a56eb28fc75f8b9a) @@ -0,0 +1,63 @@ +package org.lamsfoundation.lams.web.filter; + +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import org.lamsfoundation.lams.util.WebUtil; +import org.lamsfoundation.lams.web.filter.ResponseWrapper; +import org.apache.log4j.Logger; + +/** + * @author mtruong + * + * This ResponseCaptureFilter takes the response that is normally sent + * to the client, and outputs all the content into a file. + * All servlets which this filter is applied, must set the request + * attribute "filename" to the value of the filename in which the + * contents should be saved. For example, if I want to save the + * contents in the directory D:\portfolio\activityId23 with the filename + * main.html then the request attribute filename must be + * D:\portfolio\activityId23\main.html + * + */ +public class ResponseCaptureFilter implements Filter +{ + + private static Logger log = Logger.getLogger(ResponseCaptureFilter.class); + + public void init(FilterConfig config) throws ServletException + {} + public void destroy() + {} + + public void doFilter(ServletRequest request, + ServletResponse response, + FilterChain chain) throws IOException, ServletException + { + log.debug("Inside ResponseCaptureFilter"); + + ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse)response); + + chain.doFilter(request, responseWrapper); + + String responseBody = responseWrapper.toString(); //output of the original response that was stored in the response wrapper + String filename = (String)request.getAttribute(WebUtil.PARAM_FILENAME); + + if (filename != null && !filename.equals("")) + { + FileWriter file = new FileWriter(filename); + + // System.out.println(responseWrapper.toString()); + + file.write(responseBody); + file.close(); + } + + //output to browser + PrintWriter out = response.getWriter(); + out.write(responseBody); + + } + + +} Index: lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseWrapper.java =================================================================== diff -u --- lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseWrapper.java (revision 0) +++ lams_common/src/java/org/lamsfoundation/lams/web/filter/ResponseWrapper.java (revision bda8157a824fd430b5fbc860a56eb28fc75f8b9a) @@ -0,0 +1,66 @@ +package org.lamsfoundation.lams.web.filter; + + +import javax.servlet.http.*; +import java.io.*; + +/** A response wrapper that takes everything the client + * would normally output and saves it in one big + * character array. + *
+ * Taken from More Servlets and JavaServer Pages + * from Prentice Hall and Sun Microsystems Press, + * http://www.moreservlets.com/. + * © 2002 Marty Hall; may be freely used or adapted. + */ + +public class ResponseWrapper extends HttpServletResponseWrapper{ + + private CharArrayWriter charWriter; + + /** Initializes wrapper. + *
+ * First, this constructor calls the parent + * constructor. That call is crucial so that the response + * is stored and thus setHeader, setStatus, addCookie, + * and so forth work normally. + *
+ * Second, this constructor creates a CharArrayWriter + * that will be used to accumulate the response. + */ + + public ResponseWrapper(HttpServletResponse response) { + super(response); + charWriter = new CharArrayWriter(); + } + + /** When servlets or JSP pages ask for the Writer, + * don't give them the real one. Instead, give them + * a version that writes into the character array. + * The filter needs to send the contents of the + * array to the client (perhaps after modifying it). + */ + + public PrintWriter getWriter() { + return(new PrintWriter(charWriter)); + } + + /** Get a String representation of the entire buffer. + *
+ * Be sure not to call this method multiple times + * on the same wrapper. The API for CharArrayWriter + * does not guarantee that it "remembers" the previous + * value, so the call is likely to make a new String + * every time. + */ + + public String toString() { + return(charWriter.toString()); + } + + /** Get the underlying character array. */ + + public char[] toCharArray() { + return(charWriter.toCharArray()); + } + } \ No newline at end of file