Mercurial Hosting > luan
annotate web/src/luan/modules/web/LuanServlet.java @ 467:f86bf532f87c
improve LuanServlet to allow for URL mapping
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 08 May 2015 12:10:53 -0600 |
parents | 8f1be9704726 |
children |
rev | line source |
---|---|
399 | 1 package luan.modules.web; |
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.LuanState; | |
9 import luan.LuanException; | |
10 | |
11 | |
12 public class LuanServlet extends HttpServlet { | |
13 protected final String uriPrefix; | |
14 protected final LuanState luan; | |
15 | |
16 public LuanServlet(String uriPrefix,LuanState luan) { | |
17 this.uriPrefix = uriPrefix; | |
18 this.luan = luan; | |
19 } | |
20 | |
21 public LuanServlet(String uriPrefix) { | |
400
8f1be9704726
change LuanState.newStandard() to newInstance() which doesn't preload anything
Franklin Schmidt <fschmidt@gmail.com>
parents:
399
diff
changeset
|
22 this(uriPrefix,LuanState.newInstance()); |
399 | 23 } |
24 | |
467
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
25 @Override protected void service(HttpServletRequest request,HttpServletResponse response) |
399 | 26 throws IOException |
27 { | |
28 String path = request.getRequestURI(); | |
467
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
29 service(request,response,path); |
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
30 } |
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
31 |
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
32 public void service(HttpServletRequest request,HttpServletResponse response,String path) |
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
33 throws IOException |
f86bf532f87c
improve LuanServlet to allow for URL mapping
Franklin Schmidt <fschmidt@gmail.com>
parents:
400
diff
changeset
|
34 { |
399 | 35 if( !path.endsWith(".luan") ) |
36 throw new RuntimeException("'"+path+"' doesn't end with '.luan'"); | |
37 String uri = uriPrefix + path.substring(0,path.length()-5); | |
38 try { | |
39 if( !HttpServicer.service(luan,request,response,uri) ) | |
40 response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
41 } catch(LuanException e) { | |
42 throw new RuntimeException(e); | |
43 } | |
44 } | |
45 | |
46 } |