comparison http/src/luan/modules/http/LuanServlet.java @ 494:2b9bc97f0439

change luan:web to luan:http
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 May 2015 17:43:13 -0600
parents http/src/luan/modules/web/LuanServlet.java@1d082a0812e0
children
comparison
equal deleted inserted replaced
493:1d082a0812e0 494:2b9bc97f0439
1 package luan.modules.http;
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) {
22 this(uriPrefix,LuanState.newInstance());
23 }
24
25 @Override protected void service(HttpServletRequest request,HttpServletResponse response)
26 throws IOException
27 {
28 String path = request.getRequestURI();
29 service(request,response,path);
30 }
31
32 public void service(HttpServletRequest request,HttpServletResponse response,String path)
33 throws IOException
34 {
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 }