Permalink
Browse files

still broken - need messagebodywriter for json

  • Loading branch information...
1 parent f2f504c commit 82d7a563b0cf319a483c4361e0b8dc4dde014a4b @feliperazeek feliperazeek committed Feb 7, 2011
Showing with 116 additions and 15 deletions.
  1. +24 −2 pom.xml
  2. +92 −13 src/main/scala/resources.scala
View
@@ -102,6 +102,7 @@
</dependency>
<!-- GAE libraries for local testing as described here: http://code.google.com/appengine/docs/java/howto/unittesting.html -->
+ <!--
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-labs</artifactId>
@@ -115,20 +116,22 @@
<version>${gae.version}</version>
<scope>test</scope>
</dependency>
+ -->
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>2.2.2</version>
</dependency>
+<!--
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${gae.version}</version>
<scope>test</scope>
</dependency>
-
+-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
@@ -158,7 +161,18 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
-
+ <dependency>
+ <groupId>com.sun.jersey.contribs</groupId>
+ <artifactId>jersey-scala</artifactId>
+ <version>${jersey-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>net.liftweb</groupId>
+ <artifactId>lift-json</artifactId>
+ <version>1.1-M5</version>
+ </dependency>
+
+<!--
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
@@ -169,11 +183,14 @@
<artifactId>datanucleus-appengine</artifactId>
<version>1.0.4.1</version>
</dependency>
+ -->
+ <!--
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-jpa</artifactId>
<version>${datanucleus.version}</version>
</dependency>
+ -->
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
@@ -244,6 +261,11 @@
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.jackson</groupId>
+ <artifactId>jackson-jaxrs</artifactId>
+ <version>1.6.4</version>
+ </dependency>
<!-- testing -->
<dependency>
@@ -6,23 +6,35 @@ import java.util._
import java.lang.{Long => JLong}
import javax.persistence.Id
import com.googlecode.objectify.annotation.Cached
+import java.lang.reflect._
+import java.io.OutputStream
+import java.lang.annotation.Annotation
+import java.lang.{String, Class}
+import javax.ws.rs.core.{MultivaluedMap, MediaType}
+import javax.ws.rs.ext.{MessageBodyWriter, Provider}
+import java.lang.reflect.Type
+import com.sun.jersey.core.provider._
+import scala.xml.Node
+import scala.xml.PrettyPrinter
+import java.io._
+import scala.reflect.BeanProperty
package net.activedatatech.vendor.data {
trait Contact {
- var workPhone: String = _
- var mobilePhone: String = _
- var fax: String = _
- var email: String = _
- var address: String = _
+ @BeanProperty var workPhone: String = _
+ @BeanProperty var mobilePhone: String = _
+ @BeanProperty var fax: String = _
+ @BeanProperty var email: String = _
+ @BeanProperty var address: String = _
}
class Vendor extends AnyRef with Contact {
- @Id var id: String = _
+ @Id @BeanProperty var id: String = _
}
@@ -36,23 +48,90 @@ package net.activedatatech.vendor.data {
package net.activedatatech.vendor.rest {
+ import net.activedatatech.vendor.data._
+
@Provider
- @Path("/vendor")
+ @Path("/vendors")
class VendorService {
@GET
- @Path("/hello.json")
- @Produces(Array("application/json"))
- def getMediaType = "hello"
+ @Path("/blank.json")
+ @Produces(scala.Array("application/json"))
+ def blankJSON: Vendor = { new Vendor }
@GET
- @Path("/hello.xml")
- @Produces(Array("text/json"))
- def getHTML = "hello"
+ @Path("/blank.xml")
+ @Produces(scala.Array("text/json"))
+ def blankXML: Vendor = { new Vendor }
+
}
}
+package net.activedatatech.vendor.rest.providers {
+
+import net.activedatatech.vendor.data._
+import scala.reflect.Manifest
+import java.io.{InputStream, OutputStream}
+import java.lang.annotation.Annotation
+import java.lang.reflect.Type
+import javax.ws.rs.{WebApplicationException, Produces, Consumes}
+import javax.ws.rs.core.Response.Status
+import javax.ws.rs.core.{Response, MultivaluedMap, MediaType}
+import com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider
+import javax.ws.rs.ext.Provider
+import org.slf4j.LoggerFactory
+
+import org.codehaus.jackson.map.{MappingJsonFactory, ObjectMapper}
+import org.codehaus.jackson.{JsonGenerator, JsonParser => JacksonParser}
+
+@Provider
+@Produces(scala.Array(MediaType.APPLICATION_JSON))
+@Consumes(scala.Array(MediaType.APPLICATION_JSON))
+class JsonCaseClassProvider extends AbstractMessageReaderWriterProvider[Vendor] {
+ private val logger = LoggerFactory.getLogger(classOf[JsonCaseClassProvider])
+
+ def writeTo(json: Vendor, t: Class[_], genericType: Type, annotations: scala.Array[Annotation],
+ mediaType: MediaType, httpHeaders: MultivaluedMap[String, AnyRef],
+ entityStream: OutputStream) {
+ try {
+ "Some stuff"
+ } catch {
+ case e => logger.error("Error encoding %s as JSON".format(json), e)
+ }
+ }
+ def isWriteable(t: Class[_], genericType: Type, annotations: scala.Array[Annotation],
+ mediaType: MediaType) =
+ mediaType == MediaType.APPLICATION_JSON_TYPE &&
+ classOf[Vendor].isAssignableFrom(t)
+
+ def readFrom(t: Class[Vendor], genericType: Type,
+ annotations: scala.Array[Annotation], mediaType: MediaType,
+ httpHeaders: MultivaluedMap[String, String],
+ entityStream: InputStream): Vendor = {
+ try {
+ // Json.parse(entityStream)(Manifest.classType(t))
+ null
+ } catch {
+ case e: Exception => {
+ throw new WebApplicationException(Response.status(Status.BAD_REQUEST)
+ .entity(e.getMessage)
+ .build)
+ }
+ case e => {
+ logger.error("Error decoding JSON request entity", e)
+ throw e
+ }
+ }
+ }
+
+ def isReadable(t: Class[_], genericType: Type, annotations: scala.Array[Annotation],
+ mediaType: MediaType): Boolean =
+ mediaType == MediaType.APPLICATION_JSON_TYPE &&
+ classOf[Vendor].isAssignableFrom(t)
+}
+
+}

0 comments on commit 82d7a56

Please sign in to comment.