Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

XMLRPC Add comment to Wiki View in Wiki Edit Wiki page Printable Version

What is the XMLRPC module?

This is a module which allows you to create a local XML-RPC server and/or to make calls on remote XML-RPC servers.

What is XML-RPC?

XML-RPC is a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet. It uses HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible while allowing complex data structures to be transmitted, processed and returned.

Using XMLRPC

Here is an example:

The Server

It's really easy to set up a server which provides a set of remotely callable functions.

  1. Create a server object
    import groovy.net.xmlrpc.*
    import java.net.ServerSocket
    
    def server = new XMLRPCServer()
  2. Add some methods
    server.echo = {return it}  // the closure is now named "echo" and is remotely callable
  3. Start the server
    def serverSocket = new ServerSocket()      // Open a server socket on a free port
    server.startServer(serverSocket)           // Start the XML-RPC server listening on the server socket
  4. You're done!

The Client

It's pretty easy to make the remote calls too

  1. Create a proxy object to represent the remote server
    def serverProxy = new XMLRPCServerProxy("http://localhost:${serverSocket.getLocalPort()}")
  2. Call the remote method via the proxy
    println serverProxy.echo("Hello World!")
  3. That's all you need

More information

The sources can be found here : XML-RPC.

For a binary download, go to the repository.

If you are using maven to download your dependencies, you won't find all the dependencies in the Maven 2 Repo yet.

The missing dependency (smack) can be manually downloaded from here.

Sample scripts

  • Confluence Example showing how to download a secured Confluence page.
  • Another example inspired by Glen's Confluence example:
    import groovy.net.xmlrpc.*
    
    def c = new XMLRPCServerProxy("http://docs.codehaus.org/rpc/xmlrpc")
    def token = c.confluence1.login("your_username","your_password")
    // print all the code snippets from the Groovy Home page
    def page = c.confluence1.getPage(token, "Groovy", "Home")
    def incode = false
    def separator = '////////////////////////////////////'
    page.content.split('\n').each{
        if (it =~ /\{code\}/) {
            incode = !incode
            if (incode) println separator
            return
        }
        if (incode) print it
    }
    println separator

    Which results in (at least around December 2007) the following:

    ////////////////////////////////////
    def name='World'; println "Hello $name!"
    ////////////////////////////////////
    class Greet {
      def name
      Greet(who) { name = who[0].toUpperCase() +
                          who[1..-1] }
      def salute() { println "Hello $name!" }
    }
    
    g = new Greet('world')  // create object
    g.salute()              // Output "Hello World!"
    ////////////////////////////////////
    import static org.apache.commons.lang.WordUtils.*
    
    class Greeter extends Greet {
      Greeter(who) { name = capitalize(who) }
    }
    
    new Greeter('world').salute()
    ////////////////////////////////////
    groovy -e "println 'Hello ' + args[0]" World
    ////////////////////////////////////