-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompojure_response.clj
42 lines (40 loc) · 1.19 KB
/
compojure_response.clj
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
(ns compojure.response
"Methods for generating Ring response maps"
(:use [clojure.core.incubator :only (-?>)]
[ring.util.response :only (response content-type)])
(:require [clojure.java.io :as io])
(:import [java.io File InputStream]
[java.net URL]
[clojure.lang APersistentMap IDeref IFn ISeq]))
(defprotocol Renderable
(render [this request]
"Render the object into a form suitable for the given request map."))
(extend-protocol Renderable
nil
(render [_ _] nil)
String
(render [body _]
(-> (response body)
(content-type "text/html; charset=utf-8")))
APersistentMap
(render [resp-map _]
(merge (with-meta (response "") (meta resp-map))
resp-map))
IFn
(render [func request]
(render (func request) request))
IDeref
(render [ref request]
(render (deref ref) request))
File
(render [file _] (response file))
ISeq
(render [coll _] (-> (response coll)
(content-type "text/html; charset=utf-8")))
InputStream
(render [stream _] (response stream))
URL
(render [url _]
(if (= "file" (.getProtocol url))
(response (io/as-file url))
(response (io/input-stream url)))))