[ Team LiB ] Previous Section Next Section

Recipe 27.5 Getting Set Up with Amazon's Web Services API


You want to connect to Amazon Web Services (AWS) with a servlet or JSP.


Download the Amazon Web Services SDK, acquire an Amazon developer's token, and create a Java-SOAP package for interacting with AWS.


The process for setting up AWS goes like this:

  1. Download the AWS SDK at http://www.amazon.com/gp/aws/download_sdk.html/002-2688331-0628046. This kit.zip file includes several code samples and web services API documentation in HTML format.

  2. Acquire a developer's token from: http://associates.amazon.com/exec/panama/associates/join/developer/application.html/002-2688331-0628046. Similar to the license key you use with Google's Web APIs, the free-of-charge token comprises a series of encoded characters that must accompany each interaction between your Java code and AWS.

  3. Develop the Java API for making SOAP requests to AWS. The end result is a JAR file containing the classes that your servlets or JSPs use to make SOAP requests. The rest of this recipe describes how to generate this JAR file, because it is a multistep process.

Interacting with AWS using SOAP messages is one option that Amazon makes available to developers. Another one involves encoding the web services requests in URLs, and thereby making AWS requests via HTTP (called "XML over HTTP"). Recipe 27-7 shows an example of this URL search (they are useful for debugging your SOAP applications). If you store an XSLT file on the Web, AWS uses this file to format the response to XML-over-HTTP requests. See the SDK documentation for more details.

SOAP with Apache Axis

The creation of a Java-SOAP API for using AWS begins with downloading an open source SOAP toolkit named Apache Axis (http://ws.apache.org/axis/). Here are the steps involved in creating the API:

  1. Download Axis and extract the Axis ZIP file to the directory of your choice (this creates a directory named axis-1_1).

  2. Inside the axis-1_1/lib directory are several JAR files. Place these JAR files on your classpath and then run a program named org.apache.axis.wsdl.WSDL2Java to generate Java source files. These Java source files comprise the Java API you will use with AWS when you compile the files.

  3. Download the Web Services Description Language (WSDL) file associated with the Amazon Web Services. At this writing, the file can be found at: http://soap.amazon.com/schemas3/AmazonWebServices.wsdl.

  4. The following command line generates the com.amazon.soap.axis package for your Java API. The command lines in this recipe work on both Windows- and Unix-based machines. The command line is designed to refer to the AmazonWebServices.wsdl file in the current directory. The WSDL2Java program generates Java classes based on the XML elements described by the WSDL XML file (XML-to-Java conversion). This allows you to work with AWS using only Java objects, which is very nice—it's why you are enduring the initial pain of creating these Java classes! Break up this command line into separate lines to make it more readable, but when you actually run it, the commands must all be combined on one line:

    java -cp .;lib/axis.jar;lib/commons-discovery.jar;lib/commons-
        org.apache.axis.wsdl.WSDL2Java AmazonWebServices.wsdl --verbose 
          --package com.amazon.soap.axis
  5. This command line generates Java source files in a directory tree that matches the specified package name (com.amazon.soap.axis). Now you have to compile these classes with the javac tool, as in the following command line (the current directory contains the com directory). Once again, we break up this single-line command into separate lines just for the sake of readability (you have to run the command line unbroken by any newline characters):

    javac -classpath  .;lib/axis.jar;lib/commons-discovery.jar;lib/commons-
  6. Now JAR up all these files. In the same directory containing the top-level com directory, this command creates a JAR file named amazonapi.jar, which is just a name I created for it:

    jar cvf amazonapi.jar ./com
  7. Take the amazonapi.jar (or whatever you've named the JAR file) and place it in WEB-INF/lib. There's one more step left.

  8. Make sure that the JAR files or libraries that the com.amazon.soap.axis package depends on are also available to the web application. The amazonapi.jar file depends on the same Axis libraries that you added to the class path in the prior java and javac command-line sequences. You have to add these JARs to WEB-INF/lib as well (unless your application server makes all of these libraries generally available to web applications).

Okay, now for the fun part, where your Java code gets to explore books and other stuff at Amazon using servlets. Your servlets should use the com.amazon.soap.axis package for this purpose.

See Also

The AWS SDK http://www.amazon.com/gp/aws/download_sdk.html/002-2688331-0628046; Apache Axis: http://ws.apache.org/axis/; the Amazon Web Services WSDL file: http://soap.amazon.com/schemas3/AmazonWebServices.wsdl.

    [ Team LiB ] Previous Section Next Section