-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestServlet3.groovy
304 lines (294 loc) · 11.5 KB
/
TestServlet3.groovy
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
import javax.servlet.RequestDispatcher
import javax.servlet.ServletException
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.util.concurrent.CountDownLatch
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_PARAMETERS
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS
class TestServlet3 {
@WebServlet
static class Sync extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
String servletPath = req.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH)
if (servletPath == null) {
servletPath = req.servletPath
}
ServerEndpoint endpoint = ServerEndpoint.forPath(servletPath)
HttpServerTest.controller(endpoint) {
resp.contentType = "text/plain"
switch (endpoint) {
case SUCCESS:
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case INDEXED_CHILD:
resp.status = endpoint.status
endpoint.collectSpanAttributes { req.getParameter(it) }
break
case QUERY_PARAM:
resp.status = endpoint.status
resp.writer.print(req.queryString)
break
case REDIRECT:
resp.sendRedirect(endpoint.body)
break
case CAPTURE_HEADERS:
resp.setHeader("X-Test-Response", req.getHeader("X-Test-Request"))
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case CAPTURE_PARAMETERS:
req.setCharacterEncoding("UTF8")
def value = req.getParameter("test-parameter")
if (value != "test value õäöü") {
throw new ServletException("request parameter does not have expected value " + value)
}
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case ERROR:
resp.sendError(endpoint.status, endpoint.body)
break
case EXCEPTION:
throw new ServletException(endpoint.body)
case AbstractServlet3Test.HTML_PRINT_WRITER:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.setContentLengthLong(endpoint.body.length())
resp.writer.print(endpoint.body)
break
case AbstractServlet3Test.HTML_SERVLET_OUTPUT_STREAM:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.setContentLength(endpoint.body.length())
byte[] body = endpoint.body.getBytes()
resp.getOutputStream().write(body, 0, body.length)
break
case AbstractServlet3Test.HTML_PRINT_WRITER_WITH_OTHER_HEAD:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.setContentLengthLong(endpoint.body.length())
resp.writer.print(endpoint.body)
break
case AbstractServlet3Test.HTML_SERVLET_OUTPUT_STREAM_WITH_OTHER_HEAD:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.setContentLength(endpoint.body.length())
byte[] body = endpoint.body.getBytes()
resp.getOutputStream().write(body, 0, body.length)
break
}
}
}
}
@WebServlet(asyncSupported = true)
static class Async extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
ServerEndpoint endpoint = ServerEndpoint.forPath(req.servletPath)
def latch = new CountDownLatch(1)
def context = req.startAsync()
if (endpoint == EXCEPTION) {
context.setTimeout(5000)
}
context.start {
try {
HttpServerTest.controller(endpoint) {
resp.contentType = "text/plain"
switch (endpoint) {
case SUCCESS:
resp.status = endpoint.status
resp.writer.print(endpoint.body)
context.complete()
break
case INDEXED_CHILD:
endpoint.collectSpanAttributes { req.getParameter(it) }
resp.status = endpoint.status
context.complete()
break
case QUERY_PARAM:
resp.status = endpoint.status
resp.writer.print(req.queryString)
context.complete()
break
case REDIRECT:
resp.sendRedirect(endpoint.body)
context.complete()
break
case CAPTURE_HEADERS:
resp.setHeader("X-Test-Response", req.getHeader("X-Test-Request"))
resp.status = endpoint.status
resp.writer.print(endpoint.body)
context.complete()
break
case CAPTURE_PARAMETERS:
req.setCharacterEncoding("UTF8")
def value = req.getParameter("test-parameter")
if (value != "test value õäöü") {
throw new ServletException("request parameter does not have expected value " + value)
}
resp.status = endpoint.status
resp.writer.print(endpoint.body)
context.complete()
break
case ERROR:
resp.status = endpoint.status
resp.writer.print(endpoint.body)
// resp.sendError(endpoint.status, endpoint.body)
context.complete()
break
case EXCEPTION:
resp.status = endpoint.status
def writer = resp.writer
writer.print(endpoint.body)
if (req.getClass().getName().contains("catalina")) {
// on tomcat close the writer to ensure response is sent immediately, otherwise
// there is a chance that tomcat resets the connection before the response is sent
writer.close()
}
throw new ServletException(endpoint.body)
break
case AbstractServlet3Test.HTML_PRINT_WRITER:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.setContentLength(endpoint.body.length())
resp.writer.print(endpoint.body)
context.complete()
break
case AbstractServlet3Test.HTML_SERVLET_OUTPUT_STREAM:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.getOutputStream().print(endpoint.body)
context.complete()
break
}
}
} finally {
latch.countDown()
}
}
latch.await()
}
}
@WebServlet(asyncSupported = true)
static class FakeAsync extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
def context = req.startAsync()
try {
ServerEndpoint endpoint = ServerEndpoint.forPath(req.servletPath)
HttpServerTest.controller(endpoint) {
resp.contentType = "text/plain"
switch (endpoint) {
case SUCCESS:
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case INDEXED_CHILD:
endpoint.collectSpanAttributes { req.getParameter(it) }
resp.status = endpoint.status
break
case QUERY_PARAM:
resp.status = endpoint.status
resp.writer.print(req.queryString)
break
case REDIRECT:
resp.sendRedirect(endpoint.body)
break
case CAPTURE_HEADERS:
resp.setHeader("X-Test-Response", req.getHeader("X-Test-Request"))
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case CAPTURE_PARAMETERS:
req.setCharacterEncoding("UTF8")
def value = req.getParameter("test-parameter")
if (value != "test value õäöü") {
throw new ServletException("request parameter does not have expected value " + value)
}
resp.status = endpoint.status
resp.writer.print(endpoint.body)
break
case ERROR:
resp.sendError(endpoint.status, endpoint.body)
break
case EXCEPTION:
resp.status = endpoint.status
resp.writer.print(endpoint.body)
throw new ServletException(endpoint.body)
case AbstractServlet3Test.HTML_PRINT_WRITER:
// intentionally testing setting status before contentType here to cover that case somewhere
resp.status = endpoint.status
resp.contentType = "text/html"
resp.writer.print(endpoint.body)
break
case AbstractServlet3Test.HTML_SERVLET_OUTPUT_STREAM:
resp.contentType = "text/html"
resp.status = endpoint.status
resp.getOutputStream().print(endpoint.body)
break
}
}
} finally {
context.complete()
}
}
}
@WebServlet(asyncSupported = true)
static class DispatchImmediate extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
def target = req.servletPath.replace("/dispatch", "")
if (req.queryString != null) {
target += "?" + req.queryString
}
req.startAsync().dispatch(target)
}
}
@WebServlet(asyncSupported = true)
static class DispatchAsync extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
def target = req.servletPath.replace("/dispatch", "")
if (req.queryString != null) {
target += "?" + req.queryString
}
def context = req.startAsync()
context.start {
context.dispatch(target)
}
}
}
// TODO: Add tests for this!
@WebServlet(asyncSupported = true)
static class DispatchRecursive extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
if (req.servletPath.equals("/recursive")) {
resp.writer.print("Hello Recursive")
return
}
def depth = Integer.parseInt(req.getParameter("depth"))
if (depth > 0) {
req.startAsync().dispatch("/dispatch/recursive?depth=" + (depth - 1))
} else {
req.startAsync().dispatch("/recursive")
}
}
}
}