TOMÁŠ HUBÁLEK BLOG: BAVTE SE PŘIMĚŘENĚ…

How to submit Bugzilla bug using XML/RPC and pure Java

Written By: Tomáš Hubálek - Jul• 16•09

Our project uses Bugzilla for tracking issues. I was looking for solution how to submit occured exception programatically. I found quite simple solution using Groovy and I have adapted it to pure Java.

Here is the source code:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

public class Main {
   public static void main(String s[])
      throws MalformedURLException, XmlRpcException {

        HttpClient httpClient = new HttpClient();
        XmlRpcClient rpcClient = new XmlRpcClient();
        XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

        factory.setHttpClient(httpClient);
        rpcClient.setTransportFactory(factory);
        config.setServerURL(new URL("http://bugzilla.example.com/xmlrpc.cgi"));
        rpcClient.setConfig(config);

        // map of the login data
        Map loginMap = new HashMap();
        loginMap.put("login", "tomas.hubalek@....");
        loginMap.put("password", "*top*secret*");
        loginMap.put("rememberlogin", "Bugzilla_remember");

        // login to bugzilla
        Object loginResult = rpcClient.execute("User.login", new Object[]{loginMap});
        System.err.println ("loginResult=" + loginResult);

        // map of the bug data
        Map bugMap = new HashMap();

        bugMap.put("product", "Playground");
        bugMap.put("component", "Database");
        bugMap.put("summary", "Bug created from groovy script");
        bugMap.put("description", "This is text including stacktrace");
        bugMap.put("version", "unspecified");
        bugMap.put("op_sys", "Linux");
        bugMap.put("platform", "PC");
        bugMap.put("priority", "P2");
        bugMap.put("severity", "Normal");
        bugMap.put("status", "NEW");

        // create bug
        Object createResult = rpcClient.execute("Bug.create", new Object[]{bugMap});
        System.err.println("createResult = " + createResult);
     }
 }

Usage of Apache XML RPC Client is quite straightforward, only gotcha is that mapped parameters must be set passed as new Object[]{bugMap}. Otherwise you get ClassCastException.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Comments

  1. […] Na našem posledním projektu používáme na evidenci změn Bugzillu. Je to velice praktický nástroj, hodně vývojářům a testerům pomáhá, nehledě na snadnou integraci přímo do testovaného produktu (vyhozená exception může být snadno reportována do Bugzilly). […]

  2. rahul singh says:

    Nice blog . To run this sample following jar will be use :;
    1.commons-pack.jar
    2.ws-commons-util-1.0.2.jar
    3.commons-logging-1.1.1.jar
    4.xmlrpc-commons-3.1.jar
    5.xmlrpc-client-3.1.3.jar

  3. O D says:

    Using the above code i am getting following Error:

    BugCreator2.java:20: error: cannot find symbol
    factory.setHttpClient(httpClient);
    ^
    symbol: method setHttpClient(HttpClient)
    location: variable factory of type XmlRpcCommonsTransportFactory
    Note: BugCreator2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error

    Please Help.

  4. anant says:

    I am getting the following error using above code,please help me out.
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: I/O exception (javax.net.ssl.SSLProtocolException) caught when processing request: handshake alert: unrecognized_name
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: Retrying request
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: I/O exception (javax.net.ssl.SSLProtocolException) caught when processing request: handshake alert: unrecognized_name
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: Retrying request
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: I/O exception (javax.net.ssl.SSLProtocolException) caught when processing request: handshake alert: unrecognized_name
    Feb 23, 2015 6:17:28 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    INFO: Retrying request
    Exception in thread “main” org.apache.xmlrpc.XmlRpcException: I/O error while communicating with HTTP server: handshake alert: unrecognized_name
    at org.apache.xmlrpc.client.XmlRpcCommonsTransport.writeRequest(XmlRpcCommonsTransport.java:244)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:151)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
    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 Logging.LogginDefects1.bugZillaConnection(LogginDefects1.java:62)
    at Logging.LoggingDefects.main(LoggingDefects.java:26)
    Caused by: javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name
    at sun.security.ssl.ClientHandshaker.handshakeAlert(ClientHandshaker.java:1288)
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1936)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1059)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1294)
    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:685)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:111)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at java.io.FilterOutputStream.flush(FilterOutputStream.java:140)
    at org.apache.xmlrpc.client.XmlRpcCommonsTransport$1$1.close(XmlRpcCommonsTransport.java:204)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport$ByteArrayReqWriter.write(XmlRpcHttpTransport.java:55)
    at org.apache.xmlrpc.client.XmlRpcCommonsTransport$1.writeRequest(XmlRpcCommonsTransport.java:214)
    at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:495)
    at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:1973)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:993)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at org.apache.xmlrpc.client.XmlRpcCommonsTransport.writeRequest(XmlRpcCommonsTransport.java:227)
    … 8 more
    Caused by:

Leave a Reply

Your email address will not be published. Required fields are marked *