casualjim
1/18/2012 - 8:24 PM

gistfile1.scala

trait CookieSupport extends Handler {
  self: ScalatraKernel =>

  import CookieSupport._

  private val _cookieOptions = new DynamicVariable[CookieOptions](null)
  implicit def cookieOptions: CookieOptions = _cookieOptions.value

  private val _cookies = new DynamicVariable[SweetCookies](null)
  def cookies = _cookies.value

  abstract override def handle(req: HttpServletRequest, res: HttpServletResponse) {
    _cookies.withValue(new SweetCookies(req.cookies, res)) {
      _cookieOptions.withValue(CookieOptions(path = req.getContextPath)) {
        super.handle(req, res)
      }
    }
  }

}