Trac over XML-RPC with JAVA

For our work on the C++ Refactoring in Eclipse CDT we created some helper classes. We thought it would be nice to document this helper classes in a way that it is browsable and searchable. The best would be to generate a wiki entry in our trac using the javadoc. Because then the documentation is directly in the source and not in a separate file and the advantage with the wiki should be obvious.
To extract the needed information out of the Java source code I wrote my own Doclet. This was not a big problem in contrary to editing the trac wiki.
Here are a few things you should pay attention to if you want use the Trac XML-RPC with Java:

Before starting you need to install the Trac XML-RPC Plugin. We had already done this to use Mylyn. After installing the plugin you should check if it works and make sure that you use the accurate url. You can do this by navigating to http://your.domain/xmlrpc or with authentication to http://your.domain/login/xmlrpc. You should see the “XML-RPC exported functions” site. Now you are ready to start!

I started with fetching all the needed libraries.
There are different vendors which provide libraries for XML-RPC. I use the own from apache.
To use the Trac XML-RPC Plugin the easiest way is to use the provided java library.

The next thing you should do is to create a user explicitly for this purpose and give him the needed privileges. Additionally you have to set the XML_RPC privilege. For a detailed view which privileges are needed look on your XML-RPC exported functions site.

After that let us have a look at the code. (Hint: I removed the exception handling)

XML-RPC Java code

This Code consists mainly of two parts. The first part where the connection is established over XML-RPC and the second part where the interaction with the Trac happens. I think that the first part is self explanatory. Therefore let us move on to the second part.


TrackerDynamicProxy tdp = new TrackerDynamicProxy(client);
Wiki wiki = (Wiki)tdp.newInstance(Wiki.class);

To add or change something in the wiki you need the correspondent object. This can be fetched over the TrackerDynamicProxy.


Hashtable h = new Hashtable();
wiki.putPage("MyWikiSite", "Hello World!", h);

From there it is quite easy. Just use the wiki object for all the modifications.
The Hashtable h can be used to set PageInformations which can be fetched again with wiki.getPageInfo(). But because we only need to write the page we won’t store any additional information with it.

Problems?
Following I will give you two hints which might help you.

  1. An exception will occur if you try to write the same page content into the wiki which already exists. This exception will look somewhat like:
    Caused by: org.apache.xmlrpc.XmlRpcException: 'Page not modified' while executing 'wiki.putPage()'
  2. Another really strange problem I experienced was with the authenticated access. My first approach was to put the user and password into the url (username:password@url) as common on the internet .
    conf.setServerURL(new URL("http://username:password@your.domain/login/xmlrpc"));
    But that did not work and I do not have a clue why?

    The only way it work was as shown above. By setting the user and password separately from the url.
    conf.setBasicUserName("username");
    conf.setBasicPassword("password");
    conf.setServerURL(new URL("http://your.domain/login/xmlrpc"));

5 Responses to “Trac over XML-RPC with JAVA”

  1. David Says:

    “helper classes but in a way” -> “helper classes in a way” that’s a Germanism which doesn’t quite work in English
    “generate an wiki entry” -> “generate a wiki entry”
    “You can to this” -> “You can do this” typo ;)
    “The next you should do” -> “The next thing you should do” or “The next step you should take”
    “create a user explicit for” -> “create a user explicitly for”
    “detailed view which privileges look” -> “detailed view which privileges are needed look”
    “which is already existent” -> “which is already existing” or “which already exists”

    Interesting article!

  2. Guido Says:

    Long post => lots of mistakes ;-)

    Thanks a lot!

    =>changed

  3. Bertil Says:

    I had a lot of problems with the authentication process as well (and with the xmlrpc util and the python examples) but your example worked like a charm. You saved my day, thanks!

  4. Erick Audet Says:

    Hi,

    Thanks for the post. I am trying to understand how the Hashtable works. I try to use this call:

    Vector vector = ticket.get(new Integer(5896));

    This returns a vector and at position #3 there is a “HashMap” with all attributes. My idea was to use this HashMap, change some of the values and do an update. But the update uses a “Hashtable”… Before creating a Hashtable out of the HashMap, can you tell me if the classes (TicketProperty, TicketType all other extensions) may help in creating and or updating a ticket?

    Thanks,

    Erick

  5. Guido Says:

    Hey Erick,
    I haven’t work with XML-RPC for quite a while. I looked 10min into it, but couldn’t figure it out that quickly. Maybe someone else who reads this might have enough experience.
    Otherwise, I would recommend http://stackoverflow.com/ to ask this question.

    Cheers,
    Guido

Leave a Reply