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
|
package cask.endpoints
import cask.internal.Router.EntryPoint
import cask.internal.Router
import cask.main.Routes
import cask.model.{ParamContext, Response}
import io.undertow.server.HttpServerExchange
import io.undertow.server.handlers.form.FormParserFactory
import collection.JavaConverters._
sealed trait FormReader[T] extends Router.ArgReader[Seq[FormValue], T, cask.model.ParamContext]
object FormReader{
implicit def paramFormReader[T: QueryParamReader] = new FormReader[T]{
def arity = implicitly[QueryParamReader[T]].arity
def read(ctx: cask.model.ParamContext, input: Seq[FormValue]) = {
implicitly[QueryParamReader[T]].read(ctx, input.map(_.value))
}
}
implicit def formValueReader = new FormReader[FormValue]{
def arity = 1
def read(ctx: cask.model.ParamContext, input: Seq[FormValue]) = input.head
}
implicit def formValuesReader = new FormReader[Seq[FormValue]]{
def arity = 1
def read(ctx: cask.model.ParamContext, input: Seq[FormValue]) = input
}
implicit def formValueFileReader = new FormReader[FormValue.File]{
def arity = 1
def read(ctx: cask.model.ParamContext, input: Seq[FormValue]) = input.head.asFile.get
}
implicit def formValuesFileReader = new FormReader[Seq[FormValue.File]]{
def arity = 1
def read(ctx: cask.model.ParamContext, input: Seq[FormValue]) = input.map(_.asFile.get)
}
}
class postForm(val path: String, override val subpath: Boolean = false) extends Endpoint[Response]{
type InputType = Seq[FormValue]
def wrapMethodOutput(t: Response) = t
def parseMethodInput[T](implicit p: FormReader[T]) = p
def handle(ctx: ParamContext,
bindings: Map[String, String],
routes: Routes,
entryPoint: EntryPoint[Seq[FormValue], Routes, cask.model.ParamContext]): Router.Result[Response] = {
val formData = FormParserFactory.builder().build().createParser(ctx.exchange).parseBlocking()
val formDataBindings =
formData
.iterator()
.asScala
.map(k => (k, formData.get(k).asScala.map(FormValue.fromUndertow).toSeq))
val pathBindings =
bindings.map{case (k, v) => (k, Seq(new FormValue.Plain(v, new io.undertow.util.HeaderMap())))}
entryPoint.invoke(routes, ctx, pathBindings ++ formDataBindings)
.asInstanceOf[Router.Result[Response]]
}
}
|