There are many examples within the OFBiz out-of-the-box code base of web service clients that call real world web services and pass XML documents as part of the payload. These web services clients use the HttpClient
as described in the previous recipe in addition to XML document preparation utilities to build the necessary message content used to communicate with external web services.
In this section, we discuss how to write your own web service client(s) and exchange XML documents with one or more external-to-OFBiz web services providers.
The first step in writing any web services client is to gather the following facts about how the target web service operates:
get, post
, or other)To write a web service client that passes one or more XML documents, import at least the following into your Java program:
// Use these to build an XML document import org.ofbiz.base.util.UtilXml; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; // Use these to send/receive web services client services: import org.ofbiz.base.util.HttpClient; import org.ofbiz.base.util.HttpClientException;
To create an HttpClient
and pass XML documents, follow these steps:
String connectString = "http://www.some_web_service_url.com/serviceName";
HttpClient
object with the URL/connection string passed to the constructor as shown here:HttpClient http = new HttpClient(connectString);
UpsServices.java
program where an XML document is sent to the target web service:Document rateRequestDoc = UtilXml.makeEmptyXmlDocument("RatingServiceSelectionRequest"); Element rateRequestElement = rateRequestDoc.getDocumentElement(); rateRequestElement.setAttribute("xml:lang", "en-US"); // XML request header Element requestElement = UtilXml.addChildElement(rateRequestElement, "Request", rateRequestDoc); // Code removed… // Example, setting up required and optional XML document // elements: the pickup type Element pickupElement = UtilXml.addChildElement(rateRequestElement, "PickupType", rateRequestDoc); // Code removed… // Example setting more elements, this time: package info String maxWeightStr = UtilProperties.getPropertyValue(serviceConfigProps, "shipment.ups.max.estimate.weight", "99"); BigDecimal maxWeight = new BigDecimal("99"); // Code removed… // Use OFBiz UtilXml utility to create the XML document try { rateRequestString = UtilXml.writeXmlDocument(rateRequestDoc); } HttpClientsXML documents, passingcatch (IOException e) { String ioeErrMsg = "Error writing RatingServiceSelectionRequest XML Document" + " to a String: " + e.toString(); Debug.logError(e, ioeErrMsg, module); return ServiceUtil.returnFailure(ioeErrMsg); } // More code removed… // Call to the method that eventually calls the UPS web service try { rateResponseString = sendUpsRequest("Rate", xmlString.toString()); } catch (UpsConnectException e) { // Process errors here }
HttpClient
object to call the target web service and pass the XML document within the request message. For example, using the UpsServices.java
program from step 3, the call to the HttpClient
object looks something like the following:public static String sendUpsRequest(String upsService, String xmlString) throws UpsConnectException { // Code removed… HttpClient http = new HttpClient(conStr); // You may set a timeout in milliseconds http.setTimeout(timeout * 1000); http.setAllowUntrusted(true); String response = null; try { response = http.post(xmlString); } catch (HttpClientException e) { // Process errors here } // Handle the return response as necessary here }
The HttpClient
utility may be used to send and receive XML documents as directed by web service providers. When using the HttpClient
, web service client requests are made inline with code logic. Once established, communications between the HttpClient
and the target web service are streamed directly across the HTTP/HTTPS connection, bypassing OFBiz.
52.15.80.101