View source | Discuss this page | Page history | Printable version   
Main Page
Upload file
What links here
Recent changes

PDF Books
Add page
Show collection (0 pages)
Collections help


How To Call An Openbravo Webservice From Java



The objective of this article it to provide the necessary steps to prepare a Java class so it is possible to call Openbravo web services from this class.

Recommended articles

Before reading this guide, we recommend that you take some time to get further information about the REST webservice concept:

Execution Steps

To call an Openbravo Webservice from a Java class we have to make use of several classes that provide the proper Java API to create a HTTP connection via the webservice's URL and to use classes of the package that allow to read the data streams. Furthermore we can optionally use some classes that ease processing the results that are returned in XML format in case of the XML REST type webservices, because they allow to get, parse and validate an XML document[1].

Openbravo has a test java class that exemplifies the above. This class is "" and it is located inside the directory "src-test/org/openbravo/test/webservice". To make a webservice request we have the "createConnection()" method which uses the httpURLConnection class to make a request to a webservice by using the specific command parameter and specifying the credentials (username/password).

protected HttpURLConnection createConnection(String wsPart, String method) throws Exception {
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(LOGIN, PWD.toCharArray());
    log.debug(method + ": " + getOpenbravoURL() + wsPart);
    final URL url = new URL(getOpenbravoURL() + wsPart);
    final HttpURLConnection hc = (HttpURLConnection) url.openConnection();
    hc.setRequestProperty("Content-Type", "text/xml");
    return hc;

Using this function as our base, we will implement other methods for the other HTTP comamnds: POST, PUT, GET and DELETE. Furthermore, this class uses the org.xml.sax library to parse the XML results that will be returned after our HTTP request, which we refered to before. For example, the GET request:

protected String doTestGetRequest(String wsPart, String testContent, int responseCode,boolean validate) {
    try {
        final HttpURLConnection hc = createConnection(wsPart, "GET");
        final SAXReader sr = new SAXReader();
        final InputStream is = hc.getInputStream();
        final StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
        try {
            final Document doc = StringReader(sb.toString()));
            final String content = XMLUtil.getInstance().toString(doc);
            if (testContent != null && content.indexOf(testContent) == -1) {
            assertEquals(responseCode, hc.getResponseCode());
            // do not validate the xml schema itself, this results in infinite loops
            if (validate) {
            return content;
        } catch (Exception e) {
            throw e;
    } catch (final Exception e) {
          throw new OBException("Exception when executing ws: " + wsPart, e);

Thus we can utilize this class as the base for our development and implement the adaptations that we consider necessary. Using this class, we can implement calls to the webservices of your choice by simply specifying its path, that will be formed with the Openbravo URL (getOpenbravoURL()) plus the webservice part (wsPart).

Retrieved from ""

This page has been accessed 4,343 times. This page was last modified on 23 October 2015, at 07:49. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.