comparison src/luan/lib/HttpLib.java @ 136:7e160d2f6d9c

update HttpLib, untested git-svn-id: https://luan-java.googlecode.com/svn/trunk@137 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 11 Jun 2014 09:33:02 +0000
parents 3119326260ea
children 06159094b802
comparison
equal deleted inserted replaced
135:3119326260ea 136:7e160d2f6d9c
1 package luan.lib; 1 package luan.lib;
2 2
3 import java.io.PrintStream; 3 import java.io.PrintWriter;
4 import java.io.IOException; 4 import java.io.IOException;
5 import java.util.Map; 5 import java.util.Map;
6 import java.util.Set; 6 import java.util.Set;
7 import java.util.Arrays; 7 import java.util.Arrays;
8 import java.util.Enumeration; 8 import java.util.Enumeration;
15 import luan.LuanElement; 15 import luan.LuanElement;
16 import luan.LuanException; 16 import luan.LuanException;
17 import luan.LuanTable; 17 import luan.LuanTable;
18 import luan.LuanJavaFunction; 18 import luan.LuanJavaFunction;
19 import luan.LuanExitException; 19 import luan.LuanExitException;
20 import luan.DeepCloner;
20 21
21 22
22 public final class HttpLib { 23 public final class HttpLib {
23 24
24 public static final String NAME = "Http"; 25 public static final String NAME = "Http";
25 public static final String FN_NAME = "Http.server"; 26
26 27 public static final LuanFunction LOADER = new LuanFunction() {
27 public static void load(LuanState luan) throws LuanException { 28 @Override public Object call(LuanState luan,Object[] args) {
28 PackageLib.require(luan,NAME); 29 return new LuanTable(); // starts empty
29 Object fn = luan.get(HttpLib.FN_NAME); 30 }
30 if( !(fn instanceof LuanFunction) ) 31 };
31 throw luan.exception( "function '"+HttpLib.FN_NAME+"' not defined" ); 32
32 } 33 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
33 34 throws LuanException
34 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response)
35 throws LuanException, IOException
36 { 35 {
37 LuanFunction fn = (LuanFunction)luan.get(FN_NAME); 36 LuanState newLuan;
38 ServletOutputStream sout = response.getOutputStream(); 37 LuanFunction newFn;
39 luan.set( "Io.stdout", IoLib.textWriter(new PrintStream(sout)) ); 38 synchronized(luan) {
39 Object mod = PackageLib.require(luan,modName);
40 if( !(mod instanceof LuanFunction) )
41 throw luan.exception( "module '"+modName+"' must return a function" );
42 LuanFunction fn = (LuanFunction)mod;
43 DeepCloner cloner = new DeepCloner();
44 newLuan = cloner.deepClone(luan);
45 newFn = cloner.get(fn);
46 }
40 47
41 LuanTable module = (LuanTable)luan.loaded().get(NAME); 48 LuanTable module = (LuanTable)luan.loaded().get(NAME);
42 49 if( module == null )
50 throw luan.exception( "module 'Http' not defined" );
51 HttpLib lib = new HttpLib(request,response);
43 try { 52 try {
44 new HttpLib(request,response,module); 53 module.put( "request", lib.requestTable() );
54 module.put( "response", lib.responseTable() );
55 module.put( "write", new LuanJavaFunction(
56 HttpLib.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib
57 ) );
45 } catch(NoSuchMethodException e) { 58 } catch(NoSuchMethodException e) {
46 throw new RuntimeException(e); 59 throw new RuntimeException(e);
47 } 60 }
48 61
49 luan.call(fn,FN_NAME); 62 newLuan.call(newFn,"<http>");
50 } 63 }
64
65
51 66
52 private final HttpServletRequest request; 67 private final HttpServletRequest request;
53 private final HttpServletResponse response; 68 private final HttpServletResponse response;
54 69 private PrintWriter writer = null;
55 private HttpLib(HttpServletRequest request,HttpServletResponse response,LuanTable module) throws NoSuchMethodException { 70 // private ServletOutputStream sos = null;
71
72 private HttpLib(HttpServletRequest request,HttpServletResponse response) {
56 this.request = request; 73 this.request = request;
57 this.response = response; 74 this.response = response;
58 75 }
76
77 private LuanTable requestTable() throws NoSuchMethodException {
59 LuanTable req = new LuanTable(); 78 LuanTable req = new LuanTable();
60 module.put("request",req);
61 LuanTable resp = new LuanTable();
62 module.put("response",resp);
63
64 req.put( "get_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("getAttribute",String.class),request) ); 79 req.put( "get_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("getAttribute",String.class),request) );
65 req.put( "set_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("setAttribute",String.class,Object.class),request) ); 80 req.put( "set_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("setAttribute",String.class,Object.class),request) );
66 req.put( "get_parameter", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameter",String.class),request) ); 81 req.put( "get_parameter", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameter",String.class),request) );
67 req.put( "get_parameter_values", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameterValues",String.class),request) ); 82 req.put( "get_parameter_values", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameterValues",String.class),request) );
68 req.put( "get_header", new LuanJavaFunction(HttpServletRequest.class.getMethod("getHeader",String.class),request) ); 83 req.put( "get_header", new LuanJavaFunction(HttpServletRequest.class.getMethod("getHeader",String.class),request) );
70 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) ); 85 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) );
71 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) ); 86 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) );
72 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) ); 87 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) );
73 add( req, "current_url" ); 88 add( req, "current_url" );
74 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) ); 89 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) );
75 90 return req;
91 }
92
93 private LuanTable responseTable() throws NoSuchMethodException {
94 LuanTable resp = new LuanTable();
76 add( resp, "send_redirect", String.class ); 95 add( resp, "send_redirect", String.class );
77 add( resp, "send_error", Integer.TYPE, String.class ); 96 add( resp, "send_error", Integer.TYPE, String.class );
78 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) ); 97 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) );
79 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); 98 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) );
80 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); 99 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
81 add( resp, "remove_cookie", String.class, String.class ); 100 add( resp, "remove_cookie", String.class, String.class );
101 return resp;
82 } 102 }
83 103
84 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 104 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
85 t.put( method, new LuanJavaFunction(HttpLib.class.getMethod(method,parameterTypes),this) ); 105 t.put( method, new LuanJavaFunction(HttpLib.class.getMethod(method,parameterTypes),this) );
106 }
107
108 public void text_write(LuanState luan,Object... args) throws LuanException, IOException {
109 if( writer == null )
110 writer = response.getWriter();
111 for( Object obj : args ) {
112 writer.print( luan.toString(obj) );
113 }
86 } 114 }
87 115
88 public String get_cookie_value(String name) { 116 public String get_cookie_value(String name) {
89 return getCookieValue(request, name); 117 return getCookieValue(request, name);
90 } 118 }