Add jsonp to get of single renkan resources.
authorymh <ymh.work@gmail.com>
Tue, 23 Apr 2013 21:57:58 +0200
changeset 165 12a9c423bf6f
parent 151 e8287f25f6bb
child 166 fc9ee87424c0
Add jsonp to get of single renkan resources.
.classpath
server/pom.xml
server/src/main/java/org/iri_research/renkan/Constants.java
server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java
server/src/main/webapp/WEB-INF/web.xml
server/src/test/java/org/iri_research/renkan/test/rest/ProjectRestTest.java
server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java
--- a/.classpath	Mon Apr 08 01:21:24 2013 +0200
+++ b/.classpath	Tue Apr 23 21:57:58 2013 +0200
@@ -55,13 +55,13 @@
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-server/1.17/jersey-server-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-server/1.17/jersey-server-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-server/1.17.1/jersey-server-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-server/1.17/jersey-server-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/jersey-server/1.17/jersey-server-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-core/1.17/jersey-core-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-core/1.17/jersey-core-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-core/1.17.1/jersey-core-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-core/1.17/jersey-core-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/jersey-core/1.17/jersey-core-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
@@ -229,25 +229,25 @@
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-test-framework/jersey-test-framework-core/1.17/jersey-test-framework-core-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-test-framework/jersey-test-framework-core/1.17/jersey-test-framework-core-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-test-framework/jersey-test-framework-core/1.17.1/jersey-test-framework-core-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-test-framework/jersey-test-framework-core/1.17/jersey-test-framework-core-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/jersey-test-framework/jersey-test-framework-core/1.17/jersey-test-framework-core-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-servlet/1.17/jersey-servlet-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-servlet/1.17/jersey-servlet-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-servlet/1.17.1/jersey-servlet-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-servlet/1.17/jersey-servlet-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/jersey-servlet/1.17/jersey-servlet-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/contribs/jersey-spring/1.17/jersey-spring-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/contribs/jersey-spring/1.17/jersey-spring-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/contribs/jersey-spring/1.17.1/jersey-spring-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/contribs/jersey-spring/1.17/jersey-spring-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/contribs/jersey-spring/1.17/jersey-spring-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-client/1.17/jersey-client-1.17.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-client/1.17/jersey-client-1.17-sources.jar">
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-client/1.17.1/jersey-client-1.17.1.jar" sourcepath="M2_REPO/com/sun/jersey/jersey-client/1.17/jersey-client-1.17-sources.jar">
 		<attributes>
 			<attribute name="javadoc_location" value="jar:file:/Users/ymh/.m2/repository/com/sun/jersey/jersey-client/1.17/jersey-client-1.17-javadoc.jar!/"/>
 			<attribute name="org.eclipse.jst.component.nondependency" value=""/>
@@ -261,5 +261,6 @@
 	</classpathentry>
 	<classpathentry kind="var" path="M2_REPO/javax/validation/validation-api/1.1.0.CR2/validation-api-1.1.0.CR2.jar"/>
 	<classpathentry kind="var" path="M2_REPO/com/fasterxml/uuid/java-uuid-generator/3.1.3/java-uuid-generator-3.1.3.jar"/>
+	<classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-json/1.17.1/jersey-json-1.17.1.jar"/>
 	<classpathentry kind="output" path="server/target/classes"/>
 </classpath>
--- 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 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.iri_research.renkan</groupId>
     <artifactId>renkan</artifactId>
-    <version>0.6.2</version>
+    <version>0.6.3</version>
     <packaging>war</packaging>
 
     <properties>
         <surefire-version>2.14</surefire-version>
         <coweb-version>1.0</coweb-version>
         <cowebx-version>1.0</cowebx-version>
-        <jersey-version>1.17</jersey-version>
+        <jersey-version>1.17.1</jersey-version>
         <spring-version>3.2.1.RELEASE</spring-version>
         <spring-security-version>3.1.3.RELEASE</spring-security-version>
         <jetty-version>8.1.10.v20130312</jetty-version>
@@ -198,6 +198,11 @@
         	<version>${jersey-version}</version>
         </dependency>
         <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-json</artifactId>
+            <version>${jersey-version}</version>
+        </dependency>
+        <dependency>
             <groupId>com.sun.jersey.contribs</groupId>
             <artifactId>jersey-spring</artifactId>
             <version>${jersey-version}</version>
--- 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");
 		}
--- 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<T extends IRenkanModel<ID>, 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
--- 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 @@
     <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.iri_research.renkan.rest;com.fasterxml.jackson.jaxrs</param-value>
+      <param-value>org.iri_research.renkan.rest</param-value>
     </init-param>
     <init-param>
       <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
--- 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();
--- 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());
+		
+	}
 }