Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 163 lines (130 sloc) 6.251 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
package play.mvc

import results._
import scala.xml.NodeSeq
import scala.io.Source
//import scala.collection.JavaConversions._

import dispatch.json.JsValue
import sjson.json.Writes

import java.io.InputStream
import java.util.concurrent.Future

import play.mvc.Http._
import play.mvc.Scope._
import play.data.validation.Validation
import play.classloading.enhancers.LocalvariablesNamesEnhancer.{LocalVariablesSupport, LocalVariablesNamesTracer}
import play.classloading.enhancers.ControllersEnhancer.ControllerSupport
import play.WithEscape

/**
*
* Represents a Scala based Controller
*/
abstract class ScalaController extends ControllerDelegate with LocalVariablesSupport with ControllerSupport {

    /**
* implicit definition to provider an easier way to render arguments
*/
    implicit def richRenderArgs(x: RenderArgs) = new RichRenderArgs(x)

    /**
* implicit definition to provider an easier way to flash arguments
*/
    implicit def richFlash(x: Flash) = new RichFlash(x)

    /**
* implicit definition to provide some extra syntactic sugar while dealing with Response objects
*/
    implicit def richResponse(x: Response) = new RichResponse(x)

    /**
* implicit definition to to provide some extra syntactic sugar while dealing with a sessions
*/
    implicit def richSession(x: Session) = new RichSession(x)

    /**
* implicit definition to to provide some extra syntactic sugar while dealing with validation
*/
    implicit def richValidation(x: Validation) = new RichValidation(x)

    // -- Responses

    def Ok = new Ok()
    def Created = new Status(201)
    def Accepted = new Status(202)
    def NoContent = new Status(204)
    def NotModified = new NotModified()
    def NotModified(etag: String) = new NotModified(etag)
    def Forbidden = new Forbidden("Forbidden")
    def Forbidden(why: String) = new Forbidden(why)
    def NotFound = new NotFound("Not found")
    def NotFound(why: String) = new NotFound(why)
    def NotFound(method: String, path: String) = new NotFound(method, path)
    def Error = new Error("Internal server error")
    def Error(why: String) = new Error(why)
    def Error(status: Int, why: String) = new Error(status, why)
    def BadRequest = new BadRequest()
    def Unauthorized = new Unauthorized("Secure")
    def Unauthorized(area: String) = new Unauthorized(area)
    def Html(html: Any) = new RenderHtml( if(html != null) html.toString else "" )
    def Xml(document: org.w3c.dom.Document) = new RenderXml(document)
    def Xml(xml: Any) = new RenderXml( if(xml != null) xml.toString else "<empty/>" )
    def Json(json: String) = new RenderJson(json)
    def Json(jsvalue: JsValue) = new RenderJson(jsvalue.toString())
    def Json(o: AnyRef) = new RenderJson(new com.google.gson.Gson().toJson(o))
    def Text(content: Any) = new RenderText(if(content != null) content.toString else "")
    def Redirect(url: String) = new Redirect(url)
    def Redirect(url: String, permanent: Boolean) = new Redirect(url, permanent)
    def Template = new Template()
    def Template(args: (Symbol, Any)*) = new Template(args = ScalaControllerCompatibility.argsToParams(args: _*))
    def Template(name: String, args: (Symbol, Any)*) = new Template(template = Some(name), args = ScalaControllerCompatibility.argsToParams(args: _*))
    def Action(action: => Any) = new ScalaAction(action)
    def Continue = new NoResult()

    @deprecated def Suspend(s: String) = new ScalaSuspend(s)
    @deprecated def Suspend(t: Int) = new ScalaSuspend(t)
    @deprecated def WaitFor(tasks: Future[_]) = new ScalaWaitFor(tasks)


    /**
* @returns a play request object
*/
    implicit def request = Request.current()

    /**
* @returns a play response object
*/
    implicit def response = Response.current()

    /**
* @returns a session object
*/
    implicit def session = Session.current()

    /**
* @returns a flash object
*/
    implicit def flash = Flash.current()

    /**
* @returns parameters
*/
    implicit def params = Params.current()

    /**
* @returns render argument object
*/
    implicit def renderArgs = RenderArgs.current()

    /**
* @returns Validation
*/
    implicit def validation = Validation.current()

    implicit def validationErrors:Map[String,play.data.validation.Error] = {
        import scala.collection.JavaConverters._
        Map.empty[String,play.data.validation.Error] ++ Validation.errors.asScala.map( e => (e.getKey, e) )
    }

    def reverse(action: => Any): play.mvc.Router.ActionDefinition = {
        val actionDefinition = reverse()
        action
        actionDefinition
    }

    def templateExists(name: String) = ControllerDelegate.templateExists(name)

}

object ScalaControllerCompatibility {

    def argsToParams(args: (Symbol,Any)*): Map[String,Any] = Map(args:_*).collect {
        case (key, value) => (key.name, value)
    }

}


case class Promise[A]()
abstract class Awaited[E,T]{
  
  def flatMap[U](f: T => Awaited[_,U]) : Awaited[_,U] ={
   this match {
      case Done(t) => f(t)
      case a@Await(p,g) => Await(p, (e:E) =>(g(e).flatMap(f)))
    }
  }

}
case class Await[E,T](p:Promise[E],f: E => Awaited[_,T]) extends Awaited[E,T]
case class Done[E,T](t:T) extends Awaited[E,T]
Something went wrong with that request. Please try again.