--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/Application0.scala Tue Sep 24 12:23:54 2013 +0100
@@ -0,0 +1,23 @@
+package controllers
+
+import play.api.mvc._
+
+// hello world program:
+// just answer the GET request with a string
+
+object Application extends Controller {
+
+ // answering a GET request
+ val index = Action { request =>
+
+ Ok("Hello world!")
+ }
+
+}
+
+/*
+ * HTML can be returned using
+ *
+ * Ok("<H1>Hello world!</H1>").as(HTML)
+ *
+ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/Application1.scala Tue Sep 24 12:23:54 2013 +0100
@@ -0,0 +1,39 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data._
+import play.api.data.Forms._
+
+/*
+ * Answers a GET-request by sending a simple login form.
+ *
+ * Processes the POST-data by just printing the results.
+ *
+ */
+
+object Application extends Controller {
+
+ // GET request -> login form
+ val index = Action { request =>
+
+ val form = """
+ <form method="post">
+ Login: <input type="text" name="login"><br>
+ Password: <input type="password" name="password"><br>
+ <input type="submit"></form>"""
+
+ Ok(form).as(HTML)
+ }
+
+
+ // POST data: processing the login data
+ val receive = Action { request =>
+
+ val form_data = Form(tuple ("login" -> text, "password" -> text))
+ val (login, password) = form_data.bindFromRequest()(request).get
+
+ Ok(s"Received login: $login and password: $password")
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/Application2.scala Tue Sep 24 12:23:54 2013 +0100
@@ -0,0 +1,43 @@
+package controllers
+
+import play.api.mvc._
+
+/*
+ * Application sets a cookie in plain ASCII on the
+ * client's browser recording the number of visits
+ * of a page.
+ */
+
+object Application extends Controller {
+
+ //no or invalid cookie results in the counter being 0
+ def gt_cookie(c: Cookie) : Int = c.value match {
+ case s if (s.forall(_.isDigit)) => s.toInt
+ case _ => 0
+ }
+
+ def mk_cookie(i: Int) : Cookie = {
+ Cookie("visits", i.toString)
+ }
+
+ // GET request: read cookie data first
+ def index = Action { request =>
+
+ //reads the cookie and extracts the visits counter
+ val visits_cookie = request.cookies.get("visits")
+ val visits = visits_cookie.map(gt_cookie).getOrElse(0)
+
+ //printing a message according to value of visits counter
+ val msg =
+ if (visits >= 10)
+ s"You are a valued customer who has visited this site $visits times."
+ else s"You have visited this site $visits times."
+
+ //send message with new cookie
+ Ok(msg).withCookies(mk_cookie(visits + 1))
+ }
+}
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/Application3.scala Tue Sep 24 12:23:54 2013 +0100
@@ -0,0 +1,50 @@
+package controllers
+
+import play.api.mvc._
+import java.security.MessageDigest
+
+/*
+ * Application sets a cookie in plain ASCII on the
+ * clients browser recording the visits of a page.
+ *
+ * The cookie data is hashed and stored in the format:
+ *
+ * visits_counter/hashed_value
+ */
+
+object Application extends Controller {
+
+ //hash functions: SHA-1, SHA-256, etc
+ def mk_hash(s: String) : String = {
+ val hash_fun = MessageDigest.getInstance("SHA-1")
+ hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString
+ }
+
+ //extracting from the string .../... the visits
+ //value and hash
+ def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
+ case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt
+ case _ => 0
+ }
+
+ def mk_cookie(i: Int) : Cookie = {
+ val hash = mk_hash(i.toString)
+ Cookie("visits", s"$i/$hash")
+ }
+
+ def index = Action { request =>
+ val visits_cookie = request.cookies.get("visits")
+ val visits = visits_cookie.map(gt_cookie).getOrElse(0)
+
+ val msg =
+ if (visits >= 10)
+ s"You are a valued customer who has visited this site $visits times."
+ else s"You have visited this site $visits times."
+
+ Ok(msg).withCookies(mk_cookie(visits + 1))
+ }
+}
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/Application4.scala Tue Sep 24 12:23:54 2013 +0100
@@ -0,0 +1,52 @@
+package controllers
+
+import play.api.mvc._
+import java.security.MessageDigest
+
+/*
+ * Application sets a cookie in plain ASCII on the
+ * clients browser recording the visits of a page.
+ *
+ * The cookie data is hashed and salted with a
+ * secret key.
+ */
+
+
+object Application extends Controller {
+
+ //secret key for salting
+ val salt = "my secret key"
+
+ //SHA-1 + salt
+ def mk_hash(s: String) : String = {
+ val hash_fun = MessageDigest.getInstance("SHA-1")
+ hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString
+ }
+
+ def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
+ case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt
+ case _ => 0
+ }
+
+ def mk_cookie(i: Int) : Cookie = {
+ val hash = mk_hash(i.toString)
+ Cookie("visits", s"$i/$hash")
+ }
+
+ def index = Action { request =>
+
+ val visits_cookie = request.cookies.get("visits")
+ val visits = visits_cookie.map(gt_cookie).getOrElse(0)
+
+ val msg =
+ if (visits >= 10)
+ s"You are a valued customer who has visited this site $visits times."
+ else s"You have visited this site $visits times."
+
+ Ok(msg).withCookies(mk_cookie(visits + 1))
+ }
+}
+
+
+
+