Mercurial Hosting > luan
changeset 76:97b03fc807ad
add HttpLib and WebServlet
git-svn-id: https://luan-java.googlecode.com/svn/trunk@77 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 15 Feb 2013 04:52:16 +0000 |
parents | aa7538ae5fb6 |
children | 4bf3d0c0b6b9 |
files | src/luan/LuanRuntimeException.java src/luan/lib/HttpLib.java src/luan/lib/LuanRuntimeException.java src/luan/lib/TableLib.java src/luan/tools/WebServlet.java |
diffstat | 5 files changed, 132 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/LuanRuntimeException.java Fri Feb 15 04:52:16 2013 +0000 @@ -0,0 +1,8 @@ +package luan; + + +public final class LuanRuntimeException extends RuntimeException { + public LuanRuntimeException(LuanException e) { + super(e); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/HttpLib.java Fri Feb 15 04:52:16 2013 +0000 @@ -0,0 +1,57 @@ +package luan.lib; + +import java.io.PrintStream; +import java.io.IOException; +import java.util.Map; +import java.util.Set; +import java.util.Arrays; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import luan.LuanState; +import luan.LuanFunction; +import luan.LuanElement; +import luan.LuanException; +import luan.LuanTable; + + +public final class HttpLib { + + public static final String NAME = "http"; + public static final String FN_NAME = "serve_http"; + + public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response) + throws LuanException, IOException + { + LuanFunction fn = (LuanFunction)luan.global.get(FN_NAME); + ServletOutputStream sout = response.getOutputStream(); + luan.out = new PrintStream(sout); + + LuanTable module = new LuanTable(); + luan.global.put(NAME,module); + + LuanTable parameters = new LuanTable(); + LuanTable parameter_lists = new LuanTable(); + @SuppressWarnings("unchecked") + Map<String,String[]> paramMap = request.getParameterMap(); + for( Map.Entry<String,String[]> entry : paramMap.entrySet() ) { + String name = entry.getKey(); + String[] values = entry.getValue(); + parameters.put(name,values[0]); + parameter_lists.put( name, new LuanTable(Arrays.asList((Object[])values)) ); + } + module.put("parameters",parameters); + module.put("parameter_lists",parameter_lists); + + luan.call(fn,LuanElement.JAVA,FN_NAME); + } +/* + private final HttpServletRequest request; + private final HttpServletResponse response; + + private HttpLib(HttpServletRequest request,HttpServletResponse response) { + this.request = request; + this.response = response; + } +*/ +}
--- a/src/luan/lib/LuanRuntimeException.java Wed Feb 13 09:19:15 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -package luan.lib; - -import luan.LuanException; - - -public final class LuanRuntimeException extends RuntimeException { - public LuanRuntimeException(LuanException e) { - super(e); - } -}
--- a/src/luan/lib/TableLib.java Wed Feb 13 09:19:15 2013 +0000 +++ b/src/luan/lib/TableLib.java Fri Feb 15 04:52:16 2013 +0000 @@ -10,6 +10,7 @@ import luan.LuanJavaFunction; import luan.LuanElement; import luan.LuanException; +import luan.LuanRuntimeException; public final class TableLib {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/tools/WebServlet.java Fri Feb 15 04:52:16 2013 +0000 @@ -0,0 +1,66 @@ +package luan.tools; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import luan.LuanState; +import luan.LuanException; +import luan.LuanRuntimeException; +import luan.LuanFunction; +import luan.LuanElement; +import luan.interp.LuanCompiler; +import luan.lib.HttpLib; +import luan.lib.BasicLib; +import luan.lib.PackageLib; +import luan.lib.MathLib; +import luan.lib.StringLib; +import luan.lib.TableLib; +import luan.lib.HtmlLib; + + +public class WebServlet extends HttpServlet { + + public static final String HTTP_SERVER = "http_server"; + + protected void loadLibs(LuanState luan) throws LuanException { + luan.load(BasicLib.LOADER,BasicLib.NAME); + luan.load(PackageLib.LOADER,PackageLib.NAME); + luan.load(MathLib.LOADER,MathLib.NAME); + luan.load(StringLib.LOADER,StringLib.NAME); + luan.load(TableLib.LOADER,TableLib.NAME); + luan.load(HtmlLib.LOADER,HtmlLib.NAME); + } + + protected void loadLuan(LuanState luan) throws LuanException { + PackageLib.require(luan,HTTP_SERVER); + Object fn = luan.global.get(HttpLib.FN_NAME); + if( !(fn instanceof LuanFunction) ) + throw new LuanException( luan, LuanElement.JAVA, "function '"+HttpLib.FN_NAME+"' not defined" ); + } + + protected LuanState newLuanState() throws LuanException { + LuanState luan = LuanCompiler.newLuanState(); + loadLibs(luan); + loadLuan(luan); + return luan; + } + + protected LuanState getLuanState() throws LuanException { + return newLuanState(); + } + + protected void service(HttpServletRequest request,HttpServletResponse response) + throws ServletException, IOException + { + try { + LuanState luan = getLuanState(); + HttpLib.service(luan,request,response); + } catch(LuanException e) { + throw new LuanRuntimeException(e); + } + } + +}