Google App Engine Java + Restlet + Zillow

I’ve written a simple tiny test application to test out Restlet with Zillow web services on Google App Engine Java platform. You can test the application using this url:

There are three main Java files in this application. I’ll briefly describe their main roles without getting into detail. Source code for all three java files along with web.xml file is attached for viewers. basically routes a URL to invoke ZillowResource java class. This is the main application entry point which extends org.restlet.Application. The routing uri I constructed may not be adherring to true REST style here. I’m learning REST and Restlet so please pardon my weak REST URI definition. More information about Restlet + GAE/J can be found here.

<pre>package com.latifrealtor;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import com.latifrealtor.zillow.ZillowResource;

public class RestletMain extends Application {

	 * Creates a root Restlet that will receive all incoming calls.
	public synchronized Restlet createRoot() {
		Router router = new Router(getContext());
		router.attach("/zillow/searchresults/{zip}/{state}/{city}/{street}", ZillowResource.class);
		return router;

This java class makes call to Zillow’s REST based Zestimate web services call and retrieves house valuation estimates. This class is also parsing URL to extract out address information that is needed to make a successful Zillow call. Response from Zillow web services is parsed using a helper class called “” and returned to any HTTP Client as JSON string.

package com.latifrealtor.zillow;

import java.util.logging.Logger;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class ZillowResource extends ServerResource {
	private static final Logger log = Logger.getLogger(ZillowResource.class.getName());

	public String getProcessor() {
		try {
			String street = this.getRequest().getAttributes().get("street").toString();
			String city = this.getRequest().getAttributes().get("city").toString();
			String state = this.getRequest().getAttributes().get("state").toString();
			String zip = this.getRequest().getAttributes().get("zip").toString();
			if(street.isEmpty() || city.isEmpty() || state.isEmpty() || zip.isEmpty()){
				throw new Exception("Street or City or State or Zip was not provided");
			String responseStream = getSearchResults(street, city+","+state+","+zip);
			String zestimate = new ZillowXMLParser().getEstimate(responseStream);
			return zestimate;
		} catch (Exception e) {
			return e.getMessage();

	public String getSearchResults(String pAddress, String pCityStateZip) {
		StringBuffer sbf = new StringBuffer();
		try {
			String zUrl = "";
			String zWS = "/GetSearchResults.htm?";
			String zwsid = "zws-id=" + "<< <em>YOUR ZILLOW API ID</em> >>";
			String address = "&address=" + (pAddress);
			String citystatezip = "&citystatezip=" + URLEncoder.encode(pCityStateZip, "UTF-8");

			String fullUrl = zUrl + zWS + zwsid + address + citystatezip;
			log.warning("Full Url : " + fullUrl);

			// String encodedURL = URLEncoder.encode(fullUrl, "UTF-8");
			URL url = new URL(fullUrl);

			try {
				// URL url = new URL("");
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(url.openStream()));
				String line;

				log.warning("Reading lines from reader.readLine()");
				while ((line = reader.readLine()) != null) {
				return sbf.toString();
			} catch (MalformedURLException e) {
				return (e.getMessage());
			} catch (IOException e) {
				return (e.getMessage());

		} catch (Exception e) {
			return (e.getMessage());

This is a helper class to parse XML response from Zillow. We ran into problem using javax.xml.parsers package and found a solution from Google App Engine Java community forum here.  This java class basically parse Zillow response and returns a JSON formatted string.

package com.latifrealtor.zillow;

import java.util.ArrayList;
import java.util.logging.Logger;

import org.apache.xerces.impl.xpath.XPath;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class ZillowXMLParser {
	private static final Logger log = Logger.getLogger(ZillowXMLParser.class.getName());

    public String getEstimate(String zillowXMLResponseStream)
            throws ParserConfigurationException, SAXException,
            IOException, XPathExpressionException, Exception {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();

        Document doc = null;
        try {
            if (!zillowXMLResponseStream.isEmpty()){ //check if respones is empty or not
                //Important to note:
                // if you use String on builder.parse(string) it will throw an error.
                InputStream is = new ByteArrayInputStream(zillowXMLResponseStream.getBytes("UTF-8"));
                doc = builder.parse(is);
            }else {
                throw new Exception("zillow response xml is empty");

            // GAEJ Bug fix:
            // We need to use full class path to instantiate XPathFactoryImpl(), and
            // we also needed xalan-j_2_7_0.jar JAR to be placed in WEB-INF/lib folder to make Xalan work in GAE/J
            XPathFactory factory = new org.apache.xpath.jaxp.XPathFactoryImpl();

            XPathExpression expr = factory.newXPath().compile("//zestimate/amount/text()");
            String zestimateDollarAmount = (String) expr.evaluate(doc, XPathConstants.STRING);

            XPathExpression exprLastUpdated = factory.newXPath().compile("//zestimate/last-updated/text()");
            String zestimateLastUpdated = (String) exprLastUpdated.evaluate(doc, XPathConstants.STRING);

            XPathExpression exprValueLow = factory.newXPath().compile("//zestimate/valuationRange/low/text()");
            String zestimateValueLow = (String) exprValueLow.evaluate(doc, XPathConstants.STRING);

            XPathExpression exprValueHigh = factory.newXPath().compile("//zestimate/valuationRange/high/text()");
            String zestimateValueHigh = (String) exprValueHigh.evaluate(doc, XPathConstants.STRING);

            String jsonReturn = "{"zestimateDollarAmount" : "" + zestimateDollarAmount +"" , " + ""zestimateLastUpdated" : "" + zestimateLastUpdated + """
        	+ ", "zestimateValueLow" : "" + zestimateValueLow +"" , " + ""zestimateValueHigh" : "" + zestimateValueHigh + """
        	+ "}";
            return jsonReturn;
        } catch (IllegalArgumentException iae) {
            throw new Exception(iae);
        } catch (Exception e){
        	log.warning(  e.getMessage());
            throw new Exception("Error at FlickrImageBuilder " , e);


I’m attaching web.xml to provide help viewer configure their GAE/J web application to work properly with Restlet.

<?xml version="1.0" encoding="utf-8"?>
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

<web-app xmlns="" version="2.5">
<display-name>first steps servlet</display-name>
	<display-name>first steps servlet</display-name>
	<!-- Application class name -->

	<!-- Restlet adapter -->
	<!-- Catch all requests -->



One Comment

  1. JhinSeok Lee June 17, 2011 Reply

Add a Comment

Your email address will not be published. Required fields are marked *