• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Filter - Reading Request Error

 
PJ Crump
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
PJ Crump
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had some troubles with uploading files, so I made some changed to the code:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic