added programs
authorChristian Urban <urbanc@in.tum.de>
Fri, 28 Sep 2012 15:18:44 +0100
changeset 13 2bd3530c30af
parent 12 621301c1cb63
child 14 fd7e587c794e
added programs
pics/accesscontrolbook.jpg
pics/radeon.jpg
programs/Application0.scala
programs/Application1.scala
programs/Application2.scala
programs/Application3.scala
programs/Application4.scala
programs/routes
Binary file pics/accesscontrolbook.jpg has changed
Binary file pics/radeon.jpg has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/Application0.scala	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,23 @@
+package controllers
+
+import play.api.mvc._
+
+// hello world program 
+// just answers 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/programs/Application1.scala	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,38 @@
+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("Received login: " + login + " and password: " + password)
+  }
+  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/Application2.scala	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,45 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data._
+import play.api.data.Forms._
+
+/*
+ * Application sets a cookie in plain ASCII on the
+ * clients browser recording the visits of a page.
+ */
+
+object Application extends Controller {
+
+  //no or invalid cookie results in the counter being 0
+  def gt_cookie(c: Option[Cookie]) : Int = c.map(_.value) match {
+    case Some(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 = gt_cookie(visits_cookie)
+
+    //printing a message according to value of visits counter
+    val msg1 = "You are a valued customer who has visited this site %d times."               
+    val msg2 = "You have visited this site %d times."
+    val msg = 
+      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
+    
+    //send message with new cookie
+    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
+  }
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/Application3.scala	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,56 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data._
+import play.api.data.Forms._
+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 ans 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: Option[Cookie]) : Int = 
+    c.map(_.value.split("/")) match {
+      case Some(Array(s, h)) 
+        if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
+      case _ => 0
+    }
+
+  def mk_cookie(i: Int) : Cookie = {
+    val s = i.toString
+    Cookie("visits", s + "/" + mk_hash(s))
+  }
+   
+  def index = Action { request =>
+
+    val visits_cookie = request.cookies.get("visits")
+    val visits = gt_cookie(visits_cookie)
+
+    val msg1 = "You are a valued customer who has visited this site %d times."
+    val msg2 = "You have visited this site %d times."
+    val msg = 
+      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
+    
+    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
+  }     
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/Application4.scala	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,59 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data._
+import play.api.data.Forms._
+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 - this key should not be
+  //sent to the client; the key should normally be
+  //a unguessable random number generated once
+  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: Option[Cookie]) : Int = 
+    c.map(_.value.split("/")) match {
+      case Some(Array(s, h)) 
+        if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
+      case _ => 0
+    }
+
+  def mk_cookie(i: Int) : Cookie = {
+    val s = i.toString
+    Cookie("visits", s + "/" + mk_hash(s))
+  }
+   
+  def index = Action { request =>
+
+    val visits_cookie = request.cookies.get("visits")
+    val visits = gt_cookie(visits_cookie)
+
+    val msg1 = "You are a valued customer who has visited this site %d times."
+    val msg2 = "You have visited this site %d times."
+    val msg = 
+      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
+    
+    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
+  }     
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/routes	Fri Sep 28 15:18:44 2012 +0100
@@ -0,0 +1,10 @@
+# Application1 needs entries for both GET and POST 
+#
+# all other applications only need an entry for GET
+
+# Home page
+GET     /                           controllers.Application.index
+#POST    /                           controllers.Application.receive
+
+# Map static resources from the /public folder to the /assets URL path
+GET     /assets/*file               controllers.Assets.at(path="/public", file)
\ No newline at end of file