Jeff has posted 11 posts at DZone. View Full User Profile

Getting Started with "Software as a Service"

06.10.2008
| 31003 views |
  • submit to reddit
Example: Amazon S3 Buckets Service

Amazon S3 is a web-based storage service that can be used to store and retrieve any amount of data. The underlying infrastructure is the same that Amazon uses for its own network of web sites. Developers can access Amazon S3 through a simple RESTful web service interface. They can upload, download and delete objects of up to 5 GB and store an unlimited number of such objects. The default download protocol is HTTP, but BitTorrent is also supported. Authentication is provided by a Header Signing mechanism, which involves a public key and a secret key. The Amazon account owner can make individual objects public or private and grant rights to individual users.

To use Amazon S3 Buckets, go to http://www.amazon.com/gp/browse.html?node=16427261 and sign up for an access key and a secret key. You'll have to pay for this by credit card but it is not expensive.

In our example, we use the Amazon S3 Buckets Service to store and retrieve the Creek.jpg photo. First, we create a Web Application project. In that project, we create three servlets, CreateBucketServlet, GetPhotoServlet, and PutPhotoServlet. In each servlet, we delete the method body of the processRequest(request, response) method. Then in the Services tab, we go to the Amazon -> Simple Storage Service -> S3 Bucket Service and expand {bucket}.s3.amazonaws.com -> {object}/

Under the {bucket} node, we drag and drop createBucket into the empty processRequest method body of the CreateBucketServlet. A dialog opens in which we set the default name of the bucket. The bucket name must be globally unique and in lower case. You may want to prepend your name or company name, such as org-netbeans-photobucket, to insure uniqueness. If you use a non-unique bucket name, you will receive an "Access Denied" error message in your server log. (The name "myphotobucket" in the example will not work, because we created a bucket with that name in the process of writing this article!)

When we click OK, the following code is generated in the CreateBucketServlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String bucket = "myphotobucket";

RestResponse result = S3BucketsService.createBucket(bucket, contentType, content);
//TODO - Uncomment the print Statement below to print result.
//out.println("The SaasService returned: "+result.getDataAsString());
} catch (Exception ex) {
ex.printStackTrace();
}

}

The IDE generates the following classes in the project:

  • RestConnection – A wrapper around HttpUrlConnection

  • RestResponse – A wrapper for the HTTP response

  • AmazonS3BucketsServiceAuthenticator – A class that signs the Amazon header

  • amazons3bucketsserviceauthenticator.properties – A properties file that stores the access key and secret key

  • AmazonS3BucketsService – A service wrapper containing the wrapper methods that uses RestConnection to make calls to the Amazon S3 service.

Next, under the {object}/ node, we drag and drop the putObject method into the empty processRequest method
body of the PutPhotoServlet. A dialog opens in which we can specify the type of content we're passing along with the bucket and object names.

When we click OK, the IDE inserts the RestResponse.putObject method into the S3BucketsService, and the
following code into the processRequest method body of PutPhotoServlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String bucket = "myphotobucket";
String object = "Creek.jpg";
String contentType = "image/jpeg";
java.io.InputStream=null;

RestResponse result = AmazonS3BucketsService.putObject(bucket, object, contentType, content);
//TODO - Uncomment the print Statement below to print result.
//out.println("The SaasService returned: "+result.getDataAsString());
} catch (Exception ex) {
ex.printStackTrace();
}
}

In order for the servlet to put our Creek.jpg file in the bucket, we replace java.io.InputStream=null; with the following code:

// Load the Creek.jpg image to be stored on Amazon S3.
java.io.InputStream content = getServletContext().getResourceAsStream(object);

Next we drag and drop the getObject method into the empty processRequest method body of the GetPhotoServlet. The following dialog opens, in which we specify the bucket and object names.

When we click OK, the IDE inserts the RestResponse.getObject method into the S3BucketsService, and the
following code into the processRequest method body of the GetPhotoServlet. We have manually added some additional code, lines 12-15 below, to set the content type for the response and write the return image data into the response’s output stream. We also added an import statement for java.io.OutputStream.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String bucket = "myphotobucket";
String object = "Creek.jpg";

RestResponse result = AmazonS3BucketsService.getObject(bucket, object);
//TODO - Uncomment the print Statement below to print result.
//out.println("The SaasService returned: "+result.getDataAsString());

// Add this to set the content type for the response and
// write the return image data into the response’s output stream.
response.setContentType(result.getContentType());
OutputStream os = response.getOutputStream();
os.write(result.getDataAsByteArray());
os.close();

} catch (Exception ex) {
ex.printStackTrace();
}
}

Now we need to make some changes to the project so that it will run and will put a picture in the Amazon bucket:

  • Put our Amazon S3 public key and secret key in the amazons3bucketsserviceauthenticator.properties file.

  • Find a nice picture and save it in the Web Pages folder. We have one called Creek.jpg, below:

  • In each servlet's processRequest method body, make sure that the bucket and object values correspond
    to our bucket name and photo file and that the content type, if present, is image/jpeg.

Now we can deploy and run the project. Run each servlet by right-clicking on the servlet's file node and selecting Run File from the context menu. First we run CreateBucketServlet, then PutPhotoServlet, then GetPhotoServlet. At the end, our picture appears in a browser window. Success!

Summary

This article is an introduction to "Software as a Service" and the Web Services Manager, which is a new interface for using SaaS functionality in your NetBeans IDE projects. We explained what Software as a Service is and gave you a brief tour of the Web Service Manager and its functionality. You could also read a description of the authentication mechanisms that the Web Service Manager supports. Lastly, we provided an example of a Web Application project using the Amazon S3 Buckets Service and three servlets to create a bucket for storing photos, save a photo to the bucket, and get a photo back from the bucket.

AttachmentSize
jeff2.png6.23 KB
jeff4.jpg100 KB
jeff5.png15.82 KB
jeff7.png10.76 KB
jeff1.png6.64 KB
jeff3.png6.36 KB
jeff6.png13.44 KB
Published at DZone with permission of its author, Jeff Rubinoff.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)