/
NothingServerHandler.scala
433 lines (350 loc) · 12.5 KB
/
NothingServerHandler.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.something.lift
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.channel._
import java.net.URL
import net.liftweb.common.{Empty, Box}
import net.liftweb.http.provider.servlet.{HTTPResponseServlet, HTTPRequestServlet}
import java.io.{OutputStream, InputStream}
import net.liftweb.http.provider._
import net.liftweb.http._
import java.util.Locale
import net.liftweb.util.{Helpers, Schedule, LoanWrapper}
/**
* Created by IntelliJ IDEA.
* User: jordanrw
* Date: 2/22/12
* Time: 10:04 PM
*/
class NettyContext extends HTTPContext {
/**
* @return - the context path. It always comes first in a request URI. It is
* the URI part that represent to context of the request.
*/
def path: String = ""
/**
* Returns the URL representation of a resource that is mapped by a fully qualified path.
* The path is considered relative to the root path and it always starts with '/'.
*
* @param path - the resource path
* @return - the URL object of the path
*/
def resource(path: String): URL = throw new Exception("Implement me, please")
/**
* Same as <i>resource</i> but returns an InputStream to read the resource.
* @param path - the resource path
* @return InputStream
*/
def resourceAsStream(path: String): InputStream = throw new Exception("Implement me, please")
/**
* @param path
* @return - the mime type mapped to resource determined by this path.
*/
def mimeType(path: String): Box[String] = Empty
/**
* @param name
* @return - the value of the init parameter identified by then provided name. Note
* that this is not typesfe and you need to explicitely do the casting
* when reading this attribute. Returns Empty if this parameter does not exist.
*/
def initParam(name: String): Box[String] = Empty
/**
* @return - a List of Tuple2 consisting of name and value pair of the init parameters
*/
def initParams: List[(String, String)] = Nil
/**
* @param name
* @return - the value of the context attribute identified by then provided name.
* Returns Empty if this parameter does not exist.
*/
def attribute(name: String): Box[Any] = Empty
/**
* @return - a List of Tuple2 consisting of name and value pair of the attributes
*/
def attributes: List[(String, Any)] = Nil
/**
* @param - name
* @param - value. Any reference. Note that this is not typesfe and you need to explicitely do
* the casting when reading this attribute.
*/
def setAttribute(name: String, value: Any): Unit = {}
/**
* @param - name. The name ofthe parameter that needs to be removed.
*/
def removeAttribute(name: String): Unit = {}
}
class NothingServerHandler extends SimpleChannelUpstreamHandler with HTTPProvider{
type VarProvider = { def apply[T](session: Box[LiftSession], f: => T): T}
lazy val transientVarProvider: VarProvider =
findObject("net.liftweb.http.TransientRequestVarHandler").open_!.asInstanceOf[VarProvider]
lazy val reqVarProvider: VarProvider =
findObject("net.liftweb.http.RequestVarHandler").open_!.asInstanceOf[VarProvider]
private def findObject(cls: String): Box[AnyRef] =
Helpers.tryo[Class[_]](Nil)(Class.forName(cls + "$")).flatMap {
c =>
Helpers.tryo {
val field = c.getField("MODULE$")
field.get(null)
}
}
lazy val nettyContext = new NettyContext
def context = nettyContext
lazy val liftLand = new LiftServlet(nettyContext)
/**
* Wrap the loans around the incoming request
*/
private def handleLoanWrappers[T](f: => T): T = {
/*
FIXME -- this is a 2.5-ism
val wrappers = LiftRules.allAround.toList
def handleLoan(lst: List[LoanWrapper]): T = lst match {
case Nil => f
case x :: xs => x(handleLoan(xs))
}
handleLoan(wrappers)
*/
f
}
override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent) {
println("MESSAGE RECEIVED")
val request = e.getMessage.asInstanceOf[HttpRequest]
println(request)
if (HttpHeaders.is100ContinueExpected(request)) {
send100Continue(e);
}
def doNotHandled() {
}
/**
* The representation of a HTTP request state
*/
class NettyHttpRequest extends HTTPRequest {
/**
* @return - cookies from this request. Nil if there are no cookies.
*/
def cookies: List[HTTPCookie] = throw new Exception("Implement me")
/**
* The provider associated with this request
*/
def provider: HTTPProvider = NothingServerHandler.this
/**
* @return - HTTP authentication scheme: BASIC, DIGEST etc.
* Empty if there is auth header.
*/
def authType: Box[String] = throw new Exception("Implement me")
/**
* Return the header values by the given name.
*
* @param name - the header name
* @return - List[String] or Nil if there is no such header
*/
def headers(name: String): List[String] = throw new Exception("Implement me")
/**
* @return - all header parameters present in this request. Nil if no
* headers are used.
*/
def headers: List[HTTPParam] = throw new Exception("Implement me")
/**
* @return - the context path. Similar with HttpServletRequest.getContextPath.
* Return "" empty string if your implementation does not support the contept of
* context path
*/
def contextPath: String = ""
/**
* @return - the HTTPContext of this service. It does not guarantee that it returns the same
* HTTPContext reference upon each invocation but it must guarantee that HTTPContext
* reference contains the same information.
*/
def context: HTTPContext = nettyContext
/**
* @return - the MIME type of the body of the request. Empty if this is unknonwn.
*/
def contentType: Box[String] = Empty //FIXME // throw new Exception("Implement me")
/**
* @return - the request URI
*/
def uri: String = request.getUri // throw new Exception("Implement me")
/**
* @return - the request URL
*/
def url: String = throw new Exception("Implement me")
/**
* @return - the entire query string. Empty if the requst contains no query string
*/
def queryString: Box[String] = throw new Exception("Implement me")
/**
* @param name - the parameter name
* @return - the list of values associated with this name
*/
def param(name: String): List[String] = throw new Exception("Implement me")
/**
* @return - all request parameters
*/
def params: List[HTTPParam] = throw new Exception("Implement me")
/**
* @return - request parameter names
*/
def paramNames: List[String] = throw new Exception("Implement me")
/**
* @return - the HTTP session associated with this request
*/
def session: HTTPSession = throw new Exception("Implement me")
/**
* Destroy the underlying servlet session
*/
def destroyServletSession(): Unit = {}
/**
* @return the sessionID (if there is one) for this request. This will *NOT* create
* a new session if one does not already exist
*/
def sessionId: Box[String] = Empty
/**
* @return - the remote address of the client or the last seen proxy.
*/
def remoteAddress: String = throw new Exception("Implement me")
/**
* @return - the source port of the client or last seen proxy.
*/
def remotePort: Int = throw new Exception("Implement me")
/**
* @return - the fully qualified name of the client host or last seen proxy
*/
def remoteHost: String = throw new Exception("Implement me")
/**
* @return - the host name of the server
*/
def serverName: String = throw new Exception("Implement me")
/**
* @return - the name of the scheme of this request: http, https etc.
*/
def scheme: String = throw new Exception("Implement me")
/**
* @return - the server port
*/
def serverPort: Int = throw new Exception("Implement me")
/**
* @return - the HTTP method: GET, POST etc.
*/
def method: String = request.getMethod.toString
/**
* @return true if the underlying container supports suspend/resume idiom.
*/
def suspendResumeSupport_? : Boolean = false // FIXME this should be trivial
/**
* @return - Some[Any] if this is a resumed request, return the state
* associated with it.
*/
def resumeInfo : Option[(Req, LiftResponse)] = throw new Exception("Implement me") // FIXME trivial support
/**
* Suspend the curent request and resume it after a given timeout
*/
def suspend(timeout: Long): RetryState.Value = throw new Exception("Implement me") // FIXME trivial support
/**
* Resume this request
* @return false if this continuation cannot be resumed
* as it is not in pending state.
*/
def resume(what: (Req, LiftResponse)): Boolean = throw new Exception("Implement me") // FIXME trivial support
/**
* @return - the input stream for the request body
*/
def inputStream: InputStream = throw new Exception("Implement me")
/**
* @return true - if the request content is multipart
*/
def multipartContent_? : Boolean = throw new Exception("Implement me")
/**
* @return - the files uploaded
*/
def extractFiles: List[ParamHolder] = throw new Exception("Implement me")
/**
* @return - the locale forthis request. Empty if there is not language information.
*/
def locale: Box[Locale] = throw new Exception("Implement me")
/**
* Sets the character encoding that will be used for request body read
*
* @param encoding - the encoding that will be used (e.g. UTF-8)
*/
def setCharacterEncoding(encoding: String) = throw new Exception("Implement me")
/**
* Creates a new HTTPRequest instance as a copy of this one. It is used when
* snapshots of the current request context is created in order for this request object
* to be used on different threads (such as asynchronous template fragments processing).
* The new instance must not keep any reference to the container' instances.
*/
def snapshot: HTTPRequest = this
/**
* The User-Agent of the request
*/
def userAgent: Box[String] = throw new Exception("Implement me")
}
class MyResponse extends HTTPResponse {
/**
* Add cookies to the response
*
* @param cookies - the list of response cookies
*/
def addCookies(cookies: List[HTTPCookie]) = throw new Exception("Implement me")
/**
* Encodes the URL such that it adds the session ID if it is necessary to the URL.
* It is implementation specific detail how/if to add the session informations. This will be used
* for URL rewriting purposes.
*
* @param url - the URL that needs to be analysed
*/
def encodeUrl(url: String): String = url
/**
* Add a list of header parameters to the response object
*
* @param headers - the list of headers
*/
def addHeaders(headers: List[HTTPParam]) = throw new Exception("Implement me")
/**
* Sets the HTTP response status
*
* @param status - the HTTP status
*/
def setStatus(status: Int) = throw new Exception("Implement me")
/**
* Returns the HTTP response status that has been set with setStatus
* */
def getStatus: Int = throw new Exception("Implement me")
/**
* Sets the HTTP response status
*
* @param status - the HTTP status
* @param reason - the HTTP reason phrase
*/
def setStatusWithReason(status: Int, reason: String) = throw new Exception("Implement me")
/**
* @return - the OutputStream that can be used to send down o the client the response body.
*/
def outputStream: OutputStream = throw new Exception("Implement me")
}
Schedule(() => {
try {
transientVarProvider(Empty,
reqVarProvider(Empty,{
val httpRequest: HTTPRequest = new NettyHttpRequest // FIXME new HTTPRequestServlet(httpReq, this)
val httpResponse = new MyResponse
handleLoanWrappers(service(httpRequest, httpResponse) {
doNotHandled()
})}))
} catch {
case e => e.printStackTrace
}
})
/*
val response: HttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
val future = e.getChannel.write(response)
future.addListener(ChannelFutureListener.CLOSE)
*/
}
override def exceptionCaught(ctx: ChannelHandlerContext, e: ExceptionEvent) {
println(e.getCause)
e.getChannel.close()
}
private def send100Continue(e: MessageEvent) {
val response: HttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)
e.getChannel.write(response)
}
}