Skip to content

Commit

Permalink
rawResponse, 4.1.1, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Capote committed Jun 3, 2012
1 parent 355da90 commit 7997fa2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -131,6 +131,12 @@ object Example extends FinatraApp {
//delete the file here //delete the file here
} }



// use rawResponse to send bytes instead of strings
get("/file") { request =>
val file = GetTheFile(params.get("file"))
rawResponse(body=file.toBytes)
}
} }
``` ```


Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.posterous</groupId> <groupId>com.posterous</groupId>
<artifactId>finatra</artifactId> <artifactId>finatra</artifactId>
<version>4.1.0</version> <version>4.1.1</version>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>
<description>Sinatra clone on top of finagle-http</description> <description>Sinatra clone on top of finagle-http</description>
<inceptionYear>2012</inceptionYear> <inceptionYear>2012</inceptionYear>
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/com/posterous/finatra/FinatraApp.scala
Expand Up @@ -23,6 +23,8 @@ class FinatraApp extends FinatraController {
response(status=301, body="moved", headers=Map("Location" -> url)) response(status=301, body="moved", headers=Map("Location" -> url))
} }



// TODO: refactor these 2 methods some how
def response(status:Int = 200, body: String, headers: Map[String,String] = Map()) = { def response(status:Int = 200, body: String, headers: Map[String,String] = Map()) = {
val responseStatus = HttpResponseStatus.valueOf(status) val responseStatus = HttpResponseStatus.valueOf(status)
val resp = new DefaultHttpResponse(HTTP_1_1, responseStatus) val resp = new DefaultHttpResponse(HTTP_1_1, responseStatus)
Expand All @@ -33,6 +35,16 @@ class FinatraApp extends FinatraController {
Future.value(resp) Future.value(resp)
} }


def rawResponse(status:Int = 200, body: Array[Byte], headers: Map[String,String] = Map()) = {
val responseStatus = HttpResponseStatus.valueOf(status)
val resp = new DefaultHttpResponse(HTTP_1_1, responseStatus)
headers.foreach { xs =>
resp.setHeader(xs._1, xs._2)
}
resp.setContent(copiedBuffer(body))
Future.value(resp)
}

def toJson(obj: Any) = { def toJson(obj: Any) = {
response(body=generate(obj), headers=Map("Content-Type" -> "application/json")) response(body=generate(obj), headers=Map("Content-Type" -> "application/json"))
} }
Expand Down

0 comments on commit 7997fa2

Please sign in to comment.