Online Book Reader

Home Category

Beautiful Code [304]

By Root 5002 0
may seem to be a rather simple abstraction, it goes a long way in making the code readable and understandable by an inexperienced programming staff. When it comes time to add a new distributor that happens to be using Microsoft's BizTalk product and wants to place orders electronically, the programmer has a simple template for adding this new requirement.

Integrating Business Partners the RESTful Way > Exchanging Data Using E-Business Protocols

27.4. Exchanging Data Using E-Business Protocols

Something that was new to me on this project was the use of standard e-business protocols. When the distributor informed me of the requirement to exchange requests and responses using the Rosettanet standard, I had to do a little research. I started by going to the Rosettanet web site (http://www.rosettanet.org) and downloading the specific standards I was interested in. I found a diagram detailing a typical exchange between business partners, along with a specification for the XML request and response.

Since I had a lot of trial-and-error type work to do, the first thing I did was set up a test that I could run myself to simulate an interaction with the distributor without having to coordinate testing with their staff for each iteration of development. I used the Apache Commons HttpClient to manage the HTTP exchanges:

Code View: Scroll / Show All

public class TestHotKeyService {

public static void main (String[] args) throws Exception {

String strURL = "http://xxxxxxxxxxx/HotKey/HotKeyService";

String strXMLFilename = "SampleXMLRequest.xml";

File input = new File(strXMLFilename);

PostMethod post = new PostMethod(strURL);

post.setRequestBody(new FileInputStream(input));

if (input.length( ) < Integer.MAX_VALUE) {

post.setRequestContentLength((int)input.length());

} else {

post.setRequestContentLength(

EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

}

post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

HttpClient httpclient = new HttpClient();

System.out.println("[Response status code]: " +

httpclient.executeMethod(post));

System.out.println("\n[Response body]: ");

System.out.println("\n" + post.getResponseBodyAsString( ));

post.releaseConnection();

}

}

This allowed me to accelerate my learning curve as I tried out several different types of requests and examined the results. I'm a firm believer in diving right into coding as soon as possible. You can only learn so much from a book, an article on a web site, or a set of API documents. By getting your hands dirty early on in the process, you'll uncover a lot of things you may not have thought about by simply studying the problem.

The Rosettanet standard, like many others, is very detailed and complete. Chances are, you'll end up needing and using only a small fraction of it to accomplish any given task. For this project, I only needed to set a few standard identification fields, along with a product number and availability date for pricing inquiries, or an order number for order status inquiries.

27.4.1. Parsing the XML Using XPath

The XML request data was far from simple XML. As mentioned earlier, the Rosettanet standard is very detailed and thorough. Parsing such a document could have proved to be quite a nightmare if it were not for XPath. Using XPath mappings, I was able to define the exact path to each node that I was interested in and easily pull out the necessary data. I chose to implement these mappings as a HashMap, which I later iterated over, grabbing the specified nodes and creating a new HashMap with the values. These values were then used later in both the executeQuery and getResponseXML methods that I'll describe next:

Code View: Scroll / Show All

public class HotkeyAdaptorRosProdAvailImpl implements HotkeyAdaptor {

String inputFile; // request XML

HashMap requestValues; // stores parsed XML values from request

HashMap as400response; // stores return parameter from RPG call

/* Declare XPath mappings and populate with a static initialization block

Return Main Page Previous Page Next Page

®Online Book Reader