summaryrefslogtreecommitdiff
path: root/cask/src/cask/model/Response.scala
blob: 07e8ecf31e4849751eddc0a29d25580e2faf951b (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
99
package cask.model

import java.io.{InputStream, OutputStream, OutputStreamWriter}

import cask.internal.Util


/**
  * The basic response returned by a HTTP endpoint.
  *
  * Note that [[data]] by default can take in a wide range of types: strings,
  * bytes, uPickle JSON-convertable types or arbitrary input streams. You can
  * also construct your own implementations of `Response.Data`.
  */
case class Response[+T](
  data: T,
  statusCode: Int,
  headers: Seq[(String, String)],
  cookies: Seq[Cookie]
){
  def map[V](f: T => V) = new Response(f(data), statusCode, headers, cookies)
}

object Response {
  type Raw = Response[Data]
  def apply[T](data: T,
               statusCode: Int = 200,
               headers: Seq[(String, String)] = Nil,
               cookies: Seq[Cookie] = Nil) = new Response(data, statusCode, headers, cookies)
  trait Data{
    def write(out: OutputStream): Unit
  }
  trait DataCompanion[V]{
    // Put the implicit constructors for Response[Data] into the `Data` companion
    // object and all subclasses of `Data`, because for some reason putting them in
    // the `Response` companion object doesn't work properly. For the same unknown
    // reasons, we cannot have `dataResponse` and `dataResponse2` take two type
    // params T and V, and instead have to embed the implicit target type as a
    // parameter of the enclosing trait

    implicit def dataResponse[T](t: T)(implicit c: T => V): Response[V] = {
      Response(c(t))
    }

    implicit def dataResponse2[T](t: Response[T])(implicit c: T => V): Response[V] = {
      t.map(c(_))
    }
  }
  object Data extends DataCompanion[Data]{
    implicit class UnitData(s: Unit) extends Data{
      def write(out: OutputStream) = ()
    }
    implicit class StringData(s: String) extends Data{
      def write(out: OutputStream) = out.write(s.getBytes)
    }
    implicit class NumericData[T: Numeric](s: T) extends Data{
      def write(out: OutputStream) = out.write(s.toString.getBytes)
    }
    implicit class BooleanData(s: Boolean) extends Data{
      def write(out: OutputStream) = out.write(s.toString.getBytes)
    }
    implicit class BytesData(b: Array[Byte]) extends Data{
      def write(out: OutputStream) = out.write(b)
    }
    implicit class StreamData(b: InputStream) extends Data{
      def write(out: OutputStream) = Util.transferTo(b, out)
    }
  }
}

object Redirect{
  def apply(url: String) = Response("", 301, Seq("Location" -> url), Nil)
}
object Abort{
  def apply(code: Int) = Response("", code, Nil, Nil)
}
object StaticFile{
  def apply(path: String, headers: Seq[(String, String)]) = {
    val relPath = java.nio.file.Paths.get(path)
    val (data0, statusCode0) =
      if (java.nio.file.Files.exists(relPath) && java.nio.file.Files.isRegularFile(relPath)){
        (java.nio.file.Files.newInputStream(relPath): Response.Data, 200)
      }else{
        ("": Response.Data, 404)
      }
    Response(data0, statusCode0, headers, Nil)
  }
}
object StaticResource{
  def apply(path: String, resourceRoot: ClassLoader, headers: Seq[(String, String)]) = {
    val (data0, statusCode0) = resourceRoot.getResourceAsStream(path) match{
      case null => ("": Response.Data, 404)
      case res => (res: Response.Data, 200)
    }
    Response(data0, statusCode0, headers, Nil)
  }
}