# HG changeset patch # User ymh # Date 1366747078 -7200 # Node ID 12a9c423bf6f55f340d5d7c5c661347e5cbe7502 # Parent e8287f25f6bb6c37026e25963fdfa9f1e2fd9630 Add jsonp to get of single renkan resources. diff -r e8287f25f6bb -r 12a9c423bf6f .classpath --- a/.classpath Mon Apr 08 01:21:24 2013 +0200 +++ b/.classpath Tue Apr 23 21:57:58 2013 +0200 @@ -55,13 +55,13 @@ - + - + @@ -229,25 +229,25 @@ - + - + - + - + @@ -261,5 +261,6 @@ + diff -r e8287f25f6bb -r 12a9c423bf6f server/pom.xml --- a/server/pom.xml Mon Apr 08 01:21:24 2013 +0200 +++ b/server/pom.xml Tue Apr 23 21:57:58 2013 +0200 @@ -4,14 +4,14 @@ 4.0.0 org.iri_research.renkan renkan - 0.6.2 + 0.6.3 war 2.14 1.0 1.0 - 1.17 + 1.17.1 3.2.1.RELEASE 3.1.3.RELEASE 8.1.10.v20130312 @@ -198,6 +198,11 @@ ${jersey-version} + com.sun.jersey + jersey-json + ${jersey-version} + + com.sun.jersey.contribs jersey-spring ${jersey-version} diff -r e8287f25f6bb -r 12a9c423bf6f server/src/main/java/org/iri_research/renkan/Constants.java --- a/server/src/main/java/org/iri_research/renkan/Constants.java Mon Apr 08 01:21:24 2013 +0200 +++ b/server/src/main/java/org/iri_research/renkan/Constants.java Tue Apr 23 21:57:58 2013 +0200 @@ -20,7 +20,7 @@ { add("0"); add("6"); - add("2"); + add("3"); add("final"); add("0"); } diff -r e8287f25f6bb -r 12a9c423bf6f server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java --- a/server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java Mon Apr 08 01:21:24 2013 +0200 +++ b/server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java Tue Apr 23 21:57:58 2013 +0200 @@ -6,12 +6,14 @@ import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -31,6 +33,7 @@ import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; +import com.sun.jersey.api.json.JSONWithPadding; public abstract class RenkanResource, ID extends Serializable> { @@ -57,8 +60,10 @@ @GET @Path("{id : [a-zA-Z\\-0-9]+}") - @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") - public T getObject(@PathParam("id") ID objectId) { + @Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8", "application/javascript", + "application/x-javascript", "text/ecmascript", + "application/ecmascript", "text/jscript"}) + public JSONWithPadding getObject(@PathParam("id") ID objectId, @QueryParam("callback") @DefaultValue("callback") String callback) { this.logger.debug("GetObject: " + objectId); @@ -67,8 +72,8 @@ if (null == obj) { throw new WebApplicationException(Status.NOT_FOUND); } - - return obj; + + return new JSONWithPadding(obj, callback); } @DELETE diff -r e8287f25f6bb -r 12a9c423bf6f server/src/main/webapp/WEB-INF/web.xml --- a/server/src/main/webapp/WEB-INF/web.xml Mon Apr 08 01:21:24 2013 +0200 +++ b/server/src/main/webapp/WEB-INF/web.xml Tue Apr 23 21:57:58 2013 +0200 @@ -70,7 +70,7 @@ com.sun.jersey.spi.spring.container.servlet.SpringServlet com.sun.jersey.config.property.packages - org.iri_research.renkan.rest;com.fasterxml.jackson.jaxrs + org.iri_research.renkan.rest com.sun.jersey.api.json.POJOMappingFeature diff -r e8287f25f6bb -r 12a9c423bf6f server/src/test/java/org/iri_research/renkan/test/rest/ProjectRestTest.java --- a/server/src/test/java/org/iri_research/renkan/test/rest/ProjectRestTest.java Mon Apr 08 01:21:24 2013 +0200 +++ b/server/src/test/java/org/iri_research/renkan/test/rest/ProjectRestTest.java Tue Apr 23 21:57:58 2013 +0200 @@ -78,7 +78,7 @@ return new WebAppDescriptor.Builder("org.iri_research.renkan.rest") .contextPath("rest") .contextParam("contextConfigLocation", "classpath:/org/iri_research/renkan/test/rest/rest-context.xml") - .initParam("com.sun.jersey.config.property.packages", "org.iri_research.renkan.rest;com.fasterxml.jackson.jaxrs") + .initParam("com.sun.jersey.config.property.packages", "org.iri_research.renkan.rest") .servletClass(SpringServlet.class) .contextListenerClass(ContextLoaderListener.class) .build(); diff -r e8287f25f6bb -r 12a9c423bf6f server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java --- a/server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java Mon Apr 08 01:21:24 2013 +0200 +++ b/server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java Tue Apr 23 21:57:58 2013 +0200 @@ -61,7 +61,7 @@ return new WebAppDescriptor.Builder("org.iri_research.renkan.rest") .contextPath("rest") .contextParam("contextConfigLocation", "classpath:/org/iri_research/renkan/test/rest/rest-context.xml") - .initParam("com.sun.jersey.config.property.packages", "org.iri_research.renkan.rest;com.fasterxml.jackson.jaxrs") + .initParam("com.sun.jersey.config.property.packages", "org.iri_research.renkan.rest") .servletClass(SpringServlet.class) .contextListenerClass(ContextLoaderListener.class) .build(); @@ -289,7 +289,7 @@ Space space = this.spacesList.get(this.firstSpaceUUID); WebResource webResource = this.resource(); - String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class); + String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class); logger.debug("RESPONSE : " + respString); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(respString); @@ -718,4 +718,37 @@ Assert.assertNull("Space object must be in rep", sp); } + + @Test + public void testSingleGetJsonp() throws JsonProcessingException, IOException { + WebResource webResource = this.resource(); + ClientResponse resp = webResource.path("spaces").path(this.firstSpaceUUID).queryParam("callback", "callback_func").accept("application/javascript").get(ClientResponse.class); + Assert.assertEquals("return type must be application/javascript","application/javascript", resp.getType().toString()); + + String respStr = resp.getEntity(String.class); + + Assert.assertTrue("resp must start with callback call", respStr.startsWith("callback_func(")); + Assert.assertTrue("resp must start with callback call", respStr.endsWith(")")); + + String jsonStr = respStr.substring("callback_func(".length(), respStr.length()-1); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(jsonStr); + + Space space = this.spacesList.get(this.firstSpaceUUID); + + String id = jsonNode.get("id").asText(); + Assert.assertEquals("id must be equals", space.getId(), id); + + Assert.assertTrue("the space must have a created", jsonNode.hasNonNull("created")); + + String dateStr = jsonNode.get("created").asText(); + + DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); + DateTime date = fmt.parseDateTime(dateStr); + + Assert.assertEquals("Created date must be the same", space.getCreated(), date.toDate()); + + Assert.assertEquals("title must be the same", space.getTitle(), jsonNode.get("title").asText()); + + } }