# HG changeset patch # User Christian Urban # Date 1348841924 -3600 # Node ID 2bd3530c30afe4dc491ea2346d79c5bc542e67fa # Parent 621301c1cb63291744a31526586ad216ce29f1d4 added programs diff -r 621301c1cb63 -r 2bd3530c30af pics/accesscontrolbook.jpg Binary file pics/accesscontrolbook.jpg has changed diff -r 621301c1cb63 -r 2bd3530c30af pics/radeon.jpg Binary file pics/radeon.jpg has changed diff -r 621301c1cb63 -r 2bd3530c30af programs/Application0.scala --- /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("

Hello world!

").as(HTML) + * + */ diff -r 621301c1cb63 -r 2bd3530c30af programs/Application1.scala --- /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 = """
+ Login:
+ Password:
+
""" + + 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) + } + +} diff -r 621301c1cb63 -r 2bd3530c30af programs/Application2.scala --- /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)) + } +} + + + + diff -r 621301c1cb63 -r 2bd3530c30af programs/Application3.scala --- /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)) + } +} + + + + diff -r 621301c1cb63 -r 2bd3530c30af programs/Application4.scala --- /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)) + } +} + + + + diff -r 621301c1cb63 -r 2bd3530c30af programs/routes --- /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