Creating HttpClients and passing XML documents

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.

Getting ready

The first step in writing any web services client is to gather the following facts about how the target web service operates:

  • The URL on the web for the service provider
  • Any connection parameters and/or HTTP/HTTPS request message header settings that must be passed as required by the service provider
  • The HTTP/HTTPS connection verb (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;

How to do it...

To create an HttpClient and pass XML documents, follow these steps:

  1. Create a connection string that includes the URL of the target web service and the type of request:
    String connectString =
    "http://www.some_web_service_url.com/serviceName";
    
  2. Within your Java program, create an instance of the HttpClient object with the URL/connection string passed to the constructor as shown here:
    HttpClient http = new HttpClient(connectString);
    
  3. Create the XML document as prescribed by the target web service. For example, the following code snippet was taken from the 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
    }
    
  4. Invoke the 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
    }
    
  5. Process the response from the web service call inline.
  6. There is no need to release or close the connection as OFBiz manages the handoff of network connections transparent to the developer.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.80.101