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.

Powered by WordPress