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

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

Search

How To Call An Openbravo Webservice From Java

Contents

Objective

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 java.io 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 "BaseWSTest.java" 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() {
        @Override
        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.setRequestMethod(method);
    hc.setAllowUserInteraction(false);
    hc.setDefaultUseCaches(false);
    hc.setDoOutput(true);
    hc.setDoInput(true);
    hc.setInstanceFollowRedirects(true);
    hc.setUseCaches(false);
    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");
        hc.connect();
        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) {
            sb.append(line).append("\n");
        }
        try {
            final Document doc = sr.read(new StringReader(sb.toString()));
            final String content = XMLUtil.getInstance().toString(doc);
            if (testContent != null && content.indexOf(testContent) == -1) {
                log.debug(content);
                fail();
            }
            assertEquals(responseCode, hc.getResponseCode());
            is.close();
            // do not validate the xml schema itself, this results in infinite loops
            if (validate) {
                validateXML(content);
            }
            return content;
        } catch (Exception e) {
            log.debug(sb.toString());
            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 "http://wiki.openbravo.com/wiki/How_To_Call_An_Openbravo_Webservice_From_Java"

This page has been accessed 4,224 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.