axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "sridhar vudutha" <>
Subject Intercepting SOAP Request Using ServletFilter
Date Fri, 01 Dec 2006 00:40:39 GMT
Hello All,
   I understand that this is not the right forum to post this message but
want to see if there any better solution.

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.util.*;
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) {

CustomRequestWrapper cr = new
chain.doFilter(cr, response);

// HttpServletRequestWrapper


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{
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) {
in = inputStream;

public int read(byte[] b, int off, int len) throws IOException{
final int chr =,0,b.length);
System.out.println(" total : " + chr);
return chr;

public int read(byte[] b) throws IOException {
int chr =;
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 =;
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,

View raw message