Hello All,
I'm trying to intercept a
SOAP request, modify the XML and pass it on to the AxisServlet. Gone through forums and got to a point where I could read the request content into a StringBuffer. Can someone please help me to set the read bytes back to the HttpServletRequest object.
//
Servlet Filter
import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public final class SOAPServletFilter implements Filter {
private FilterConfig config = null;
PrintWriter out;
public void init(FilterConfig filterConfig) throws ServletException {
config = filterConfig;
}
public void destroy() {
config = null; // destroy method called
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (config == null) {
return;
}
CustomRequestWrapper cr = new
CustomRequestWrapper((HttpServletRequest)request);
chain.doFilter(cr, response);
}
}
// HttpServletRequestWrapper
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class CustomRequestWrapper extends HttpServletRequestWrapper{
private CustomServerInputStream in;
private HttpServletRequest request;
public CustomRequestWrapper(HttpServletRequest request) throws IOException{
super(request);
this.request = request;
in = new CustomServerInputStream(request.getInputStream());
}
public ServletInputStream getInputStream() {
return in;
}
public BufferedReader getReader() {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
return br;
}
}
class CustomServerInputStream extends ServletInputStream {
final private ServletInputStream in;
private StringBuffer strbuf = new StringBuffer();
public CustomServerInputStream(ServletInputStream inputStream) {
super();
in = inputStream;
}
public int read(byte[] b, int off, int len) throws IOException{
final int chr = in.read(b,0,b.length);
System.out.println(" total : " + chr);
return chr;
}
public int read(byte[] b) throws IOException {
int chr = in.read(b);
strbuf.append(new
String(b));
System.out.println(" content " + new String(b));
System.out.println(" noOfBytes " + chr);
return chr;
}
public int read() throws IOException {
final int chr = in.read();
System.out.println(" char " + chr);
return chr;
}
public StringBuffer getBody() {
return strbuf;
}
}
I have overrided the 3 read() methods. Not sure why the read(byte [] b) is only invoked. Also, it is invoked more than once depending on the no of bytes in the request object. The example I'm trying has 5278 bytes, it read the first 4000 bytes and rest in the 2nd invocation. I'm putting all the read bytes into a StringBuffer.
The problem is, after all the bytes are read, how to set it back to the Request object.
Any help in this regard is greatly appreciated.
Thanks in advance,
Sridhar.