summaryrefslogtreecommitdiff
path: root/cask/src/cask/model/Response.scala
blob: 59b44c99af050b2fd98382378dbfb9039cae808b (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
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(
  data: Response.Data,
  statusCode: Int,
  headers: Seq[(String, String)],
  cookies: Seq[Cookie]
)
object Response{
  def apply(data: Data,
            statusCode: Int = 200,
            headers: Seq[(String, String)] = Nil,
            cookies: Seq[Cookie] = Nil) = new Response(data, statusCode, headers, cookies)

  implicit def dataResponse[T](t: T)(implicit c: T => Data) = Response(t)
  trait Data{
    def write(out: OutputStream): Unit
  }
  object Data{
    implicit class StringData(s: String) extends Data{
      def write(out: OutputStream) = out.write(s.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)
    }
    implicit def JsonResponse[T: upickle.default.Writer](t: T) = new Data{
      def write(out: OutputStream) = implicitly[upickle.default.Writer[T]].write(
        new ujson.BaseRenderer(new OutputStreamWriter(out)),
        t
      )
    }
  }
}
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) = {
    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, Nil, Nil)
  }
}
object StaticResource{
  def apply(path: String, resourceRoot: ClassLoader) = {
    val relPath = java.nio.file.Paths.get(path)
    val (data0, statusCode0) = resourceRoot.getResourceAsStream(path) match{
      case null => ("": Response.Data, 404)
      case res => (res: Response.Data, 200)
    }
    Response(data0, statusCode0, Nil, Nil)
  }
}