author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 26 Nov 2015 14:54:45 +0000 | |
changeset 438 | 85a25932811f |
parent 198 | 2ce98ee39990 |
permissions | -rw-r--r-- |
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 |
|
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 |
* 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
|
7 |
* client's browser recording the number of visits |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
* 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 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
object Application extends Controller { |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
//no or invalid cookie results in the counter being 0 |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
def gt_cookie(c: Cookie) : Int = c.value match { |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case s if (s.forall(_.isDigit)) => s.toInt |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case _ => 0 |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
} |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
def mk_cookie(i: Int) : Cookie = { |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
Cookie("visits", i.toString) |
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 |
// GET request: read cookie data first |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
def index = Action { request => |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
//reads the cookie and extracts the visits counter |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
val visits_cookie = request.cookies.get("visits") |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
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
|
29 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
//printing a message according to value of visits counter |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
val msg = |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
if (visits >= 10) |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
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
|
34 |
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
|
35 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
//send message with new cookie |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
Ok(msg).withCookies(mk_cookie(visits + 1)) |
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 |
} |
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
bd6e45c7aa8d
added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |