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

11 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
  8. Hi,I am trying to develop java code for reporting bugs in bugzilla and i am using bugzilla rest api. Can any one provide me sample code and necessary environment to run the code.Thanks in advance.

    ReplyDelete
  9. Q:: I am trying to develop code in java which automatically reports bugs in

    Bugzilla using j2BugZilla API. My Bugzilla Version is 2.16.4 and j2BugZilla

    Version is 2.2. I am able to connect to BugZilla but after that i am not

    able to login into bugzilla. Code is :

    public class CreateBugInBugZilla {
    public BugzillaConnector connector;
    private String bugzillaurl="http://10.21.9.10/bugzilla-2.16.4";

    public void connect() throws ConnectionException, BugzillaException {
    BugZillaConnector connector;
    connector = new BugzillaConnector();

    connector.connectTo(bugzillaurl);
    System.out.println("...connected.");
    LogIn login=new LogIn(bugzillaUsername,bugzillaPassword);

    connector.executeMethod(login);
    System.out.println("logged in");
    GetLegalValues get=new GetLegalValues(GetLegalValues.Fields.COMPONENT);
    connector.executeMethod(get);
    Set values=get.getLegalValues();
    System.out.println(values);
    BugSearch search = new BugSearch(new BugSearch.SearchQuery

    (BugSearch.SearchLimiter.COMPONENT,"WEBMAIL"));
    connector.executeMethod(search);
    List searchResults=search.getSearchResults();
    for(Bug bug:searchResults)
    {
    System.out.println(bug);
    }
    }


    public static void main(String[]args) throws ConnectionException,

    BugzillaException {

    CreateBugInBugZilla createBugInBugZilla=new CreateBugInBugZilla();

    createBugInBugZilla.connect();
    }

    Exception StackTrace is:---------------------------------------------

    Exception in thread "main" com.j2bugzilla.base.BugzillaException: An unknown

    error was encountered; fault code: 0
    at com.j2bugzilla.base.XmlExceptionHandler.handleFault

    (XmlExceptionHandler.java:96)
    at com.j2bugzilla.base.BugzillaConnector.executeMethod

    (BugzillaConnector.java:169)
    at CreateBugInBugZilla.connect(CreateBugInBugZilla.java:33)
    at CreateBugInBugZilla.main(CreateBugInBugZilla.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke

    (NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke

    (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: org.apache.xmlrpc.client.XmlRpcHttpTransportException: HTTP

    server returned unexpected status: Service Unavailable
    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:137)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126)
    at com.j2bugzilla.base.BugzillaConnector.executeMethod

    (BugzillaConnector.java:164)
    ... 7 more



    ReplyDelete
  10. can anybody tell me how to get bug from a particular year.

    ReplyDelete
  11. Amazing work. Many thanks. Really appreciated the effort.

    ReplyDelete