Skip to content

Instantly share code, notes, and snippets.

@mixaz
Last active August 8, 2022 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mixaz/330b7e7f27d08ed3ec70a8a9f1c5d166 to your computer and use it in GitHub Desktop.
Save mixaz/330b7e7f27d08ed3ec70a8a9f1c5d166 to your computer and use it in GitHub Desktop.
Java XML-RPC transport with logging and filtering NIL values
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);
}
}
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment