Creating a Web Service

In this homework, you will learn a simple way to construct REST style Web Services. This project will also introduce you to a new language called groovy, a Java-based scripting language, in which you will develop a simple mashup. The mashup you will be creating will take data about automobile recalls and mash it together with the Google chart service.

Prerequisites

Part 1: Get Familiar with Project Zero

First, walk through the tutorial to become familiar with the basic concepts.

Import the Project

Since this project requires database access we have provided you with a pre-configured eclipse project with the necessary JDBC jars and some helper classes. To import this project into your workspace use the following steps.
  1. Download the zipped version of the project here.
  2. Unzip the project on your machine (NOT into the eclipse workspace).
  3. Open eclipse and go to File => Import which will open a wizard.
  4. In the wizard, select General => Existing Projects into Workspace and then click next.
  5. Ensure the "select root directory" option is selected and click browse.
  6. Browse to and select the location of the directory that was created as a result of unzipping and click choose.
  7. Make sure the checkbox "Copy projects into workspace" is checked.
  8. Click finish and the project should now be in your workspace.
  9. Rename the project to SOC_Service_[unityID].
You will find that there are three java classes in the soc.recalls package and one groovy file in the app/resources folder. These should be the only files that you need and the remainder of the assignment will involve extending these files to develop your service.

Part 2: Setup the Service

Now it's time to create the service. The service will allow you to query for automobile recalls in a RESTful manner. The service endpoint will be http://localhost:8080/resources/Recalls (this is case-sensitive). The skeleton of the Web service is already created, as app/resources/Recalls.groovy, but it is up to you to complete it. For starters you must do the following...
  1. Open the app/resources/Recalls.groovy file. You may want to read up on the groovy language if you haven't seen it before. You can write standard Java in a groovy file, but it also allows for other operations that are not allowed in a normal Java class.
  2. You will need to be able to read in parameters from the request. The params that you need to support are the strings "make", "model", and "year". For instance, a valid invocation of the service might be http://localhost:8080/resources/Recalls?make=ford&model=mustang&year=2005, in which your service will return all recalls for 2005 Ford Mustangs.
  3. After reading in the parameters, you will need to query the database for results. To assist you in querying that data you are provided with a helper class soc.recalls.RecallSearch, which contains a static search method that can be called by your service. This method queries an Apache Derby database with actual recall data (located in the RecallsDB folder of your eclipse project).
  4. Put the recall objects into the soc.recalls.RecallResponse object.
  5. Render the RecallResponse object as XML. You will need to reference the Project Zero documentation for this, as they allow your service to output plain text, XML, and JSON.
  6. You should test your service using many different query combinations and then make sure you handle any exceptions you encounter.
NOTE: Don't worry about deploying the project outside of eclipse. To deploy you can right click on the project folder in eclipse and select Run As... => Project Zero Application.

Part 3: Google Charts

Google offers a REST service that generates charts (images) based on information supplied by a requester. You will utilize this service to create graphs that will be included in your response. Become familiar with Google charts API before attempting to generate the REST calls programatically.

You will generate 2 charts and include the URLs in the RecallResponse object, which will get translated into XML. Both charts will be pie charts and should contain only 6 slices. If more than 6 possible entries exist, choose the largest 5 entries and use the 6th entry as an OTHER entry that aggregates all entries that are not in the largest 5.
  1. Chart 1: Manufacturer's Chart
  2. Chart 2: Year Chart
Each chart must have a label in the RecallResponse object since it is a map, so label them "manufacturer" and "year," respectively. The output of your service should look very similar to this xml snippet.
<recallresponse>
	<chartURLs>
		<year>http://chart.apis.google.com/chart?...</year>
		...
	</chartURLs>
	<recalls>
		<item index="0">
			...
			<make>NISSAN</make>
			...
			<model>XTERRA</model>
			<modelYear>2002</modelYear>
			...
		</item>
	</recalls>
</recallresponse>

Deliverables