Created
September 23, 2013 04:24
-
-
Save waxzce/6666449 to your computer and use it in GitHub Desktop.
this is the code example from my talks at scala user group of Tokyo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import play.api.libs.iteratee.Iteratee | |
import play.api.libs.iteratee.Input | |
import play.api.libs.iteratee.Done | |
import play.api.libs.iteratee.Cont | |
import scalax.file.Path | |
import views.html.defaultpages.badRequest | |
import scalax.file.ImplicitConversions.jfile2path | |
import play.api.libs.iteratee.Enumerator | |
import scala.concurrent.Future | |
import play.api.libs.concurrent.Execution.Implicits._ | |
import scala.concurrent.duration._ | |
import java.io.File | |
object Application extends Controller { | |
def index = Action { | |
Ok(views.html.index("Your new application is ready.")) | |
} | |
///////////////// Simple body parser | |
def mysimplebodyparser = BodyParser({ | |
{ rh: RequestHeader => | |
Logger.info("this is begining of request :-)") | |
val md = java.security.MessageDigest.getInstance("SHA-256") | |
def getHash(data: Array[Byte]) = md.digest(data).map("%02x".format(_)).mkString | |
def step(c_buff: List[String])(i: Input[Array[Byte]]): Iteratee[Array[Byte], Either[Result, List[String]]] = i match { | |
case Input.EOF => Done(Right(c_buff), Input.EOF) | |
case Input.Empty => Cont[Array[Byte], Either[Result, List[String]]](i => step(c_buff :+ "empty chunk")(i)) | |
case Input.El(e) => | |
val sha256 = getHash(e) | |
val n_buff: List[String] = c_buff :+ sha256 | |
Logger.info("get " + e.length + " Bytes from request " + "" + " and hash is " + sha256) | |
Cont[Array[Byte], Either[Result, List[String]]](i => step(n_buff)(i)) | |
} | |
(Cont[Array[Byte], Either[Result, List[String]]](i => step(List[String]())(i))) | |
} | |
}) | |
def simplebodyparser = Action(mysimplebodyparser) { | |
request => | |
Ok(request.body.toString) | |
} | |
def simpleexample = Action { | |
Async { | |
val e = Enumerator[String]("hello", "tokyo", "scala", "user", "group", "!") | |
val consume: Iteratee[String, String] = { | |
def step(c_buff: String)(i: Input[String]): Iteratee[String, String] = i match { | |
case Input.EOF | Input.Empty => Done(c_buff, Input.EOF) | |
case Input.El(e) => | |
Cont[String, String](i => step(if (c_buff.isEmpty()) e else c_buff + " " + e)(i)) | |
} | |
(Cont[String, String](i => step("")(i))) | |
} | |
val newIteratee: Future[Iteratee[String, String]] = e(consume) | |
val eventuallyResult: Future[String] = newIteratee.flatMap(i => i.run) | |
eventuallyResult.map(x => Ok(x)) | |
} | |
} | |
def streamresponse = Action { | |
val e = Enumerator[String]("hello", "tokyo", "scala", "user", "group", "!") | |
Ok.stream(e.andThen(Enumerator.eof)); | |
} | |
def upload = Action(parse.temporaryFile) { request => | |
request.body.moveTo(new File("/tmp/picture/uploaded")) | |
Ok("File uploaded") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment