Attic/scala/Application3.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 15 Dec 2016 16:18:25 +0000
changeset 506 5446dc1fc992
parent 198 2ce98ee39990
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
103
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
package controllers
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import play.api.mvc._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import java.security.MessageDigest
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
/*
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
 * Application sets a cookie in plain ASCII on the
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
 * clients browser recording the visits of a page.
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
 *
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
 * The cookie data is hashed and stored in the format:
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
 *
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
 * visits_counter/hashed_value 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
 */
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
object Application extends Controller {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  //hash functions: SHA-1, SHA-256, etc
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def mk_hash(s: String) : String = {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    val hash_fun = MessageDigest.getInstance("SHA-1")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  //extracting from the string .../... the visits
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  //value and hash
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
    case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    case _ => 0
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  def mk_cookie(i: Int) : Cookie = {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    val hash = mk_hash(i.toString)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    Cookie("visits", s"$i/$hash")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
   
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def index = Action { request =>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    val visits_cookie = request.cookies.get("visits")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
    val visits = visits_cookie.map(gt_cookie).getOrElse(0)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    val msg = 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
      if (visits >= 10)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
        s"You are a valued customer who has visited this site $visits times."               
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
      else s"You have visited this site $visits times."
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
    
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
    Ok(msg).withCookies(mk_cookie(visits + 1))
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  }     
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
}
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50