|
Jkb's RaSS Servlet UPS Online Tools UpsPostage.java |
I decided to implement the Rates and Service Selection (RaSS), one of the Web Services provided by UPS in a Servlet. I produced a simple web interface in the doGet() method of the servlet where I accept parameters for the web service. Then, in the doPost() which is called by submitting the form, I connect to the web service and produce an output page containing the shipping price quote, or the detailed error message. I think this is an example of a Document Literal web service transaction as opposed to the SOAP-RPC web services that I have been using as of late. Using the UPS Online Tools involved getting a Developer's Key, which allows you to get access to the specifications involved filling out a simple form. You then can use the key when you are ready to implement one of these programs to get an Access Key, which you use to to authenticate and make the call. You will get the chance to select the type of Online Tool transaction that you will use. There are two types, HTML and XML. HTML is essentially a destination for your submitted web forms. This is optimized for Web Sites. I implemented the XML version, which allows you to integrate this same capability into server-side and non-web systems, as well as web applications. I use a ResourceBundle to store my UPS Username, Password and Access Key as well as the URL of the RaSS service. This way, I can post the code up here and let you all have a look without sacrificing my information. I produced the two-part XML request by writing my XML request document into a set of StringBuffers. The first part is the access request, which contains my credentials and access key. The second part has the XML transactional information. I connect to the Web Service and send these two documents to the URL, which reads my documents and then sends back a gigantic string containing the whole response XML document. The XML response returned by UPS is read by an Object called a StringReader that can be passed into my SAXParser. The SAXParser is linked to a RatingServiceResponseHandler, which implements the SAX event handlers based on elements. My Handler reads the data from that XML document and provides 'getters' to pull the data back out. The doPost() method calls the 'getters' of the RatinServiceResponseHandler to produce the output HTML page. I also print the Web Service return values as well as any errors if the transaction returns an error. Even though I used XML for my servlet, it may not be as fast as the HTML direct access option. The XML processing and the transfer times involved may have given the HTML interface a slight edge, but I'm not going to test it. I'll leave it to anyone else who wants to benchmark it. Anyway, I had fun implementing this little proof of concept program, and maybe you will find it fun, useful and/or interesting. Fun, since you can quote shipping on large, heavy boxes being sent from your house to wherever. Useful, in the sense of looking at the code to see how it works and maybe help one of your own development projects--use UPS to get actual quotes. And Interesting so you can say "ah ha, I always wondered how they do that sort of stuff!" |