Skip to content

Instantly share code, notes, and snippets.

@mixaz
Last active August 8, 2022 00:00
Show Gist options
  • Select an option

  • Save mixaz/330b7e7f27d08ed3ec70a8a9f1c5d166 to your computer and use it in GitHub Desktop.

Select an option

Save mixaz/330b7e7f27d08ed3ec70a8a9f1c5d166 to your computer and use it in GitHub Desktop.

Revisions

  1. mixaz revised this gist Aug 8, 2022. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions MessageLoggingTransport.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package com.usip.backend.utils;

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
  2. mixaz created this gist Aug 7, 2022.
    91 changes: 91 additions & 0 deletions MessageLoggingTransport.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    package com.usip.backend.utils;

    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());

    private final boolean loggingEnabled;
    private final boolean filterNils;

    /**
    * Default constructor
    *
    * @see XmlRpcSunHttpTransport#XmlRpcSunHttpTransport(XmlRpcClient)
    * @param pClient
    */
    public MessageLoggingTransport(final XmlRpcClient pClient, boolean log, boolean filterNils)
    {
    super(pClient);
    this.loggingEnabled = log;
    this.filterNils = filterNils;
    }


    /**
    * 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);
    if(loggingEnabled)
    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);
    }

    String ss = sb.toString();
    if(loggingEnabled)
    log.info(ss);
    if(filterNils)
    ss = ss.replace("<nil/>","");

    final ByteArrayInputStream bais = new ByteArrayInputStream(ss.getBytes());
    return super.readResponse(pConfig, bais);
    }
    }
    18 changes: 18 additions & 0 deletions usage.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // Example of use:
    ...
    private static XmlRpcClient getXmlClient(String urlStr, String user, String password) throws MalformedURLException {
    URL url = new URL(urlStr);
    XmlRpcClient client = new XmlRpcClient();
    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    config.setEnabledForExtensions(true);
    config.setServerURL(url);
    config.setBasicUserName(user);
    config.setBasicPassword(password);
    client.setConfig(config);
    client.setTransportFactory(() ->
    new MessageLoggingTransport(
    client,
    true,
    true));
    return client;
    }