I noticed today that Amazon.com has recently updated their web services. I’m a big fan of Amazon Web Services (AWS) for several reasons. First, it was the impetus for my online media library (currently offline pending my move to the country), which allows me to retrieve information on any media known to man given a keyword, or an ISBN if it happens to be a book. Second, AWS doesn’t require the use of SOAP. Personally, I find SOAP bloated and awkward. Instead, Amazon provides a rich URL-based API that returns XML.
For example, if I wanted to search for Star Trek DVD’s, I would use this URL:
http://webservices.amazon.com/onca/xml?
Service=AWSECommerceService&SubscriptionId=1NW536V6NDVV1P4F0V82
&Operation=ItemSearch&Keywords=star+trek&SearchIndex=DVD&Sort=
The SubscriptionId is provided after registering… all the other parameters are fairly self explanatory.
Another reason I like URL-based API’s is that Java makes it very easy to parse the XML. This method loads the XML from the URL and returns a Document:
private static Document getDocument(String url)
throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(url);
}
Once you have the Document, it’s easy to read data using DOM. I’m an XPath man myself:
public static void searchDVDs(String keywords) {
try {
String url = "http://webservices.amazon.com/onca/xml" +
"?Service=AWSECommerceService" +
"&SubscriptionId=" + SUBSCRIPTION_ID +
"&Operation=ItemSearch&Keywords=" + keywords +
"&SearchIndex=DVD" +
"&Sort=";
System.out.println("Searching DVD's: " + keywords);
Document document = getDocument(url);
NodeList titles = XPathAPI.selectNodeList(document, "//Title");
for (int x = 0; x < titles.getLength(); x++) {
System.out.println(
titles.item(x).getFirstChild().getNodeValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
The latest enhancement is a queue service, which uses the same URL-based API approach or SOAP if you’re so inclined. I’m not sure what Amazon hopes to achieve (in other words make money) by offering a general purpose queue, but its darned cool.
I thought about creating a JMS driver around Amazon’s queue, but didn’t want to wade into the JNDI muck. Instead I created a quick Queue class that can create, delete, read and write as well as a QueueListener class that could kick off processes when a message is put on the queue (similar to JMS’s onMessage functionality). Check out the code here:
http://blogs.parivedasolutions.com/nzumwalt/articles/168.aspx
For more information on AWS, Amazon provides detailed API documentation as well as sample code on their website:
http://www.amazon.com/gp/aws/landing.html
posted on Wednesday, December 01, 2004 6:56 PM