13

I am using my XML-RPC service using Apache XML-RPC library but in reponse of XML-RPC has junk character so library can not parse the result

Here, is my XML-RPC program:

import java.net.URL;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;


public class XMLRpcExample {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        XmlRpcClientConfigImpl cf = new XmlRpcClientConfigImpl();
        cf.setServerURL(new URL("/xmlrpc/object"));
        cf.setBasicUserName("admin");
        cf.setBasicPassword("m_demo");
        cf.setConnectionTimeout(60000);
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(cf);
        Object[] params = new Object[] {"dbname",1,"m_demo","res.partner","partner_sync_openerp","kapil5drd@bxiz","22"};
        String s =(String)client.execute("execute", params);
        System.out.println(s);
    }

}

But I am getting this error in response, which looks like this:

[Fatal Error] :16:16: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
Exception in thread "main" org.apache.xmlrpc.client.XmlRpcClientException: Failed to parse server's response: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:202)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:165)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:125)
    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 XMLRpcExample.main(XMLRpcExample.java:21)
Caused by: org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 16; An invalid XML character (Unicode: 0xc) was found in the element content of the document.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1237)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:551)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:200)
    ... 8 more

The XML-RPC gives a junk character in reponse.

And the library itself fails to parse the response.

So it means, Apache XML-RPC library is it self unable to parse the response.

Can any body help me with what I need to do?

I have also tried to fix this issue via the internet but I am unable to solve it.

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • I have got the same problem. Were you able to solve the issue? – Ahmed Akhtar May 03 '16 at 12:47
  • Do you know what the response is, which is sent back in answer to your question? (You might try to sniff the traffic with wireshark to see what is sent back.) Perhaps it's compressed in some way or is not using XML at all... – André Schild May 04 '16 at 09:56
  • @AndréSchild No, actually most calls to the remote procedure go all right. This just happens rarely for some specific calls. Note that, the remote procedure is from a third party and it cannot be changed The change needs to be made at our end to avoid this error. – Ahmed Akhtar May 04 '16 at 10:12
  • So the basic question is: Why is the remote service sometimes returning invalid XML answers. Unless you can track such a invalid message, the only way to handle it would be to wrap the client.execute(...) inside a try/catch block and when the exception is thrown, just handle it in some "gracefull" way. (Perhaps retry later, or what ever is appropriate) – André Schild May 04 '16 at 10:49
  • @AndréSchild Thanks for following up sir. To be very specific, I am fetching bugs using [Bugzilla::Webservice::Bug::search](https://www.bugzilla.org/docs/4.4/en/html/api/Bugzilla/WebService/Bug.html#search) remote procedure from its deployment at [Bugzilla@Mozilla](https://bugzilla.mozilla.org/) and using the following code `search(searchCriteria, offset, limit);` which fails when: criteria is `product=firefox`, `offset` is `21900` and `limit` is `100` whereas for other values of `offset` it works fine. – Ahmed Akhtar May 04 '16 at 11:03
  • So we now realy must know what is returned from the server in that specific call/query. – André Schild May 04 '16 at 13:14
  • @AndréSchild Doesn't the line: An invalid XML character (Unicode: 0xc) was found in the element content of the document. tell us that? – Ahmed Akhtar May 04 '16 at 15:06
  • character (Unicode: 0xc) is a page break: Please check you sql result with those parameters, it seems like you getting an invalid response. Duplicate the sql query and double check the result. – victor sosa May 04 '16 at 15:21
  • I repeat myself once more: Show us the string you receive back from the server call, when the error happens. Everything else is just guessing – André Schild May 05 '16 at 10:14
  • Could you post the server code as well? – Vitthal Kavitake May 09 '16 at 16:17

1 Answers1

3

Here is the working example for your parameters, which can help you


Handler Class:

public class Handler {
    public String execute(String dbName, Integer i, String a, String b, String c, String d, String e){
        System.out.println("Got inputs: "+dbName+", "+i+", "+a+", "+b+", "+c+", "+d+", "+e);
        return "<?xml version=\"1.0\"> <test>testmail@testdomain.com</test>";
    }
}

More such handlers can be added using phm.addHandler("handler",Handler.class); to the server code. More methods can be added to this class and can be called from client.


XMLRPC Server:

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

public class Server {
    private static final int port = 8080;

    public static void main(String[] args) throws Exception {
        WebServer webServer = new WebServer(port);
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        phm.addHandler("handler",Handler.class);
        xmlRpcServer.setHandlerMapping(phm);
        XmlRpcServerConfigImpl serverConfig =
                (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);
        webServer.start();
    }
}

XMLRPC Client:

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.net.URL;

public class Client {
    public static void main(String args[])throws Exception{
        XmlRpcClientConfigImpl cf = new XmlRpcClientConfigImpl();
        cf.setServerURL(new URL("http://localhost:8080/xmlrpc/object"));
        cf.setBasicUserName("admin");
        cf.setBasicPassword("m_demo");
        cf.setConnectionTimeout(60000);
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(cf);
        Object[] params = new Object[] {"dbname",1,"m_demo","res.partner","partner_sync_openerp","kapil5drd@bxiz","22"};
        String s =(String)client.execute("handler.execute", params);
        System.out.println(s);
    }
}
Vitthal Kavitake
  • 879
  • 5
  • 18
  • What if the server in question is a third party server and is sending an invalid XML character in response to an rpc call? Can we handle/ignore that character in some way? – Ahmed Akhtar May 10 '16 at 04:29
  • edited the example to send the special characters from the server. This is encoded while transmitting by library and does not need to be worry about. – Vitthal Kavitake May 10 '16 at 15:21
  • When returning `return " " + Character.toString((char)3) + "";` from server in Handler class, the client crashes giving error: `An invalid XML character (Unicode: 0x3) was found in the element content of the document.` Do you know how to solve this at client end without changing the server code? – Ahmed Akhtar May 12 '16 at 07:07