Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Intercepting XML using Servlet Filter

843841Dec 1 2006 — edited Dec 4 2006
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 {

// Any manipulation should be done here with the read bytes (byte array b)
// set the modified bytes in the below read method.

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.

The problem is, this method 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 the rest in the 2nd invocation. (If it was invoked just once and read the entire data in a single shot then I can modify the bytes in the method itself, as I mentioned in the method body.)

Is there a way to have control over all the bytes (The entire request body) at one place, modify it and set it.

Any help in this regard is greatly appreciated.

Thanks in advance,
Sridhar.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 1 2007
Added on Dec 1 2006
9 comments
587 views