Pages

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...