Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http/src/luan/modules/http/LuanServlet.java Fri May 15 17:43:13 2015 -0600 @@ -0,0 +1,46 @@ +package luan.modules.http; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import luan.LuanState; +import luan.LuanException; + + +public class LuanServlet extends HttpServlet { + protected final String uriPrefix; + protected final LuanState luan; + + public LuanServlet(String uriPrefix,LuanState luan) { + this.uriPrefix = uriPrefix; + this.luan = luan; + } + + public LuanServlet(String uriPrefix) { + this(uriPrefix,LuanState.newInstance()); + } + + @Override protected void service(HttpServletRequest request,HttpServletResponse response) + throws IOException + { + String path = request.getRequestURI(); + service(request,response,path); + } + + public void service(HttpServletRequest request,HttpServletResponse response,String path) + throws IOException + { + if( !path.endsWith(".luan") ) + throw new RuntimeException("'"+path+"' doesn't end with '.luan'"); + String uri = uriPrefix + path.substring(0,path.length()-5); + try { + if( !HttpServicer.service(luan,request,response,uri) ) + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } catch(LuanException e) { + throw new RuntimeException(e); + } + } + +}