-
Notifications
You must be signed in to change notification settings - Fork 297
/
servlet.clj
71 lines (62 loc) · 2.55 KB
/
servlet.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
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
; Copyright 2013 Relevance, Inc.
; Copyright 2014-2019 Cognitect, Inc.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
; which can be found in the file epl-v10.html at the root of this distribution.
;
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
;
; You must not remove this notice, or any other, from this software.
(ns io.pedestal.http.servlet
"Generic Servlet adapter that closes over its implementation
functions."
(:import (javax.servlet Servlet ServletConfig)))
;; Do not construct instances directly; use the 'servlet' function.
(deftype FnServlet [init-fn service-fn destroy-fn
^:unsynchronized-mutable config]
Servlet
(init [this servlet-config]
(set! config servlet-config)
(init-fn this servlet-config))
(destroy [this]
(destroy-fn this))
(service [this servlet-request servlet-response]
(service-fn this servlet-request servlet-response))
(getServletConfig [this]
config)
(getServletInfo [this]
(str "FnServlet dispatching to " service-fn))
ServletConfig
(getInitParameter [this name]
(when-not (nil? config)
(.getInitParameter ^ServletConfig config name)))
(getInitParameterNames [this]
(when-not (nil? config)
(.getInitParameterNames ^ServletConfig config)))
(getServletContext [this]
(when-not (nil? config)
(.getServletContext ^ServletConfig config)))
(getServletName [this]
(when-not (nil? config)
(.getServletName ^ServletConfig config))))
(defn servlet
"Returns an instance of javax.servlet.Servlet using provided
functions for its implementation. Arguments are key-value pairs of:
:init optional, initialization function taking two arguments:
the Servlet and its ServletConfig
:service required, handler function taking three arguments: the
Servlet, ServletRequest, and ServletResponse
:destroy optional, shutdown function taking one argument: the
Servlet
The :init, :service, and :destroy options correspond to the Servlet
interface methods of the same names.
Note: this function returns an instance, not a class. If you need a
class with a static name (for example, to deploy to a Servlet
container) use the Java class pedestal.servlet.ClojureVarServlet."
[& {:keys [init service destroy]}]
{:pre [(fn? service)]}
(FnServlet. (or init (fn [_ _]))
service
(or destroy (fn [_]))
nil))