aspose file tools*
The moose likes Servlets and the fly likes Response filter - doFilter() never returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Response filter - doFilter() never returns" Watch "Response filter - doFilter() never returns" New topic
Author

Response filter - doFilter() never returns

J. Cartwright
Greenhorn

Joined: Sep 27, 2004
Posts: 16
Hello All,

I'm trying to construct a servlet filter that will have access to the complete response content before sending back to the client. To this end I've created the Filter and ResponseWrapper classes listed below. However, something's still not right.

The request is passed by the filter to the destination servlet, but I don't see any evidence that the filter ever regains control after the doFilter() call.

Can anyone point out what I'm doing wrong?

Thanks so much!

-- john

===========================================================================
/*
* AXLtoJavaScriptFilter.java
*/
package gov.noaa.gdsg.arcims;


import javax.servlet.*;
import gov.noaa.gdsg.CharResponseWrapper;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.*;

public class AXLtoJavaScriptFilter implements Filter {
private ServletContext ctx;
private FilterConfig cfg;
private final Logger log = Logger.getLogger(AXLtoJavaScriptFilter.class);

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;

log.debug("Request received from "+request.getRemoteHost());
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String)en.nextElement();
log.debug(paramName+" => "+request.getParameter(paramName));
}

PrintWriter out = response.getWriter();
CharResponseWrapper responseWrapper = new CharResponseWrapper((HttpServletResponse)response);
filterChain.doFilter(request, responseWrapper);
/*
* ERROR the following statement never shows in log and
* no exceptions seem to be thrown
*/
log.debug("responseLength: "+responseWrapper.toString());

StringReader sr = new StringReader(responseWrapper.toString());
StringBuffer result = new StringBuffer();
int c;
try {
while ((c = sr.read()) != -1) {
result.append(c);
}

//@TODO transform servlet response response.setContentLength(result.toString().length());
log.debug(result.toString());
out.write(result.toString());
} catch(Exception e) {
log.fatal("Error in filter",e);
out.println(e.toString());
out.write(responseWrapper.toString());
}

}

public void destroy() {
ctx = null;
cfg = null;
}

public void init(FilterConfig cfg) throws ServletException {
this.cfg = cfg;
ctx = cfg.getServletContext();
}

}

===========================================================================
/*
* CharResponseWrapper.java
*/
package gov.noaa.gdsg;

import java.io.*;
import javax.servlet.http.*;

public class CharResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;

public String toString() {
return output.toString();
}


public CharResponseWrapper(HttpServletResponse response){
super(response);
output = new CharArrayWriter();
}


public PrintWriter getWriter(){
return new PrintWriter(output);
}
}
 
Consider Paul's rocket mass heater.
 
subject: Response filter - doFilter() never returns