wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet Filter - Reading Request Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Filter - Reading Request Error" Watch "Servlet Filter - Reading Request Error" New topic
Author

Servlet Filter - Reading Request Error

PJ Crump
Ranch Hand

Joined: Feb 06, 2002
Posts: 51
I have a servlet filter that sits in front of a webservice servlet (AXIS) - what I want the filter to do is to look at the content of the ServletRequest and if a particular string is located in the request, I want to call another service. Everything works just fine except (isin't there always an execept) that when I execute the following code in the filter:

BufferedReader inReader = request.getReader();
String line = null;
StringBuffer sbuf = new StringBuffer();
// Read the current request into a buffer

while((line = inReader.readLine()) != null) {
sbuf.append(line);
sbuf.append("\n\r");
}

if (sbuf.toString().indexOf("mystring") > -1) {
// I do some code
} else {
chain.doFilter(request, wrapper);
}


When I execute this code, at the chain.doFilter I get an "java.lang.IllegalStateException: getReader() has already been called for this request"

I know that this is because I obtained the Reader from the request. But my question is - How can I look at the request string so that I can evaluate it and not have this exception thrown..

Thx in advance...
[ October 05, 2005: Message edited by: PJ Crump ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I know that you can only call the getReader or the getInputStream (not both) from ServletRequest. This makes me think that you can only call either of them once, which the container will do if and when you call any of the methods that require parsing the request. Since you've already called it, it blows up when the container tries.

For this reason, I think you'll need to wrap the request with an instance of HttpServletRequestWrapper and override the getReader method.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequestWrapper.html

I've done something similar but with the response.
Your welcome to look at it:
http://simple.souther.us/capture.war

In this sample app, I wrap the response and overrode the methods needed to get the output stream.
This example captures all the output and prints it to stdout.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
PJ Crump
Ranch Hand

Joined: Feb 06, 2002
Posts: 51
Since my initial post I attempted to wrap the request and make a copy of the stream so that I don't hit the error.. It works the first time getReader is called, however when it's passed into the doFilter method and the request finally gets to AXIS we have a problem. AXIS calls the getInputStream method and perhaps I screwed something up.. Here is the code..


public class GenericRequestWrapper extends HttpServletRequestWrapper {

HttpServletRequest origRequest;
byte[] reqBytes;
boolean firstTime = true;

/**
* @param arg0
*/
public GenericRequestWrapper(HttpServletRequest arg0) {
super(arg0);
origRequest = arg0;
// TODO Auto-generated constructor stub
}

public BufferedReader getReader() throws IOException {

if (firstTime) {
firstTime = false;
StringBuffer sbuf = new StringBuffer();
BufferedReader oreader = origRequest.getReader();
String line;
while((line = oreader.readLine()) != null) {
sbuf.append(line);
sbuf.append("\n\r");
}
reqBytes = sbuf.toString().getBytes();
}

InputStreamReader dave = new InputStreamReader(new ByteArrayInputStream(reqBytes));
BufferedReader br = new BufferedReader(dave);
return br;

}


public ServletInputStream getInputStream() throws IOException {

if (firstTime) {
firstTime = false;
StringBuffer sbuf = new StringBuffer();
BufferedReader oreader = origRequest.getReader();
String line;
while((line = oreader.readLine()) != null) {
sbuf.append(line);
sbuf.append("\n\r");
}
reqBytes = sbuf.toString().getBytes();
}

InputStreamReader dave = new InputStreamReader(new ByteArrayInputStream(reqBytes));

return an input stream..
}
}


*** Perhaps there is a problem w/ the input stream that I'm creating under the cover??
Brad Medinger
Greenhorn

Joined: Apr 14, 2011
Posts: 1
I realize this is a very old topic but it still shows up on Google and was very helpful in getting me pointed in the right direction.

I had the same problem of wanting to see the XML contents of the request in a Servlet Filter before the request went to Axis2. I started with the code sample in PJ Crump's last post but modified the getInputStream() method:


Hopefully this can help ease the next person's headache.
Henk Bekkering
Greenhorn

Joined: Nov 07, 2011
Posts: 1

I had some troubles with uploading files, so I made some changed to the code:

 
Don't get me started about those stupid light bulbs.
 
subject: Servlet Filter - Reading Request Error
 
Similar Threads
Fudging Filter Functionality as a method call
Filters and Servlets
System time problem
Doubt on Filters
print the request body