JavaDude's Groovy Snippets

January 12, 2010

Accessing Trac with Groovy and the XML-RPC library

Filed under: Groovy Snippets, Groovy Tutorial — Tags: , , — admin @ 5:47 pm

With Groovy and the XML-RPC library (groovy.codehaus.org/XMLRPC) the access to Trac gets extremely easy. The older version 0.4 and 0.5 of the library could not handle basic authorization but thanks to Tim fixing the missing feature and some other xml format error, you can now access any Trac Wikipage with as little as 3 lines of Groovy code.

  1. def serverProxy = new XMLRPCServerProxy("https://user:password@sometracserver.com/login/xmlrpc")
  2. // or
  3. // def serverProxy = new XMLRPCServerProxy("http://sometracserver.com/login/xmlrpc")
  4. serverProxy.setBasicAuth("user","password")
  5. println serverProxy.wiki.getPage("TracBackup")

Remark:
At the time of this entry version 0.6 is not released yet. Check for availability at repository.codehaus.org/org/codehaus/groovy/groovy-xmlrpc/ for the new version or update your svn copy and build by yourself (see blog entry)

Update 2010-02-01: The new version was released as 0.5.1 (link)

Debugging XML-RPC calls

Filed under: Groovy Tutorial — Tags: , , — admin @ 7:59 am

The Groovy XML-RPC library 0.5 allows me to do perform basic authorization, but my Trac Server now throws a error code 500 at me (stating XML declaration not well-formed). Since I  dont know how to check the XML request the library posts, I choose to dissect the messages at the network level using Wireshark. Luckily I have a Python library that works fine, so I can compare the working version of the XML-RPC request. (Remark: Dont forget to start wireshark as root user/sudo)

XML Error   
  1. 2010-01-07 09:03:03,095 Trac[main] DEBUG: Dispatching <Request "POST u'/login/xmlrpc'">
  2. 2010-01-07 09:03:03,117 Trac[main] ERROR: Internal Server Error:
  3. Traceback (most recent call last):
  4. File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/trac/web/main.py", line 441, in _dispatch_request
  5. dispatcher.dispatch(req)
  6. File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/trac/web/main.py", line 205, in dispatch
  7. resp = chosen_handler.process_request(req)
  8. File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/TracXMLRPC-1.0.6-py2.5.egg/tracrpc/web_ui.py", line 163, in process_request
  9. self.process_xml_request(req, content_type)
  10. File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/TracXMLRPC-1.0.6-py2.5.egg/tracrpc/web_ui.py", line 167, in process_xml_request
  11. args, method = xmlrpclib.loads(req.read(int(req.get_header('Content-Length'))))
  12. File "/usr/local/Python-2.5.2/lib/python2.5/xmlrpclib.py", line 1130, in loads
  13. p.feed(data)
  14. File "/usr/local/Python-2.5.2/lib/python2.5/xmlrpclib.py", line 547, in feed
  15. self._parser.Parse(data, 0)
  16. ExpatError: XML declaration not well-formed: line 1, column 30
  17. 2010-01-07 09:03:03,121 Trac[chrome] DEBUG: Prepare chrome data for request

Python Library/Call

XML-RPC   
  1. import xmlrpclib
  2. server = xmlrpclib.ServerProxy("http://user:password@sometracserver.com/login/xmlrpc")
  3. print server.wiki.getPage("WikiStart")

Result in Wireshark

Wireshark

Groovy Call

XML-RPC   
  1. def serverProxy = new XMLRPCServerProxy("http://sometracserver.com/project/login/xmlrpc")
  2. serverProxy.setBasicAuth("user","password")
  3. serverProxy.wiki.getPage("WikiStart")

Result in Wireshark

Wireshark

There is the problem: encoding=”+ENCODING+”
The Trac XML reader most likely will throw up the complete call because of this. Actually the comparison was not really required because the error is very obvious.

January 6, 2010

Groovy XML-RPC library 0.5

Filed under: Groovy News — Tags: , — admin @ 4:52 pm

Update: Thanks to Guillaume’s persistence the version 0.5 was released officially today. Have a look at http://repository.codehaus.org/org/codehaus/groovy/groovy-xmlrpc/ to find the latest release of the library. Thanks.

I will continue now with my main project, to create a Netbeans plugin for Trac.

January 3, 2010

Building Groovy XML-RPC library 0.5

Filed under: Groovy Tutorial — admin @ 8:26 pm

The current release is 0.4 (groovy-xmlrpc-0.4.jar). The actual codebase also supports basic authorization (link), but this version 0.5 was not released yet due to the failing junit test.

In this tutorial we will build the library from the codebase. Using Netbeans we have all the tool we need to achieve this easily.

Tutorial:

  1. Start Netbeans. Team | Subversion | Checkout..
    Enter http://svn.codehaus.org/groovy/trunk/groovy/modules/xmlrpc/ and a local directory

    SVN

    SVN

    1. ==[IDE]== Jan 2, 2010 8:55:32 PM Checking out...
    2. co -r HEAD http://svn.codehaus.org/groovy/trunk/groovy/modules/xmlrpc /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc --config-dir /home/sven/.netbeans/6.8/config/svn/config --non-interactive
    3. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository
    4. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware
    5. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx
    6. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1
    7. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1/smackx-3.0.1.jar.md5
    8. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1/smackx-3.0.1.jar
    9. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack
    10. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1
    11. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1/smack-3.0.1.jar
    12. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1/smack-3.0.1.jar.md5
    13. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/.classpath
    14. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/.project
    15. A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/src
    16. ...
  2. Build the library (right click on the project and Build)
    Build   
    1. NetBeans: Executing 'mvn -Dnetbeans.execution=true install'
    2. NetBeans: JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.16
    3. Scanning for projects...
    4. Building Groovy XML-RPC
    5. task-segment: [install]...
    6. Installing /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/target/groovy-xmlrpc-0.5.0-SNAPSHOT.jar to /home/sven/.m2/repository/org/codehaus/groovy/groovy-xmlrpc/0.5.0-SNAPSHOT/groovy-xmlrpc-0.5.0-SNAPSHOT.jar
    7. BUILD SUCCESSFULTotal time: 9 seconds
    8. Finished at: Sat Jan 02 21:34:59 CET 2010Final Memory: 25M/137M
  3. Download missing dependencies
    Eventually some libraries are missing, Netbeans will remind you. (Just press download)

    Dependency Problems

In part 2 we will use the library to access TRAC via XML-RPC.

    Happy New Year 2010

    Filed under: General — admin @ 10:38 am

    I wish all my readers an exciting year 2010. I am looking forward to lots of your interesting blogs, screencasts and news from the Java, EE6 and Groovy & Grails ecosystem.

    Powered by WordPress