/
CoreDsl.scala
192 lines (166 loc) · 5.19 KB
/
CoreDsl.scala
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.scalatra
import javax.servlet.ServletContext
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import servlet.ServletApiImplicits
/**
* The core Scalatra DSL.
*/
trait CoreDsl extends Handler with Control with ServletApiImplicits {
@deprecated("Use servletContext instead", "2.1.0")
def applicationContext: ServletContext = servletContext
implicit def servletContext: ServletContext
/**
* The current request
*/
implicit def request: HttpServletRequest
/**
* A map of the current parameters. The map contains the head of every
* non-empty value in `multiParams`.
*/
def params: Map[String, String]
/**
* A multi-map of the current parameters. Parameters may come from:
* - the query string
* - the POST body
* - the route matchers of the currently executing route
*
* The map has a default value of `Seq.empty`.
*/
def multiParams: MultiParams
/**
* The current response.
*/
implicit def response: HttpServletResponse
/**
* Gets the content type of the current response.
*/
def contentType: String = response.contentType getOrElse null
/**
* Sets the content type of the current response.
*/
def contentType_=(contentType: String) {
response.contentType = Option(contentType)
}
@deprecated("Use status_=(Int) instead", "2.1.0")
def status(code: Int) { status_=(code) }
/**
* Sets the status code of the current response.
*/
def status_=(code: Int) { response.status = ResponseStatus(code) }
/**
* Gets the status code of the current response.
*/
def status: Int = response.status.code
/**
* Sends a redirect response and immediately halts the current action.
*/
def redirect(uri: String) {
response.redirect(uri)
halt()
}
/**
* Adds a filter to run before the route. The filter only runs if each
* routeMatcher returns Some. If the routeMatchers list is empty, the
* filter runs for all routes.
*/
def before(transformers: RouteTransformer*)(block: => Any): Unit
/**
* Adds a filter to run after the route. The filter only runs if each
* routeMatcher returns Some. If the routeMatchers list is empty, the
* filter runs for all routes.
*/
def after(transformers: RouteTransformer*)(block: => Any): Unit
/**
* Defines a block to run if no matching routes are found, or if all
* matching routes pass.
*/
def notFound(block: => Any): Unit
/**
* Defines a block to run if matching routes are found only for other
* methods. The set of matching methods is passed to the block.
*/
def methodNotAllowed(block: Set[HttpMethod] => Any): Unit
/**
* Defines an error handler for exceptions thrown in either the before
* block or a route action.
*
* If the error handler does not match, the result falls through to the
* previously defined error handler. The default error handler simply
* rethrows the exception.
*
* The error handler is run before the after filters, and the result is
* rendered like a standard response. It is the error handler's
* responsibility to set any appropriate status code.
*/
def error(handler: ErrorHandler): Unit
/**
* The Scalatra DSL core methods take a list of [[org.scalatra.RouteMatcher]]
* and a block as the action body. The return value of the block is
* rendered through the pipeline and sent to the client as the response body.
*
* See [[org.scalatra.ScalatraBase#renderResponseBody]] for the detailed
* behaviour and how to handle your response body more explicitly, and see
* how different return types are handled.
*
* The block is executed in the context of a CoreDsl instance, so all the
* methods defined in this trait are also available inside the block.
*
* {{{
* get("/") {
* <form action="/echo">
* <label>Enter your name</label>
* <input type="text" name="name"/>
* </form>
* }
*
* post("/echo") {
* "hello {params('name)}!"
* }
* }}}
*
* ScalatraKernel provides implicit transformation from boolean blocks,
* strings and regular expressions to [[org.scalatra.RouteMatcher]], so
* you can write code naturally.
* {{{
* get("/", request.getRemoteHost == "127.0.0.1") { "Hello localhost!" }
* }}}
*
*/
def get(transformers: RouteTransformer*)(block: => Any): Route
/**
* @see get
*/
def post(transformers: RouteTransformer*)(block: => Any): Route
/**
* @see get
*/
def put(transformers: RouteTransformer*)(block: => Any): Route
/**
* @see get
*/
def delete(transformers: RouteTransformer*)(block: => Any): Route
/**
* @see get
*/
def options(transformers: RouteTransformer*)(block: => Any): Route
/**
* @see patch
*/
def patch(transformers: RouteTransformer*)(block: => Any): Route
/**
* Error handler for HTTP response status code range. You can intercept every response code previously
* specified with #status or even generic 404 error.
* {{{
* trap(403) {
* "You are not authorized"
* }
}* }}}
}}*/
def trap(codes: Range)(block: => Any): Unit
/**
* @see error
*/
def trap(code: Int)(block: => Any) {
trap(Range(code, code+1))(block)
}
}