Permalink
Browse files

Add Census Resource REST endpoint

  • Loading branch information...
1 parent d2ac4e5 commit dcb63eacc19a762935f34e0134c9c006dd4139c0 @jmarin jmarin committed Sep 28, 2012
@@ -32,7 +32,6 @@
private Geometry geometry;
@XmlTransient
- @XmlElement
private Envelope envelope;
public Long getId() {
@@ -0,0 +1,18 @@
+package org.geo.spatialsearch.rest.api;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+public interface CensusResource {
+
+ public Response findByCoordinates(UriInfo uriInfo, String geography,
+ double latitude, double longitude, String format, String callback);
+
+ public Response findGeographyByName(UriInfo uriInfo, String geography,
+ String geographyName, Integer maxResults, Boolean isFullList,
+ String format, String callback);
+
+ public Response findGeographyByFips(UriInfo uriInfo, String geography,
+ String fips, String format, String callback);
+
+}
@@ -0,0 +1,96 @@
+package org.geo.spatialsearch.rest.api.impl;
+
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import org.geo.spatialsearch.census.model.CensusGeographyEnum;
+import org.geo.spatialsearch.census.rest.CensusLookupResponse;
+import org.geo.spatialsearch.rest.api.CensusResource;
+import org.geo.spatialsearch.service.CensusService;
+import org.geo.spatialsearch.util.ExceptionHandler;
+import org.geo.spatialsearch.util.RestFormatUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+/**
+ *
+ * @author Juan Marin
+ *
+ */
+
+@Path(value = "/census")
+@Component
+@Scope(value = "singleton")
+@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
+ "application/x-javascript" })
+public class CensusResourceImpl implements CensusResource {
+
+ @Autowired
+ @Qualifier("censusService")
+ private CensusService censusService;
+
+ @Autowired
+ @Qualifier("exceptionHandler")
+ private ExceptionHandler handler;
+
+ // private static final String CENSUS_BY_COORDINATES =
+ // "Census-findByCoordinates";
+ // private static final String CENSUS_BY_FIPSCODE =
+ // "Census-findGeographyByFips";
+ // private static final String CENSUS_BY_GEOGRAPHY_NAME =
+ // "Census-findGeographyByName";
+
+ @Override
+ @Path(value = "{geography}")
+ @GET
+ public Response findByCoordinates(@Context UriInfo uriInfo,
+ @PathParam(value = "geography") String geography,
+ @QueryParam(value = "latitude") double latitude,
+ @QueryParam(value = "longitude") double longitude,
+ @DefaultValue("xml") @QueryParam(value = "format") String format,
+ @QueryParam(value = "callback") String callback) {
+ CensusGeographyEnum geographyType = CensusGeographyEnum
+ .getGeographyTypeWithKey(geography);
+ CensusLookupResponse apiResponse = new CensusLookupResponse();
+ Exception exception = null;
+ try {
+ apiResponse = censusService.findByCoordinates(geographyType,
+ longitude, latitude);
+ } catch (Exception ex) {
+ handler.handle(apiResponse, ex);
+ exception = ex;
+ // System.out.println(ex.getStackTrace());
+ }
+ // APIStatsProfiler.captureStatistics(CENSUS_BY_COORDINATES,
+ // apiResponse, uriInfo, true, exception);
+ Response response = RestFormatUtil
+ .format(format, callback, apiResponse);
+ return response;
+ }
+
+ @Override
+ public Response findGeographyByName(UriInfo uriInfo, String geography,
+ String geographyName, Integer maxResults, Boolean isFullList,
+ String format, String callback) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Response findGeographyByFips(UriInfo uriInfo, String geography,
+ String fips, String format, String callback) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
@@ -0,0 +1,20 @@
+package org.geo.spatialsearch.rest.exceptions;
+
+import javax.ws.rs.QueryParam;
+
+import com.sun.jersey.api.ParamException;
+import com.sun.jersey.api.Responses;
+
+public class UnsupportedFormatException extends ParamException {
+
+ private static final long serialVersionUID = 1160004452288344472L;
+
+ /**
+ * Create a HTTP 415 (Unsupported Media Type) exception.
+ */
+ public UnsupportedFormatException() {
+ super(null, Responses.UNSUPPORTED_MEDIA_TYPE, QueryParam.class,
+ "format", "xml");
+ }
+
+}
@@ -60,12 +60,95 @@ public CensusLookupResponse findByCoordinates(
case STATE2010:
findStateByPoint(apiResponse, point);
break;
+ case COUNTY2010:
+ findCountyByPoint(apiResponse, point);
+ break;
+ case TRACT2010:
+ findTractByPoint(apiResponse, point);
+ break;
+ case BLOCK2010:
+ findBlockByPoint(apiResponse, point);
+ break;
+ case CONGRESSIONAL_DISTRICT:
+ findCongressionalDistrictByPoint(apiResponse, point);
+ break;
+ case MSA:
+ findMSAByPoint(apiResponse, point);
+ break;
+ case PLACE:
+ findPlaceByPoint(apiResponse, point);
+ break;
+ case STATE_HOUSE_DISTRICT:
+ findStateHouseDistrictByPoint(apiResponse, point);
+ break;
+ case STATE_SENATE_DISTRICT:
+ findStateSenateDistrictByPoint(apiResponse, point);
+ break;
+ case TRIBAL:
+ findTribalByPoint(apiResponse, point);
+ break;
+ case ALL:
+ findAllByPoint(apiResponse, point);
+ break;
}
apiResponse.setStatus(Status.OK.getReasonPhrase());
}
return apiResponse;
}
+ private void findAllByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findTribalByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findStateSenateDistrictByPoint(
+ CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findStateHouseDistrictByPoint(
+ CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findPlaceByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findMSAByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findCongressionalDistrictByPoint(
+ CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findBlockByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findTractByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
+ private void findCountyByPoint(CensusLookupResponse apiResponse, Point point) {
+ // TODO Auto-generated method stub
+
+ }
+
@Transactional(readOnly = true)
private void findStateByPoint(CensusLookupResponse apiResponse, Point point) {
final Criteria stateCriteria = state2010DAO.createCriteria();
@@ -79,7 +162,6 @@ private void findStateByPoint(CensusLookupResponse apiResponse, Point point) {
ValidationUtil.isEmptyResult(apiResponse, apiResponse
.getCensusLookupBaseResponse().getStates(),
Message.NO_STATE_RESULTS_FOUND, null);
-
}
}
@@ -0,0 +1,32 @@
+package org.geo.spatialsearch.util;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.List;
+
+import javax.ws.rs.core.Response.Status;
+
+import org.geo.spatialsearch.rest.APIResponse;
+
+public class ExceptionHandler {
+
+ public void handle(APIResponse apiResponse, Exception ex) {
+ apiResponse.setStatus(Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
+ List<String> messages = apiResponse.getMessageList();
+ messages.add("An error has occured");
+ StringBuffer buffer = new StringBuffer(
+ "There is a possibility of an data or coding issue. ");
+ buffer.append("So, please forward the below stack trace to the developer to look into it.\n\n");
+ buffer.append(getStackTrace(ex));
+ messages.add(buffer.toString());
+ }
+
+ public static String getStackTrace(Throwable aThrowable) {
+ final Writer result = new StringWriter();
+ final PrintWriter printWriter = new PrintWriter(result);
+ aThrowable.printStackTrace(printWriter);
+ return result.toString();
+ }
+
+}
@@ -0,0 +1,32 @@
+package org.geo.spatialsearch.util;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.geo.spatialsearch.rest.exceptions.UnsupportedFormatException;
+
+import com.sun.jersey.api.json.JSONWithPadding;
+
+public class RestFormatUtil {
+
+ public static <T> Response format(String format, String callback, T response) {
+ if (format != null) {
+ if (format.equals("json")) {
+ return Response.ok(response).type(MediaType.APPLICATION_JSON)
+ .build();
+ } else if (format.equals("xml")) {
+ return Response.ok(response).type(MediaType.APPLICATION_XML)
+ .build();
+ } else if (format.equals("jsonp")) {
+ return Response.ok()
+ .entity(new JSONWithPadding(response, callback))
+ .type("application/x-javascript").build();
+ } else {
+ throw new UnsupportedFormatException();
+ }
+ } else {
+ return Response.ok(response.toString())
+ .type(MediaType.APPLICATION_XML).build();
+ }
+ }
+}
@@ -20,7 +20,7 @@
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
- <param-value>org.geo.rest.api.impl;org.codehaus.jackson.jaxrs</param-value>
+ <param-value>org.geo.spatialsearch.rest.api.impl;org.codehaus.jackson.jaxrs</param-value>
</init-param>
</servlet>
<servlet-mapping>
@@ -0,0 +1,44 @@
+package org.geo.spatialsearch.rest.integration;
+
+import static junit.framework.Assert.assertEquals;
+
+import junit.framework.Assert;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+public class CensusResourceTest extends RestfulTest {
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Test
+ public void testFindStateByCoordinates() throws JSONException {
+ ClientResponse response = this.webResource.path("census").path("state")
+ .queryParam("latitude", "42.919")
+ .queryParam("longitude", "-75.2517")
+ .queryParam("format", "json").get(ClientResponse.class);
+ assertEquals(200, response.getStatus()); // 200 = OK
+
+ String json = response.getEntity(String.class);
+ JSONObject results = new JSONObject(json);
+ JSONObject jo = (JSONObject) results.get("Results");
+ JSONArray states = (JSONArray) jo.get("state");
+ Assert.assertFalse(states.isNull(0));
+ JSONObject state = (JSONObject) states.get(0);
+
+ assertEquals("36", state.get("fips"));
+ assertEquals("New York", state.get("name"));
+ assertEquals("NY", state.get("stateCode"));
+ assertEquals("STATE2000", state.get("geographyType"));
+ }
+
+}
@@ -0,0 +1,26 @@
+package org.geo.spatialsearch.rest.integration;
+
+import org.junit.After;
+import org.junit.Before;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+
+public class RestfulTest {
+
+ public static final String URI = "http://localhost:8081/spatialsearch";
+
+ private Client client;
+ protected WebResource webResource;
+
+ @Before
+ public void setUp() throws Exception {
+ client = new Client();
+ webResource = client.resource(RestfulTest.URI);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+}
Oops, something went wrong.

0 comments on commit dcb63ea

Please sign in to comment.