summaryrefslogtreecommitdiff
path: root/cask/src/cask/model/Params.scala
blob: cb0730bc4e3671d086c39249287d5e993f121de8 (plain) (blame)
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
package cask.model

import java.io.{ByteArrayOutputStream, InputStream}

import cask.internal.Util
import io.undertow.server.HttpServerExchange
import io.undertow.server.handlers.CookieImpl

case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String]){
  import collection.JavaConverters._
  lazy val cookies: Map[String, Cookie] = {
    exchange.getRequestCookies.asScala.mapValues(Cookie.fromUndertow).toMap
  }
  lazy val data: InputStream = exchange.getInputStream
  def readAllBytes() = {
    val baos = new ByteArrayOutputStream()
    Util.transferTo(data, baos)
    baos.toByteArray
  }
  lazy val queryParams: Map[String, collection.Seq[String]] = {
    exchange.getQueryParameters.asScala.mapValues(_.asScala.toArray.toSeq).toMap
  }
  lazy val headers: Map[String, collection.Seq[String]] = {
    exchange.getRequestHeaders.asScala
      .map{ header => header.getHeaderName.toString.toLowerCase -> header.asScala }
      .toMap
  }
}
object Cookie{

  def fromUndertow(from: io.undertow.server.handlers.Cookie): Cookie = {
    Cookie(
      from.getName,
      from.getValue,
      from.getComment,
      from.getDomain,
      if (from.getExpires == null) null else from.getExpires.toInstant,
      from.getMaxAge,
      from.getPath,
      from.getVersion,
      from.isDiscard,
      from.isHttpOnly,
      from.isSecure
    )
  }
  def toUndertow(from: Cookie): io.undertow.server.handlers.Cookie = {
    val out = new CookieImpl(from.name, from.value)
    out.setComment(from.comment)
    out.setDomain(from.domain)
    out.setExpires(if (from.expires == null) null else java.util.Date.from(from.expires))
    out.setMaxAge(from.maxAge)
    out.setPath(from.path)
    out.setVersion(from.version)
    out.setDiscard(from.discard)
    out.setHttpOnly(from.httpOnly)
    out.setSecure(from.secure)
  }
}
case class Cookie(name: String,
                  value: String,
                  comment: String = null,
                  domain: String = null,
                  expires: java.time.Instant = null,
                  maxAge: Integer = null,
                  path: String = null,
                  version: Int = 1,
                  discard: Boolean = false,
                  httpOnly: Boolean = false,
                  secure: Boolean = false) {

}


sealed trait FormEntry{
  def valueOrFileName: String
  def headers: io.undertow.util.HeaderMap
  def asFile: Option[FormFile] = this match{
    case p: FormValue => None
    case p: FormFile => Some(p)
  }
}
object FormEntry{
  def fromUndertow(from: io.undertow.server.handlers.form.FormData.FormValue) = {
    if (!from.isFile) FormValue(from.getValue, from.getHeaders)
    else FormFile(from.getFileName, from.getPath, from.getHeaders)
  }

}
case class FormValue(value: String,
                     headers: io.undertow.util.HeaderMap) extends FormEntry{
  def valueOrFileName = value
}

case class FormFile(fileName: String,
                    filePath: java.nio.file.Path,
                    headers: io.undertow.util.HeaderMap) extends FormEntry{
  def valueOrFileName = fileName
}