Skip to content
This repository
Browse code

allow for json to be cached in the body string

  • Loading branch information...
commit a9f092ca69295eb87a9bd7247ca3730bc552c46c 1 parent 347cf92
Ivan Porto Carrero authored October 07, 2012
11  core/src/test/scala/org/scalatra/ParamsTest.scala
@@ -2,6 +2,7 @@ package org.scalatra
2 2
 
3 3
 import java.util.NoSuchElementException
4 4
 import test.scalatest.ScalatraFunSuite
  5
+import io.Codec
5 6
 
6 7
 object ParamsTestServlet {
7 8
   val NoSuchElement = "No Such Element"
@@ -35,6 +36,10 @@ class ParamsTestServlet extends ScalatraServlet {
35 36
   get("/twoSymbols/:sym1/:sym2") {
36 37
     params('sym1)+" and "+ params('sym2)
37 38
   }
  39
+
  40
+  post("/read-body") {
  41
+    "body: " + request.body
  42
+  }
38 43
 }
39 44
 
40 45
 class ParamsTest extends ScalatraFunSuite {
@@ -82,4 +87,10 @@ class ParamsTest extends ScalatraFunSuite {
82 87
       body should equal ("hello and world")
83 88
     }
84 89
   }
  90
+
  91
+  test("can read the body of a post") {
  92
+    post("/read-body", "hi".getBytes(Codec.UTF8)) {
  93
+      body should equal ("body: hi")
  94
+    }
  95
+  }
85 96
 }
1  json/src/main/scala/org/scalatra/json/Jackson.scala
@@ -7,6 +7,7 @@ import org.json4s._
7 7
 trait JacksonJsonSupport extends JsonSupport[JValue] with JacksonJsonOutput {
8 8
   protected def readJsonFromStream(stream: InputStream): JValue = mapper.readValue(stream, classOf[JValue])
9 9
 
  10
+  protected def readJsonFromBody(bd: String): JValue = mapper.readValue(bd, classOf[JValue])
10 11
 }
11 12
 
12 13
 trait JacksonJsonValueReaderProperty extends JsonValueReaderProperty[JValue] { self: jackson.JsonMethods => }
13  json/src/main/scala/org/scalatra/json/JsonSupport.scala
@@ -15,17 +15,26 @@ trait JsonSupport[T] extends JsonOutput[T] {
15 15
 
16 16
   import JsonSupport._
17 17
 
  18
+  private[this] val _defaultCacheRequestBody = true
  19
+  protected def cacheRequestBodyAsString: Boolean = _defaultCacheRequestBody
18 20
   protected def parseRequestBody(format: String) = try {
19 21
     if (format == "json") {
20  
-      transformRequestBody(readJsonFromStream(request.inputStream))
  22
+      val bd  = if (cacheRequestBodyAsString) readJsonFromBody(request.body) else readJsonFromStream(request.inputStream)
  23
+      transformRequestBody(bd)
21 24
     } else if (format == "xml") {
22  
-      transformRequestBody(readXmlFromStream(request.inputStream))
  25
+      val bd  = if (cacheRequestBodyAsString) readXmlFromBody(request.body) else readXmlFromStream(request.inputStream)
  26
+      transformRequestBody(bd)
23 27
     } else JNothing
24 28
   } catch {
25 29
     case _: Throwable ⇒ JNothing
26 30
   }
27 31
 
  32
+  protected def readJsonFromBody(bd: String): JValue
28 33
   protected def readJsonFromStream(stream: InputStream): JValue
  34
+  protected def readXmlFromBody(bd: String): JValue = {
  35
+    val JObject(JField(_, jv) :: Nil) = toJson(scala.xml.XML.loadString(bd))
  36
+    jv
  37
+  }
29 38
   protected def readXmlFromStream(stream: InputStream): JValue = {
30 39
     val JObject(JField(_, jv) :: Nil) = toJson(scala.xml.XML.load(stream))
31 40
     jv
1  json/src/main/scala/org/scalatra/json/NativeJson.scala
@@ -9,6 +9,7 @@ import java.io.{InputStreamReader, InputStream, Writer}
9 9
 trait NativeJsonSupport extends JsonSupport[Document] with NativeJsonOutput {
10 10
   protected def readJsonFromStream(stream: InputStream): JValue = native.JsonParser.parse(new InputStreamReader(stream))
11 11
 
  12
+  protected def readJsonFromBody(bd: String): JValue = native.JsonParser.parse(bd)
12 13
 }
13 14
 
14 15
 trait NativeJsonValueReaderProperty extends JsonValueReaderProperty[Document] { self: native.JsonMethods => }

0 notes on commit a9f092c

Please sign in to comment.
Something went wrong with that request. Please try again.