Permalink
Please sign in to comment.
Showing
with
405 additions
and 22 deletions.
- +0 −1 src/main/java/org/geo/spatialsearch/model/GeoBaseEntity.java
- +18 −0 src/main/java/org/geo/spatialsearch/rest/api/CensusResource.java
- +96 −0 src/main/java/org/geo/spatialsearch/rest/api/impl/CensusResourceImpl.java
- +20 −0 src/main/java/org/geo/spatialsearch/rest/exceptions/UnsupportedFormatException.java
- +83 −1 src/main/java/org/geo/spatialsearch/service/impl/CensusServiceImpl.java
- +32 −0 src/main/java/org/geo/spatialsearch/util/ExceptionHandler.java
- +32 −0 src/main/java/org/geo/spatialsearch/util/RestFormatUtil.java
- +1 −1 src/main/webapp/WEB-INF/web.xml
- +44 −0 src/test/java/org/geo/spatialsearch/rest/integration/CensusResourceTest.java
- +26 −0 src/test/java/org/geo/spatialsearch/rest/integration/RestfulTest.java
- +11 −0 src/test/java/org/geo/spatialsearch/test/IntegrationTestSuite.java
- +11 −0 src/test/java/org/geo/spatialsearch/test/ServiceTestSuite.java
- +10 −0 src/test/java/org/geo/spatialsearch/test/TestSuite.java
- +21 −19 src/test/resources/geo/mappings/State2010.hbm.xml
@@ -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"); | ||
+ } | ||
+ | ||
+} |
@@ -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(); | ||
+ } | ||
+ } | ||
+} |
@@ -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