logging input/output xml in apache xmlrpc client
Asked Answered
P

2

10

I'm building an xmlrpc client with Java using Apache xmlrpc, but couldn't figure out how to log the input/output xml (the raw data received and sent). How do I do this?

Thank you

Potemkin answered 13/3, 2012 at 7:11 Comment(2)
What logging framework do you use ?Silo
no framework, I just need something simple actually, to be able to read the incoming and outgoing xmlPotemkin
D
18

My work-around was to use a custom transport as follows. Perhaps there are more graceful ways of doing this.

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcStreamTransport;
import org.apache.xmlrpc.client.XmlRpcSunHttpTransport;
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
import org.xml.sax.SAXException;


/**
 * This is a custom XML-RPC transport which logs the outgoing and incoming
 * XML-RPC messages.
 */
public class MessageLoggingTransport extends XmlRpcSunHttpTransport
{
    private static final Logger log = Logger.getLogger(MessageLoggingTransport.class.getName());


    /**
     * Default constructor
     * 
     * @see XmlRpcSunHttpTransport#XmlRpcSunHttpTransport(XmlRpcClient)
     * @param pClient
     */
    public MessageLoggingTransport(final XmlRpcClient pClient)
    {
        super(pClient);
    }


    /**
     * Dumps outgoing XML-RPC requests to the log
     */
    @Override
    protected void writeRequest(final XmlRpcStreamTransport.ReqWriter pWriter) throws IOException, XmlRpcException, SAXException
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        pWriter.write(baos);
        log.info(baos.toString());
        super.writeRequest(pWriter);
    }


    /**
     * Dumps incoming XML-RPC responses to the log
     */
    @Override
    protected Object readResponse(XmlRpcStreamRequestConfig pConfig, InputStream pStream) throws XmlRpcException
    {
        final StringBuffer sb = new StringBuffer();

        try
        {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(pStream));
            String line = reader.readLine();
            while(line != null)
            {
                sb.append(line);
                line = reader.readLine();
            }
        }
        catch(final IOException e)
        {
            log.log(Level.SEVERE, "While reading server response", e);
        }

        log.info(sb.toString());

        final ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
        return super.readResponse(pConfig, bais);
    }
}

And then in the code which creates your XML-RPC client:

final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));

final XmlRpcTransportFactory transportFactory = new XmlRpcTransportFactory()
{
    public XmlRpcTransport getTransport()
    {
        return new MessageLoggingTransport(client);
    }
};

client = new XmlRpcClient();
client.setTransportFactory(transportFactory);
client.setConfig(config);
Disencumber answered 8/8, 2012 at 11:1 Comment(0)
P
0

Try using a logger for org.apache.xmlrpc.client; I bet it would spit out the xml somewhere in that package. Try something like this:

<appender name="LOG_FILE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="Threshold" value="DEBUG"/>
    <param name="File" value="C:\xmlrpc_client.log"/>
    <param name="datePattern" value="'.'yyyy-ww" />
    <param name="Append" value="true"/>
            <layout class="org.apache.log4j.PatternLayout">
               <param name="ConversionPattern" value="[%p] %t %d{DATE} %M - %m%n"/>
            </layout>
    </appender>

    <logger name="org.apache.xmlrpc.client">
    <level value="DEBUG"/>
</logger>
    <root>
       <level value="INFO" />
       <appender-ref ref="LOG_FILE"/>
    </root>
Porphyry answered 13/3, 2012 at 7:52 Comment(3)
I'm sorry, but I don't understand. What kind of xml is this? pardon the newbie-ness :DPotemkin
Hi, no worries, but when you said to 'log', I thought you were already using log4j. In this case, if you do start using log4j, very easy to setup, use the above configuration for log4j.xml and all classes under that package org.apache.xmlrpc.client would log their stuff, I bet one of the classes there, probably XmlRpcClient will log the xml it sends out and the xml it receivesPorphyry
@CarlosJaimeC.DeLeon, I tried this, but apparently nothing is logged on that logger.Amye

© 2022 - 2024 — McMap. All rights reserved.