Blog

Ponderings of a kind

This is my own personal blog, each article is an XML document and the code powering it is hand cranked in XQuery and XSLT. It is fairly simple and has evolved only as I have needed additional functionality. I plan to Open Source the code once it is a bit more mature, however if you would like a copy in the meantime drop me a line.

Atom Feed

Authenticating with Redstone XML-RPC Proxy

Basic HTTP Authentication for Redstone XML-RPC Client

Redstone is an excellent and simple XML-RPC library. Recently when trying to create a simple XML-RPC client using Redstone in some example code for a book I am writing, I needed to be able to authenticate with the 3rd-party XML-RPC server.

The server that I was attempting to communicate with (eXist) requires at least HTTP Basic Authentication for operations that require a user to have been granted various permissions. Redstone provides no explicit functionality for authentication or information on how to achieve such things. However as its Open Source, I dug through the code and discovered that it uses a standard java.net.HttpUrlConnection. Whilst it is quite possible to do HTTP Basic Authentication with HttpUrlConnections by setting the correct HTTP Header, I was making use of Redstone's XML-RPC Proxy facility, to help keep my client code simple, which unfortunately does not expose anymore than an instance of the Interface you proxy. Another quick examination of the Redstone code showed that they were using java.lang.reflect.Proxy to create a dynamic proxy of the provided Interface.

We can use Proxy.getInvocationHandler on our XML-RPC proxy to get the Invocation Handler, which happens to be an instance of redstone.xmlrpc.XmlRpcProxy which offers us the method setRequestProperty(name, value). Any request properties set in this way are set as Headers on the Http Request used by the XML-RPC proxy.

Example code for HTTP Basic Authentication with Redstone XML-RPC Proxy Client

//create your XML-RPC Proxy
final MyInterface rpc = (MyInterface)XmlRpcProxy.createProxy(new URL("http://some-server:8080/xmlrpc"), "", new Class[] { MyInterface.class }, true );

final String username = "your-username";
final String password = "your-password";

//concatenate and base64 encode the username and password (suitable for use in HTTP Basic Authentication)
final String auth = javax.xml.bind.DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());

//set the HTTP Header for Basic Authentication
((XmlRpcProxy)Proxy.getInvocationHandler(rpc)).setRequestProperty("Authorization", "Basic " + auth);

//you can now do authenticated XML-RPC calls with the proxy
rpc.yourMethod();

                    

Adam Retter posted on Saturday, 11th May 2013 at 15.49 (GMT+01:00)
Updated: Saturday, 11th 2013 at May 15.49 (GMT+01:00)

tags: RedstoneXML-RPCJavaHTTPAuthentication

Add Comment



(will not be shown)






Tag Cloud