@@ -13,14 +13,17 @@ import com.twitter.io.Buf
1313object Main extends App {
1414
1515 val mapper : ObjectMapper = new ObjectMapper ().registerModule(DefaultScalaModule )
16- val dateFormat : DateFormat = new SimpleDateFormat (" E, dd MMM yyyy HH:mm:ss z" )
16+
17+ val dateFormat : ThreadLocal [DateFormat ] = new ThreadLocal [DateFormat ] {
18+ override def initialValue : DateFormat = new SimpleDateFormat (" E, dd MMM yyyy HH:mm:ss z" )
19+ }
1720
1821 val helloWorld : Buf = Buf .Utf8 (" Hello, World!" )
1922
2023 val muxer : HttpMuxer = new HttpMuxer ()
2124 .withHandler(" /json" , Service .mk { req : Request =>
2225 val rep = Response ()
23- rep.content = Buf .Utf8 (mapper.writeValueAsString (Map (" message" -> " Hello, World!" )))
26+ rep.content = Buf .ByteArray . Owned (mapper.writeValueAsBytes (Map (" message" -> " Hello, World!" )))
2427 rep.contentType = " application/json"
2528
2629 Future .value(rep)
@@ -34,13 +37,16 @@ object Main extends App {
3437 })
3538
3639 val serverAndDate : SimpleFilter [Request , Response ] = new SimpleFilter [Request , Response ] {
37- def apply ( req : Request , s : Service [ Request , Response ]) : Future [ Response ] =
38- s(req).map { rep =>
40+
41+ private [ this ] val addServerAndDate : Response => Response = { rep =>
3942 rep.headerMap.set(" Server" , " Finagle" )
40- rep.headerMap.set(" Date" , dateFormat.format(new Date ()))
43+ rep.headerMap.set(" Date" , dateFormat.get. format(new Date ()))
4144
4245 rep
43- }
46+ }
47+
48+ def apply (req : Request , s : Service [Request , Response ]): Future [Response ] =
49+ s(req).map(addServerAndDate)
4450 }
4551
4652 Await .ready(Http .server
0 commit comments