0

I am using XML-RPC plugin in Java to extract ticket information from Trac.

So far I am successful with the following code

     config.setBasicUserName ("username");
     config.setBasicPassword ("password");
     config.setServerURL (new URL ("trac_url"));

     client = new XmlRpcClient();
     client.setConfig (config);

     Object [] params = new Object [] {};
     Object [] queryResult = null;

     try {
         queryResult = (Object []) client.execute ("ticket.query", params);

     } catch (XmlRpcException e) {
         System.out.println(e.getMessage());
         System.out.println(e.code);
         System.out.println(e.linkedException);
         System.out.println(e.getCause());
         e.printStackTrace();
     }

but when i change url to another project then it just gives error messages

for your information the url is running fine when i use the very same url to open in a browser, i can see the tickets there, but it does not work in program

I get this for the above code

    ????????? XML_RPC ???????    // from  System.out.println(e.getMessage());
    1  // from System.out.println(e.code);
    org.apache.xmlrpc.XmlRpcException: ????????? XML_RPC ???????
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:197)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156)
    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 xmlropx.ListTracTicket.extractDataFromTrac(ListTracTicket.java:206)
    at xmlropx.ListTracTicket.main(ListTracTicket.java:263)
    null   // from  System.out.println(e.linkedException);
    null   // from  System.out.println(e.getCause());

please help me solve this

Harry
  • 1,572
  • 2
  • 17
  • 31
  • Have you tested that you can authenticate at `/login/rpc` on the site that gives you the error? That would confirm that XmlRpc is installed and that the user has the appropriate permissions. – RjOllos Jul 14 '14 at 05:15
  • @RjOllos yes i can login using browser successfully and see the tickets – Harry Jul 15 '14 at 01:02
  • Your account might not be flagged for XMLRPC permissions on that Trac instance. Do you have access to Trac's log file? That should provide more details as to what happened. – bta Jul 20 '14 at 13:50

1 Answers1

0

Seems to you have some specific configuration on Trac server (e.g. Apache Httpd) regarding to second url. You may try alternative approach to reach out the url. Maybe it will produce more detailed exception or something else.

I tried to work with Trac over regular 'URL' / 'URLConnection' java classes, it works ok and you also control the whole process since http/rpc stuff is low-level implemented.

The only tricky thing was the ticket version timestamp - it should be specified as additional argument for RPC ticket modification method.

A short example.

public static JSONObject sendJsonRpc(String method, JSONArray parameters) throws IOException, JSONException {

    JSONObject rpc = new JSONObject();
    rpc.put("method", method);
    rpc.put("params", parameters);
    rpc.put("id", jsonIdSequence++);

    String tracUrl = Configuration.getTracApiUrl();
    String tracUsername = Configuration.getTracApiUsername();
    String tracPassword = Configuration.getTracApiPassword();

    URL url = new URL(tracUrl);
    URLConnection conn = url.openConnection();
    String userpass = tracUsername + ":" + tracPassword;
    String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
    conn.setRequestProperty ("Authorization", basicAuth);
    conn.setRequestProperty ("Content-Type", "application/json");
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    Logger.debug("jsonrpc request:\n" + rpc.toString());

    wr.write(rpc.toString());
    wr.flush();
    wr.close();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    String jsonText = "";
    while ((line = rd.readLine()) != null) {
        jsonText += line;
    }
    rd.close();

    Logger.debug("jsonrpc response:\n" + jsonText);

    return new JSONObject(jsonText);
}

You can find more examples here Java, Trac, RPC and JSON .

Nikolay Antipov
  • 920
  • 2
  • 8
  • 17