<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_To_Call_An_Openbravo_Webservice_From_Java</id>
	<title>How To Call An Openbravo Webservice From Java - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_To_Call_An_Openbravo_Webservice_From_Java"/>
	<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_To_Call_An_Openbravo_Webservice_From_Java&amp;action=history"/>
	<updated>2026-04-06T16:55:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.infinite-erp.co.id/index.php?title=How_To_Call_An_Openbravo_Webservice_From_Java&amp;diff=2723&amp;oldid=prev</id>
		<title>Wikiadmin: Created page with &quot;__FORCETOC__ {{RatingArticle}} {{Languages | Documentation_Style_Guide}} == Objective == The objective of this article it to provide the necessary steps to prepare a Java clas...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_To_Call_An_Openbravo_Webservice_From_Java&amp;diff=2723&amp;oldid=prev"/>
		<updated>2021-12-15T07:00:04Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__FORCETOC__ {{RatingArticle}} {{Languages | Documentation_Style_Guide}} == Objective == The objective of this article it to provide the necessary steps to prepare a Java clas...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__FORCETOC__&lt;br /&gt;
{{RatingArticle}}&lt;br /&gt;
{{Languages | Documentation_Style_Guide}}&lt;br /&gt;
== Objective ==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Recommended articles ==&lt;br /&gt;
Before reading this guide, we recommend that you take some time to get further information about the REST webservice concept:&lt;br /&gt;
 &lt;br /&gt;
