Monday, August 30, 2010

Java sample client for Bugzilla webservice API

I recently had to use bugzilla's webservice API in a Java based project. And I was surprised to see that there was almost no sample code available for using bugzilla's webservice API especially for Java. So in the end I used the XmlRpcClient provided by Apache. Here's the sample code...

private XmlRpcClient getClient(String serverURL) {
    try {
        String apiURL = serverURL + "xmlrpc.cgi";
        XmlRpcClient rpcClient;
        XmlRpcClientConfigImpl config;
 
        config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(apiURL));
        rpcClient = new XmlRpcClient();
        rpcClient.setConfig(config);
        
        return rpcClient;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return null;
}
 
public HashMap login(String serverURL, String login, String password) {
    try {
        XmlRpcClient rpcClient = getClient(serverURL);
        ArrayList<Object> params = new ArrayList<Object>();
        Hashtable<String, Object> executionData = new Hashtable<String, Object>();
        executionData.put("login", login);
        executionData.put("password", password);
        executionData.put("remember", true);
        params.add(executionData);
 
        HashMap result = (HashMap) rpcClient.execute("User.login", params);
        System.out.println(result);
        return result;
    } catch (XmlRpcException e) {
        e.printStackTrace();
    }
    return null;
}

Here are the maven dependencies required by this code…


<repository>
    <id>mvnrepository</id>
    <url>http://mvnrepository.com/artifact/</url>
</repository>
 
<dependency>
    <groupId>org.apache.xmlrpc</groupId>
    <artifactId>xmlrpc-client</artifactId>
    <version>3.1.3</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlrpc</groupId>
    <artifactId>xmlrpc-common</artifactId>
    <version>3.1.3</version>
</dependency>
<dependency>
    <groupId>ws-commons-util</groupId>
    <artifactId>ws-commons-util</artifactId>
    <version>1.0.1</version>
</dependency>

Or if you don’t use maven you can just download these libraries form Apache’s website…

  1. xmlrpc-client

  2. xmlrpc-common

  3. ws-commons-util
The complete documentation for Bugzilla API is available here...

7 comments:

  1. Hey thanks for the code! This is REALLY helpful! I can't make it work, I'm having the following exception:

    org.apache.xmlrpc.client.XmlRpcHttpTransportException: HTTP server returned unexpected status: Not Found
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.getInputStream(XmlRpcSunHttpTransport.java:94)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:152)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:158)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:147)
    at com.bugzillaClient.controller.BugzillaController.login(BugzillaController.java:191)
    at com.bugzillaClient.controller.BugzillaController.runYSEProcessXls(BugzillaController.java:153)
    at com.bugzillaClient.runner.Launcher.main(Launcher.java:17)

    ReplyDelete
  2. Hi
    It looks like a problem with your bugzilla configuration. My guess is that you need to enable the webservice API or perhaps the API module is missing in your installation.

    ReplyDelete
  3. Hi,
    I am getting one exception when using Bug.create API.

    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at org.apache.xmlrpc.parser.XmlRpcResponseParser.addResult(XmlRpcResponseParser.java:61)
    at org.apache.xmlrpc.parser.RecursiveTypeParserImpl.endValueTag(RecursiveTypeParserImpl.java:78)
    at org.apache.xmlrpc.parser.XmlRpcResponseParser.endElement(XmlRpcResponseParser.java:186)


    Any Idea?

    Regards
    Kondal

    ReplyDelete
  4. Try Integer.parseInt(string)
    BTW this is not a problem with bugzilla..

    ReplyDelete
  5. I am sorry.get method is working fine.some prob with array initialization.

    Thanks

    ReplyDelete
  6. some rare piece of info it is. gr8 work ...

    ReplyDelete
  7. hi..
    can u tell me how to start for it ...from the initial...

    ReplyDelete