Hello,

we've a web service that receives SOAP RPC-encoded xml requests on HTTP
through axis and tomcat 5.5.

We'd like to log every xml rq we receive using a Filter, because we've
to write the xml to a file whose name depends on the xml request.

The problem is that one can call SevletRequest.getReader once.

So we extended

HttpServletRequestWrapper 

with the class in attachment, to read the request body once in a string
and then to wrap the getReader method. Then we create a "clone" of the
request we receive in the filter 

Is this a good way or there's another standard way to read HTTP request
body in a Filter? 

Should we implement an axis handler?

Many many thanks in advance 

Best regards 

Bartolomeo

package com.siap.Utility;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.StringReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;



/**
 * Filtra le richieste ai WebServices per salvare l'xml di richiesta e salvarlo in un determinato
 * path con un nome specifico 
 * @author Marco Rosa - 23/giu/10
 */
public class SiapHttpServletRequestWrapper extends HttpServletRequestWrapper {
	
	String rq_xml = "";
	
	SiapHttpServletRequestWrapper( HttpServletRequest rq ){
		super(rq);
		
		// save xml
		StringBuffer strbuf = new StringBuffer("");
		try{
		    BufferedReader reader = rq.getReader();

	        //... Loop as long as there are input lines.
	        String line = null;
	        while ((line=reader.readLine()) != null) {
	            strbuf.append(line);
	            strbuf.append("\n");   // Write system dependent end of line.
	        }

	        //... Close reader and writer.
	        reader.close();  // Close to unlock.
	        
	        rq_xml = strbuf.toString();
		}catch( Exception e){
			
		}
	}

	private class SiapServletInputStream extends ServletInputStream{
		ByteArrayInputStream m_r =null;
		SiapServletInputStream( ByteArrayInputStream r ){
			m_r = r;
		}
		
		public int read(){
			return m_r.read();
		}
		
		public int 	read(byte[] b, int off, int len) {
			return m_r.read(b, off, len );
		}
	
		//public int readLine(byte[] b, int off, int len) {
		//	return m_r.readLine( b, off, len);
		//}
	}
	
	public ServletInputStream getInputStream(){
		SiapServletInputStream retVal = null;
		try{
			retVal = new SiapServletInputStream(new ByteArrayInputStream(rq_xml.getBytes("UTF-8"))); 
		}catch(Exception e ){
			
		}
		return retVal;
	}
	
	public BufferedReader getReader(){
		return new BufferedReader( new StringReader(rq_xml) );
	}

	/**
	 *
	 * @author Marco Rosa - 23/giu/10
	 */
	public String getRq_xml() {
		return rq_xml;
	}

	/**
	 *
	 * @author Marco Rosa - 23/giu/10
	 */
	public void setRq_xml(String rq_xml) {
		this.rq_xml = rq_xml;
	}

}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to