Mercurial Hosting > nabble
view 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 |
line wrap: on
line source
package nabble.utils; 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.Luan; import luan.LuanException; import nabble.utils.luan.HttpServicer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LuanServlet extends HttpServlet { private static final Logger logger = LoggerFactory.getLogger(LuanServlet.class); protected final String uriPrefix; protected final HttpServicer httpServicer; private LuanServlet(String uriPrefix,Luan luan) { this.uriPrefix = uriPrefix; try { luan.eval("require 'classpath:nabble/utils/init.luan'"); } catch(LuanException e) { throw new RuntimeException(e); } this.httpServicer = new HttpServicer(luan); } public LuanServlet(String uriPrefix) { this(uriPrefix,new Luan()); } @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; try { if( !httpServicer.service(request,response,uri) ) response.sendError(HttpServletResponse.SC_NOT_FOUND); } catch(LuanException e) { // throw new RuntimeException(e); String err = e.getLuanStackTraceString(); logger.error(err); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"<pre>"+err+"</pre>"); } } }