Mercurial Hosting > nabble
comparison src/nabble/utils/LuanServlet.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.utils; | |
2 | |
3 import java.io.IOException; | |
4 import javax.servlet.ServletException; | |
5 import javax.servlet.http.HttpServlet; | |
6 import javax.servlet.http.HttpServletRequest; | |
7 import javax.servlet.http.HttpServletResponse; | |
8 import luan.Luan; | |
9 import luan.LuanException; | |
10 import nabble.utils.luan.HttpServicer; | |
11 import org.slf4j.Logger; | |
12 import org.slf4j.LoggerFactory; | |
13 | |
14 | |
15 public class LuanServlet extends HttpServlet { | |
16 private static final Logger logger = LoggerFactory.getLogger(LuanServlet.class); | |
17 protected final String uriPrefix; | |
18 protected final HttpServicer httpServicer; | |
19 | |
20 private LuanServlet(String uriPrefix,Luan luan) { | |
21 this.uriPrefix = uriPrefix; | |
22 try { | |
23 luan.eval("require 'classpath:nabble/utils/init.luan'"); | |
24 } catch(LuanException e) { | |
25 throw new RuntimeException(e); | |
26 } | |
27 this.httpServicer = new HttpServicer(luan); | |
28 } | |
29 | |
30 public LuanServlet(String uriPrefix) { | |
31 this(uriPrefix,new Luan()); | |
32 } | |
33 | |
34 @Override protected void service(HttpServletRequest request,HttpServletResponse response) | |
35 throws IOException | |
36 { | |
37 String path = request.getRequestURI(); | |
38 service(request,response,path); | |
39 } | |
40 | |
41 public void service(HttpServletRequest request,HttpServletResponse response,String path) | |
42 throws IOException | |
43 { | |
44 if( !path.endsWith(".luan") ) | |
45 throw new RuntimeException("'"+path+"' doesn't end with '.luan'"); | |
46 String uri = uriPrefix + path; | |
47 try { | |
48 if( !httpServicer.service(request,response,uri) ) | |
49 response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
50 } catch(LuanException e) { | |
51 // throw new RuntimeException(e); | |
52 String err = e.getLuanStackTraceString(); | |
53 logger.error(err); | |
54 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"<pre>"+err+"</pre>"); | |
55 } | |
56 } | |
57 | |
58 } |