CHAPTER 11 ADVANCED FILTERING TECHNIQUES 443 filterConfig.getServletContext().log( (Web server on xp)
CHAPTER 11 ADVANCED FILTERING TECHNIQUES 443 filterConfig.getServletContext().log( “Getting out of VisAuditFilter”); } public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; } public void destroy() {} } Code in the doFilter() method, prior to the chain.doFilter() call, is executed on the inbound request. The code retrieves the client s IP address and the host name. This information will be passed down to the custom stream. After a little logging, the class creates a new customized wrapped response, passing in the actual response as well as the IP address and host name. Then the class calls chain.doFilter(), passing the wrapped response downstream to other filters and the resource processor. Some resource processor and downstream filter combinations don t close the output stream properly, so after chain.doFilter() returns, the code forces a close on the PrintWriter. You ll see shortly that the implementation of the ReplaceContentOutputStream class is guarded against multiple closes, so this is safe even if the stream was closed previously. The rest of these methods implement the filter interface and are standard implementations that you saw within the samples of Chapter 10. The ReplaceContentOutputStream Class The custom stream, VisAuditOutStream, depends on the ReplaceContentOutputStream class (Listing 11-7) to do a lot of its magic. This class wraps an OutputStream and does the following: Supplies its own byte array based stream for the write() method, called by downstream filters and the resource processor. Handles the close() method by calling a child s replaceContent() method to transform the byte array stream. Provides the transformed content through the getResult() method. This method is called by the filter when the filter writes the response. This class can be used for any filter that transforms or replaces the response content. Listing 11-7. ReplaceContentOutputStream.java package com.apress.projsp; import java.io.*; import javax.servlet.*; public abstract class ReplaceContentOutputStream
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.