odoojax-rsxml-rpc

How to use Odoo REST Services with JAX-RS?


I am trying to write a Java web application that runs on A Tomee application server. The application uses a number of Jakarta EE technologies such as JAX-RS, Jakarta Faces, and CDI. The basic application is running fine; I have no problems with that.

However, at one point, I need to update our Odoo server. Odoo provides a REST API as described here: Odoo External API

That page provides a number of examples for various programming languages, including Java. Unfortunately, the Java example uses the Apache XML-RPC package, which hasn't been updated for about 14 years, and may have security vulnerabilities, as I understand from Maven Central.

So I would prefer not to use that, and am looking for some way of communicating with Odoo with the technology at hand, for example using the JAX-RS client.

The Odoo documentation provides an Apache XML-RPC example for obtaining the server version, which can be run without authentication:

final String url = "myServer";
final XmlRpcClientConfigImpl common_config = new XmlRpcClientConfigImpl();
common_config.setServerURL(new URL(String.format("%s/xmlrpc/2/common", url)));
client.execute(common_config, "version", emptyList());

According to the documentation, executing the example should result in the following output:

{
    "server_version": "13.0",
    "server_version_info": [13, 0, 0, "final", 0],
    "server_serie": "13.0",
    "protocol_version": 1,
}

Note that I don't have Apache XML RPC installed and haven't tested this example. Instead, I tried to carry out the same action using the JAX-RS client using the following code:

private final static String pathCommon = "/xmlrpc/2/common";                       
private final static String odooServer = "http://my.server";
Client client = ClientBuilder.newClient();                         
WebTarget server = client.target(odooServer);                      
WebTarget common = server.path(pathCommon);                                                                                         
Invocation.Builder invoker = common.request(MediaType.TEXT_PLAIN);
Entity<String> e = Entity.text("version");                                                           
Response resp = invoker.post(e);                                     

This really didn't work at all. The request did seem to make it to the Odoo server, and the Odoo server felt that it was being talked to, but the request format I sent was obviously wrong. The server responded as follows, which is not what was expected.

Response status: 200, Status type:OK, Status family:SUCCESSFUL, Status phrase:OK
 Length: 783, has entity: true
Data :
entity as string: 
<?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Traceback (most recent call last):
  File "/opt/odoo/odoo/odoo/addons/base/controllers/rpc.py", line 69, in xmlrpc_2
    response = self._xmlrpc(service)
  File "/opt/odoo/odoo/odoo/addons/base/controllers/rpc.py", line 48, in _xmlrpc
    params, method = loads(data)
  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 1017, in loads
    p.close()
  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 447, in close
    parser.Parse(b"", True) # end of data
xml.parsers.expat.ExpatError: syntax error: line 1, column 0
</string></value>
</member>
</struct></value>
</fault>
</methodResponse>

My best guess is that Odoo was expecting an XML message as input rather than text. But I cannot find any documentation at all about what message format would be expected. Can anyone provide a pointer to the Odoo documentation for the actual messages expected? I the JAX-RS client the proper tool to be using, or would there be a better alternative? Could anyone provide an example of using the Odoo REST API with Jax-RS? Does Odoo maybe have a Json-based REST interface?

Thank you for any help!


Solution

  • I got this figured out well enough for my purposes. You can communication with Odoo using XML messages that are formed according to the description here: The XML RPC Specification .

    To make my code example from above work, I had to change the code like this:

    StringBuilder txt = new StringBuilder(128);
      txt.append("<?xml version=\"1.0\"?>\n");
      txt.append("<methodCall>\n");
      txt.append("  <methodName>version</methodName>\n");
      txt.append("  <params>\n");
      txt.append("  </params>\n");
      txt.append("</methodCall>\n");
    Client client = ClientBuilder.newClient();
    WebTarget server = client.target(odooServer);
    WebTarget common = server.path(pathCommon);
    Invocation.Builder invoker = common.request(MediaType.TEXT_PLAIN);
    Entity<String> e = Entity.text(txt.toString());
    Response resp = invoker.post(e);
    

    This gave me the expected response, namely:

    <?xml version='1.0'?>
    <methodResponse>
    <params>
    <param>
    <value><struct>
    <member>
    <name>server_version</name>
    <value><string>13.0</string></value>
    </member>
    <member>
    <name>server_version_info</name>
    <value><array><data>
    <value><int>13</int></value>
    <value><int>0</int></value>
    <value><int>0</int></value>
    <value><string>final</string></value>
    <value><int>0</int></value>
    <value><string></string></value>
    </data></array></value>
    </member>
    <member>
    <name>server_serie</name>
    <value><string>13.0</string></value>
    </member>
    <member>
    <name>protocol_version</name>
    <value><int>1</int></value>
    </member>
    </struct></value>
    </param>
    </params>
    </methodResponse>
    

    With that, I was able to figure out the other messages I needed. :-)