-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Action.scala
595 lines (540 loc) · 19.5 KB
/
Action.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package play.api.mvc
import play.api.libs.iteratee._
import play.api._
import play.core.routing.{ HandlerInvoker, HandlerInvokerFactory, HandlerDef }
import scala.concurrent._
import scala.language.higherKinds
/**
* An Handler handles a request. Play understands several types of handlers,
* for example `EssentialAction`s and `WebSocket`s.
*
* The `Handler` used to handle the request is controlled by `GlobalSetting`s's
* `onRequestReceived` method. The default implementation of
* `onRequestReceived` delegates to `onRouteRequest` which calls the default
* `Router`.
*/
trait Handler
/**
* A handler that is able to tag requests. Usually mixed in to other handlers.
*/
trait RequestTaggingHandler extends Handler {
def tagRequest(request: RequestHeader): RequestHeader
}
/**
* An `EssentialAction` underlies every `Action`. Given a `RequestHeader`, an
* `EssentialAction` consumes the request body (an `Array[Byte]`) and returns
* a `Result`.
*
* An `EssentialAction` is a `Handler`, which means it is one of the objects
* that Play uses to handle requests.
*/
trait EssentialAction extends (RequestHeader => Iteratee[Array[Byte], Result]) with Handler {
/**
* Returns itself, for better support in the routes file.
*
* @return itself
*/
def apply() = this
}
/**
* Helper for creating `EssentialAction`s.
*/
object EssentialAction {
def apply(f: RequestHeader => Iteratee[Array[Byte], Result]): EssentialAction = new EssentialAction {
def apply(rh: RequestHeader) = f(rh)
}
}
/**
* An action is essentially a (Request[A] => Result) function that
* handles a request and generates a result to be sent to the client.
*
* For example,
* {{{
* val echo = Action { request =>
* Ok("Got request [" + request + "]")
* }
* }}}
*
* @tparam A the type of the request body
*/
trait Action[A] extends EssentialAction {
import Action._
/**
* Type of the request body.
*/
type BODY_CONTENT = A
/**
* Body parser associated with this action.
*
* @see BodyParser
*/
def parser: BodyParser[A]
/**
* Invokes this action.
*
* @param request the incoming HTTP request
* @return the result to be sent to the client
*/
def apply(request: Request[A]): Future[Result]
def apply(rh: RequestHeader): Iteratee[Array[Byte], Result] = parser(rh).mapM {
case Left(r) =>
logger.trace("Got direct result from the BodyParser: " + r)
Future.successful(r)
case Right(a) =>
val request = Request(rh, a)
logger.trace("Invoking action with request: " + request)
Play.maybeApplication.map { app =>
play.utils.Threads.withContextClassLoader(app.classloader) {
apply(request)
}
}.getOrElse {
apply(request)
}
}(executionContext)
/**
* The execution context to run this action in
*
* @return The execution context to run the action in
*/
def executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext
/**
* Returns itself, for better support in the routes file.
*
* @return itself
*/
override def apply(): Action[A] = this
override def toString = {
"Action(parser=" + parser + ")"
}
}
/**
* A body parser parses the HTTP request body content.
*
* @tparam A the body content type
*/
trait BodyParser[+A] extends Function1[RequestHeader, Iteratee[Array[Byte], Either[Result, A]]] {
self =>
/**
* Uses the provided function to transform the BodyParser's computed result
* when the request body has been parsed.
*
* @param f a function for transforming the computed result
* @param ec The context to execute the supplied function with.
* The context is prepared on the calling thread.
* @return the transformed body parser
* @see [[play.api.libs.iteratee.Iteratee#map]]
*/
def map[B](f: A => B)(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) =
self(request).map { _.right.map(f) }(pec)
override def toString = self.toString
}
}
/**
* Like map but allows the map function to execute asynchronously.
*
* @param f the async function to map the result of the body parser
* @param ec The context to execute the supplied function with.
* The context prepared on the calling thread.
* @return the transformed body parser
* @see [[map]]
* @see [[play.api.libs.iteratee.Iteratee#mapM]]
*/
def mapM[B](f: A => Future[B])(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) = self(request).mapM {
case Right(a) =>
// safe to execute `Right.apply` in same thread
f(a).map(Right.apply)(Execution.trampoline)
case left =>
Future.successful(left.asInstanceOf[Either[Result, B]])
}(pec)
override def toString = self.toString
}
}
/**
* Uses the provided function to transform the BodyParser’s computed result
* into another BodyParser to continue with.
*
* On Done of the Iteratee produced by this BodyParser, the result is passed
* to the provided function, and the resulting BodyParser is given the same
* RequestHeader and the Iteratee produced is used to continue consuming
* input.
*
* @param f the function to produce a new body parser from the result of this body parser
* @param ec The context to execute the supplied function with.
* The context is prepared on the calling thread.
* @return the transformed body parser
* @see [[play.api.libs.iteratee.Iteratee#flatMap]]
*/
def flatMap[B](f: A => BodyParser[B])(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) = self(request).flatMap {
case Left(e) => Done(Left(e))
case Right(a) => f(a)(request)
}(pec)
override def toString = self.toString
}
}
/**
* Like flatMap but allows the flatMap function to execute asynchronously.
*
* @param f the async function to produce a new body parser from the result of this body parser
* @param ec The context to execute the supplied function with.
* The context is prepared on the calling thread.
* @return the transformed body parser
* @see [[flatMap]]
* @see [[play.api.libs.iteratee.Iteratee#flatMapM]]
*/
def flatMapM[B](f: A => Future[BodyParser[B]])(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) = self(request).flatMapM {
case Right(a) =>
f(a).map { _.apply(request) }(pec)
case left =>
Future.successful {
Done[Array[Byte], Either[Result, B]](left.asInstanceOf[Either[Result, B]])
}
}(pec)
override def toString = self.toString
}
}
/**
* Uses the provided function to validate the BodyParser's computed result
* when the request body has been parsed.
*
* The provided function can produce either a direct result, which will short
* circuit any further Action, or a value of type B.
*
* Example:
* {{{
* def validateJson[A : Reads] = parse.json.validate(
* _.validate[A].asEither.left.map(e => BadRequest(JsError.toFlatJson(e)))
* )
* }}}
*
* @param f the function to validate the computed result of this body parser
* @param ec The context to execute the supplied function with.
* The context is prepared on the calling thread.
* @return the transformed body parser
*/
def validate[B](f: A => Either[Result, B])(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) = self(request).flatMap {
case Left(e) => Done(Left(e), Input.Empty)
case Right(a) => Done(f(a), Input.Empty)
}(pec)
override def toString = self.toString
}
}
/**
* Like validate but allows the validate function to execute asynchronously.
*
* @param f the async function to validate the computed result of this body parser
* @param ec The context to execute the supplied function with.
* The context is prepared on the calling thread.
* @return the transformed body parser
* @see [[validate]]
*/
def validateM[B](f: A => Future[Either[Result, B]])(implicit ec: ExecutionContext): BodyParser[B] = {
// prepare execution context as body parser object may cross thread boundary
implicit val pec = ec.prepare()
new BodyParser[B] {
def apply(request: RequestHeader) = self(request).flatMapM {
case Right(a) =>
// safe to execute `Done.apply` in same thread
f(a).map(Done.apply[Array[Byte], Either[Result, B]](_))(Execution.trampoline)
case left =>
Future.successful {
Done[Array[Byte], Either[Result, B]](left.asInstanceOf[Either[Result, B]])
}
}(pec)
override def toString = self.toString
}
}
}
/**
* Helper object to construct `BodyParser` values.
*/
object BodyParser {
/**
* Create an anonymous BodyParser
*
* Example:
* {{{
* val bodySize = BodyParser { request =>
* Iteratee.fold(0) { (state, chunk) => state + chunk.size } map(size => Right(size))
* }
* }}}
*/
def apply[T](f: RequestHeader => Iteratee[Array[Byte], Either[Result, T]]): BodyParser[T] = {
apply("(no name)")(f)
}
/**
* Create a BodyParser
*
* Example:
* {{{
* val bodySize = BodyParser("Body size") { request =>
* Iteratee.fold(0) { (state, chunk) => state + chunk.size } map(size => Right(size))
* }
* }}}
*/
def apply[T](debugName: String)(f: RequestHeader => Iteratee[Array[Byte], Either[Result, T]]): BodyParser[T] = new BodyParser[T] {
def apply(rh: RequestHeader) = f(rh)
override def toString = "BodyParser(" + debugName + ")"
}
}
/**
* A builder for generic Actions that generalizes over the type of requests.
* An ActionFunction[R,P] may be chained onto an existing ActionBuilder[R] to produce a new ActionBuilder[P] using andThen.
* The critical (abstract) function is invokeBlock.
* Most users will want to use ActionBuilder instead.
*
* @tparam R the type of the request on which this is invoked (input)
* @tparam P the parameter type which blocks executed by this builder take (output)
*/
trait ActionFunction[-R[_], +P[_]] {
self =>
/**
* Invoke the block. This is the main method that an ActionBuilder has to implement, at this stage it can wrap it in
* any other actions, modify the request object or potentially use a different class to represent the request.
*
* @param request The request
* @param block The block of code to invoke
* @return A future of the result
*/
def invokeBlock[A](request: R[A], block: P[A] => Future[Result]): Future[Result]
/**
* Get the execution context to run the request in. Override this if you want a custom execution context
*
* @return The execution context
*/
protected def executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext
/**
* Compose this ActionFunction with another, with this one applied first.
*
* @param other ActionFunction with which to compose
* @return The new ActionFunction
*/
def andThen[Q[_]](other: ActionFunction[P, Q]): ActionFunction[R, Q] = new ActionFunction[R, Q] {
def invokeBlock[A](request: R[A], block: Q[A] => Future[Result]) =
self.invokeBlock[A](request, other.invokeBlock[A](_, block))
}
/**
* Compose another ActionFunction with this one, with this one applied last.
*
* @param other ActionFunction with which to compose
* @return The new ActionFunction
*/
def compose[Q[_]](other: ActionFunction[Q, R]): ActionFunction[Q, P] =
other.andThen(this)
def compose(other: ActionBuilder[R]): ActionBuilder[P] =
other.andThen(this)
}
/**
* Provides helpers for creating `Action` values.
*/
trait ActionBuilder[+R[_]] extends ActionFunction[Request, R] {
self =>
/**
* Constructs an `Action`.
*
* For example:
* {{{
* val echo = Action(parse.anyContent) { request =>
* Ok("Got request [" + request + "]")
* }
* }}}
*
* @tparam A the type of the request body
* @param bodyParser the `BodyParser` to use to parse the request body
* @param block the action code
* @return an action
*/
final def apply[A](bodyParser: BodyParser[A])(block: R[A] => Result): Action[A] = async(bodyParser) { req: R[A] =>
Future.successful(block(req))
}
/**
* Constructs an `Action` with default content.
*
* For example:
* {{{
* val echo = Action { request =>
* Ok("Got request [" + request + "]")
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def apply(block: R[AnyContent] => Result): Action[AnyContent] = apply(BodyParsers.parse.default)(block)
/**
* Constructs an `Action` with default content, and no request parameter.
*
* For example:
* {{{
* val hello = Action {
* Ok("Hello!")
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def apply(block: => Result): Action[AnyContent] = apply(_ => block)
/**
* Constructs an `Action` that returns a future of a result, with default content, and no request parameter.
*
* For example:
* {{{
* val hello = Action.async {
* WS.url("http://www.playframework.com").get().map { r =>
* if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
* }
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def async(block: => Future[Result]): Action[AnyContent] = async(_ => block)
/**
* Constructs an `Action` that returns a future of a result, with default content.
*
* For example:
* {{{
* val hello = Action.async { request =>
* WS.url(request.getQueryString("url").get).get().map { r =>
* if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
* }
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def async(block: R[AnyContent] => Future[Result]): Action[AnyContent] = async(BodyParsers.parse.default)(block)
/**
* Constructs an `Action` that returns a future of a result, with default content.
*
* For example:
* {{{
* val hello = Action.async { request =>
* WS.url(request.getQueryString("url").get).get().map { r =>
* if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
* }
* }
* }}}
*
* @param block the action code
* @return an action
*/
final def async[A](bodyParser: BodyParser[A])(block: R[A] => Future[Result]): Action[A] = composeAction(new Action[A] {
def parser = composeParser(bodyParser)
def apply(request: Request[A]) = try {
invokeBlock(request, block)
} catch {
// NotImplementedError is not caught by NonFatal, wrap it
case e: NotImplementedError => throw new RuntimeException(e)
// LinkageError is similarly harmless in Play Framework, since automatic reloading could easily trigger it
case e: LinkageError => throw new RuntimeException(e)
}
override def executionContext = ActionBuilder.this.executionContext
})
/**
* Compose the parser. This allows the action builder to potentially intercept requests before they are parsed.
*
* @param bodyParser The body parser to compose
* @return The composed body parser
*/
protected def composeParser[A](bodyParser: BodyParser[A]): BodyParser[A] = bodyParser
/**
* Compose the action with other actions. This allows mixing in of various actions together.
*
* @param action The action to compose
* @return The composed action
*/
protected def composeAction[A](action: Action[A]): Action[A] = action
override def andThen[Q[_]](other: ActionFunction[R, Q]): ActionBuilder[Q] = new ActionBuilder[Q] {
def invokeBlock[A](request: Request[A], block: Q[A] => Future[Result]) =
self.invokeBlock[A](request, other.invokeBlock[A](_, block))
override protected def composeParser[A](bodyParser: BodyParser[A]): BodyParser[A] = self.composeParser(bodyParser)
override protected def composeAction[A](action: Action[A]): Action[A] = self.composeAction(action)
}
}
/**
* Helper object to create `Action` values.
*/
object Action extends ActionBuilder[Request] {
private val logger = Logger(Action.getClass)
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = block(request)
}
/* NOTE: the following are all example uses of ActionFunction, each subtly
* different but useful in different ways. They may not all be necessary. */
/**
* A simple kind of ActionFunction which, given a request (of type R), may
* either immediately produce a Result (for example, an error), or call
* its Action block with a parameter (of type P).
* The critical (abstract) function is refine.
*/
trait ActionRefiner[-R[_], +P[_]] extends ActionFunction[R, P] {
/**
* Determine how to process a request. This is the main method than an ActionRefiner has to implement.
* It can decide to immediately intercept the request and return a Result (Left), or continue processing with a new parameter of type P (Right).
*
* @param request the input request
* @return Either a result or a new parameter to pass to the Action block
*/
protected def refine[A](request: R[A]): Future[Either[Result, P[A]]]
final def invokeBlock[A](request: R[A], block: P[A] => Future[Result]) =
refine(request).flatMap(_.fold(Future.successful _, block))(executionContext)
}
/**
* A simple kind of ActionRefiner which, given a request (of type R),
* unconditionally transforms it to a new parameter type (P) to be passed to
* its Action block. The critical (abstract) function is transform.
*/
trait ActionTransformer[-R[_], +P[_]] extends ActionRefiner[R, P] {
/**
* Augment or transform an existing request. This is the main method that an ActionTransformer has to implement.
*
* @param request the input request
* @return The new parameter to pass to the Action block
*/
protected def transform[A](request: R[A]): Future[P[A]]
final def refine[A](request: R[A]) =
transform(request).map(Right(_))(executionContext)
}
/**
* A simple kind of ActionRefiner which, given a request (of type R), may
* either immediately produce a Result (for example, an error), or
* continue its Action block with the same request.
* The critical (abstract) function is filter.
*/
trait ActionFilter[R[_]] extends ActionRefiner[R, R] {
/**
* Determine whether to process a request. This is the main method that an ActionFilter has to implement.
* It can decide to immediately intercept the request and return a Result (Some), or continue processing (None).
*
* @param request the input request
* @return An optional Result with which to abort the request
*/
protected def filter[A](request: R[A]): Future[Option[Result]]
final protected def refine[A](request: R[A]) =
filter(request).map(_.toLeft(request))(executionContext)
}