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.

December 27, 2009

Snippet: Accessing SNIPPLR with Groovy and Apache XML-RPC

Filed under: Groovy Snippets, Groovy Tutorial — Tags: , , , — admin @ 9:49 am

In the previous post we used the Groovy XML-RPC library, in this snippet we will use the Apache XML-RPC library. Why ? There is no basic security implemented in the Groovy XML-RPC to access for example a

The prerequisites are still the same, just we change the library:

  • Download the Apache library from here. You should download a file from the binaries folder (apache-xmlrpc-3.1.2-bin.zip), inside you will find a lib subfolder with a couple of jars we need to add to the classpath.
    (commons-logging-1.1.jar, xmlrpc-client-3.1.2.jar, xmlrpc-server-3.1.2.jar, ws-commons-util-1.0.2.jar, xmlrpc-common-3.1.2.jar)

Snippet:


 

Remark: You can add more parameters for the server with

  • config.setEncoding(“UTF-8″)
    config.setBasicUserName(“[username]“);
    config.setBasicPassword(“[password]“);

December 26, 2009

Snippet: Accessing SNIPPLR with Groovy and XML-RPC

Filed under: Groovy Snippets, Groovy Tutorial — Tags: , — admin @ 3:08 pm

It is straight forward to access Snipplr via XML-RPC with Groovy. In this tutorial we use the Groovy Console to play with Snipplr snippets.

Requirements:

Tutorial:

  • Start the Groovy Console (usually with groovyConsole from a shell)

    Groovy Console

  • Add the XML-RPC library (Scripts| Add jar to classpath)

    Add library

  • The Snippet


     

  • Result   
    1. Forth
    2. Rails
    3. NewtonScript
    4. Diff
    5. Revolution
    6. Textpattern
    7. [..]
    8. W-Language
    9. HTML
    10. Java
    11. Objective C
    12. eZ Publish
    13. VHDL
    14. Bash
    15. C#
    16. ColdFusion
    17. You have 7 snippets and favorites
    18. [id:25390, title:Create simple XML with MarkupBuilder, updated:[timezone:-05:00 EST, datetime:Thu Dec 24 10:13:50 CET 2009], favorite:false, private:false]
    19. [id:25410, title:Create simple XML with StreamingMarkupBuilder (pretty-printed), updated:[timezone:-05:00 EST, datetime:Fri Dec 25 04:30:52 CET 2009], favorite:false, private:false]
    20. [id:24147, title:Finding the Longest Consonant Cluster, updated:[timezone:-05:00 EST, datetime:Wed Dec 02 12:15:07 CET 2009], favorite:true, private:false]
    21. [id:2090, title:Groovy Series: Regular Expressions 1/3, updated:[timezone:-05:00 EST, datetime:Fri Feb 02 11:42:05 CET 2007], favorite:true, private:false]
    22. import groovy.xml.*
    23. def createXML() {
    24. def xml = new groovy.xml.StreamingMarkupBuilder().bind(){
    25. mkp.xmlDeclaration()
    26. content() {
    27. parameter(x:0)
    28. person(id:100){
    29. firstname(&quot;John&quot;)
    30. lastname(&quot;Long&quot;)
    31. }
    32. person(id:101){
    33. firstname(&quot;Ken&quot;)
    34. lastname(&quot;Smith&quot;)
    35. }
    36. }
    37. }
    38. XmlUtil.serialize(xml)
    39. }
    40. def static main(def args) {
    41. def x = createXML()
    42. println x
    43. }
    44. xml groovy prettyprint StreamingMarkupBuilder
    45. 2009-12-25 03:37:20

    Take a look at the API documentation at snipplr.com

Powered by WordPress