*[[ERP_2.50:Developers_Guide/Concepts/XML_REST_Web_Services | XML REST web services]]&lt;br /&gt;
&lt;br /&gt;
== Execution Steps ==&lt;br /&gt;
&lt;br /&gt;
=== Background ===&lt;br /&gt;
&lt;br /&gt;
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[http://www.saxproject.org/]. &lt;br /&gt;
&lt;br /&gt;
=== Example Code ===&lt;br /&gt;
&lt;br /&gt;
Openbravo has a '''test''' java class that exemplifies the above. This class is '''&amp;quot;BaseWSTest.java&amp;quot;''' and it is located inside the directory '''''&amp;quot;src-test/org/openbravo/test/webservice&amp;quot;'''''. To make a webservice request we have the '''&amp;quot;createConnection()&amp;quot;''' 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).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;protected HttpURLConnection createConnection(String wsPart, String method) throws Exception {&lt;br /&gt;
    Authenticator.setDefault(new Authenticator() {&lt;br /&gt;
        @Override&lt;br /&gt;
        protected PasswordAuthentication getPasswordAuthentication() {&lt;br /&gt;
            return new PasswordAuthentication(LOGIN, PWD.toCharArray());&lt;br /&gt;
        }&lt;br /&gt;
    });&lt;br /&gt;
    log.debug(method + &amp;quot;: &amp;quot; + getOpenbravoURL() + wsPart);&lt;br /&gt;
    final URL url = new URL(getOpenbravoURL() + wsPart + &amp;quot;&amp;amp;basicAuthentication=true&amp;quot;);&lt;br /&gt;
    final HttpURLConnection hc = (HttpURLConnection) url.openConnection();&lt;br /&gt;
    hc.setRequestMethod(method);&lt;br /&gt;
    hc.setAllowUserInteraction(false);&lt;br /&gt;
    hc.setDefaultUseCaches(false);&lt;br /&gt;
    hc.setDoOutput(true);&lt;br /&gt;
    hc.setDoInput(true);&lt;br /&gt;
    hc.setInstanceFollowRedirects(true);&lt;br /&gt;
    hc.setUseCaches(false);&lt;br /&gt;
    hc.setRequestProperty(&amp;quot;Content-Type&amp;quot;, &amp;quot;text/xml&amp;quot;);&lt;br /&gt;
    return hc;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note: this sample code uses basic authentication, see below for more details.&lt;br /&gt;
&lt;br /&gt;
Using this function as our base, we will implement other methods for the other HTTP comamnds:&lt;br /&gt;
'''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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;protected String doTestGetRequest(String wsPart, String testContent, int responseCode,boolean validate) {&lt;br /&gt;
    try {&lt;br /&gt;
        final HttpURLConnection hc = createConnection(wsPart, &amp;quot;GET&amp;quot;);&lt;br /&gt;
        hc.connect();&lt;br /&gt;
        final SAXReader sr = new SAXReader();&lt;br /&gt;
        final InputStream is = hc.getInputStream();&lt;br /&gt;
        final StringBuilder sb = new StringBuilder();&lt;br /&gt;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, &amp;quot;UTF-8&amp;quot;));&lt;br /&gt;
        String line;&lt;br /&gt;
        while ((line = reader.readLine()) != null) {&lt;br /&gt;
            sb.append(line).append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        try {&lt;br /&gt;
            final Document doc = sr.read(new StringReader(sb.toString()));&lt;br /&gt;
            final String content = XMLUtil.getInstance().toString(doc);&lt;br /&gt;
            if (testContent != null &amp;amp;&amp;amp; content.indexOf(testContent) == -1) {&lt;br /&gt;
                log.debug(content);&lt;br /&gt;
                fail();&lt;br /&gt;
            }&lt;br /&gt;
            assertEquals(responseCode, hc.getResponseCode());&lt;br /&gt;
            is.close();&lt;br /&gt;
            // do not validate the xml schema itself, this results in infinite loops&lt;br /&gt;
            if (validate) {&lt;br /&gt;
                validateXML(content);&lt;br /&gt;
            }&lt;br /&gt;
            return content;&lt;br /&gt;
        } catch (Exception e) {&lt;br /&gt;
            log.debug(sb.toString());&lt;br /&gt;
            throw e;&lt;br /&gt;
          }&lt;br /&gt;
    } catch (final Exception e) {&lt;br /&gt;
          throw new OBException(&amp;quot;Exception when executing ws: &amp;quot; + wsPart, e);&lt;br /&gt;
      }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
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''').&lt;br /&gt;
&lt;br /&gt;
== Other Aspects ==&lt;br /&gt;
&lt;br /&gt;
=== Login and Security ===&lt;br /&gt;
&lt;br /&gt;
Openbravo general webservices provides the same login and security control as the [[XML_REST_Web_Services#Login_and_Security| XML REST]] webservice.&lt;br /&gt;
&lt;br /&gt;
=== Basic Authentication: two ways to do it ===&lt;br /&gt;
&lt;br /&gt;
The above sample code shows one way of implementing basic authentication:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;    Authenticator.setDefault(new Authenticator() {&lt;br /&gt;
      @Override&lt;br /&gt;
      protected PasswordAuthentication getPasswordAuthentication() {&lt;br /&gt;
        return new PasswordAuthentication(getLogin(), getPassword().toCharArray());&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This approach will need two requests. The first one will fail with 401, return to the caller/client and then call the getPasswordAuthentication method above for a second request with credentials. As two requests are needed for this authentication it is less useful for high frequency calls. '''To make this type of approach work in Openbravo code it is required to pass in the parameter 'basicAuthentication=true' in the request url.'''&lt;br /&gt;
&lt;br /&gt;
Another approach to set credentials in the request header like this, this only requires one request as Openbravo will directly find the authentication information and will not throw a 401:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
    String userpass = getLogin() + &amp;quot;:&amp;quot; + getPassword();&lt;br /&gt;
    String basicAuth = &amp;quot;Basic &amp;quot; + new String(new Base64().encode(userpass.getBytes()));&lt;br /&gt;
    hc.setRequestProperty(&amp;quot;Authorization&amp;quot;, basicAuth);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== High Frequency - stateless ===&lt;br /&gt;
&lt;br /&gt;
Client side code which calls webservices will typically not maintain the session cookie of the server. This means that every webservice request can create a new http session on the receiving server. This is not adviced for high frequency webservices. The implementor of the webservice within the Openbravo system can force the call being stateless. But the caller can also achieve this by passing a parameter. See [[How_to_create_a_new_REST_webservice#Stateless_Webservice_Requests_-_HTTP_Session|here]] for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:HowTo]]&lt;/div&gt;</summary>
		<author><name>Wikiadmin</name></author>
		
	</entry>
</feed>