comparison src/luan/lib/HttpLib.java @ 139:3b384dc5ca91

replace WebShell.java with web_shell.luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@140 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 13 Jun 2014 12:26:44 +0000
parents 06159094b802
children f4ce03ff6b2f
comparison
equal deleted inserted replaced
138:06159094b802 139:3b384dc5ca91
8 import java.util.Enumeration; 8 import java.util.Enumeration;
9 import javax.servlet.ServletOutputStream; 9 import javax.servlet.ServletOutputStream;
10 import javax.servlet.http.Cookie; 10 import javax.servlet.http.Cookie;
11 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
13 import luan.LuanState; 14 import luan.LuanState;
14 import luan.LuanFunction; 15 import luan.LuanFunction;
15 import luan.LuanElement; 16 import luan.LuanElement;
16 import luan.LuanException; 17 import luan.LuanException;
17 import luan.LuanTable; 18 import luan.LuanTable;
28 @Override public Object call(LuanState luan,Object[] args) { 29 @Override public Object call(LuanState luan,Object[] args) {
29 return new LuanTable(); // starts empty 30 return new LuanTable(); // starts empty
30 } 31 }
31 }; 32 };
32 33
33 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) 34 public static void service_per_request(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
34 throws LuanException 35 throws LuanException
35 { 36 {
36 LuanFunction fn; 37 LuanFunction fn;
37 synchronized(luan) { 38 synchronized(luan) {
38 Object mod = PackageLib.require(luan,modName); 39 fn = getFn(luan,modName);
39 if( !(mod instanceof LuanFunction) )
40 throw luan.exception( "module '"+modName+"' must return a function" );
41 fn = (LuanFunction)mod;
42 DeepCloner cloner = new DeepCloner(); 40 DeepCloner cloner = new DeepCloner();
43 luan = cloner.deepClone(luan); 41 luan = cloner.deepClone(luan);
44 fn = cloner.get(fn); 42 fn = cloner.get(fn);
45 } 43 }
46 44 service(luan,request,response,fn);
45 }
46
47 public static void service_global(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
48 throws LuanException
49 {
50 LuanFunction fn = getFn(luan,modName);
51 service(luan,request,response,fn);
52 }
53
54 public static void service_per_session(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
55 throws LuanException
56 {
57 HttpSession session = request.getSession();
58 LuanState sessionLuan = (LuanState)session.getValue("luan");
59 if( sessionLuan!=null ) {
60 luan = sessionLuan;
61 } else {
62 DeepCloner cloner = new DeepCloner();
63 luan = cloner.deepClone(luan);
64 session.putValue("luan",luan);
65 }
66 LuanFunction fn = getFn(luan,modName);
67 service(luan,request,response,fn);
68 }
69
70 private static LuanFunction getFn(LuanState luan,String modName) throws LuanException {
71 Object mod = PackageLib.require(luan,modName);
72 if( !(mod instanceof LuanTable) )
73 throw luan.exception( "module '"+modName+"' must return a table" );
74 LuanTable tbl = (LuanTable)mod;
75 LuanFunction fn = (LuanFunction)tbl.get("page");
76 if( fn == null )
77 throw luan.exception( "function 'page' is not defined" );
78 return fn;
79 }
80
81 private static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,LuanFunction fn)
82 throws LuanException
83 {
47 LuanTable module = (LuanTable)luan.loaded().get(NAME); 84 LuanTable module = (LuanTable)luan.loaded().get(NAME);
48 if( module == null ) 85 if( module == null )
49 throw luan.exception( "module 'Http' not defined" ); 86 throw luan.exception( "module 'Http' not defined" );
50 HttpLib lib = new HttpLib(request,response); 87 HttpLib lib = new HttpLib(request,response);
51 try { 88 try {
86 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) ); 123 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) );
87 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) ); 124 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) );
88 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) ); 125 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) );
89 add( req, "current_url" ); 126 add( req, "current_url" );
90 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) ); 127 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) );
128 add( req, "get_session_attribute", String.class );
129 add( req, "set_session_attribute", String.class, Object.class );
91 return req; 130 return req;
92 } 131 }
93 132
94 private LuanTable responseTable() throws NoSuchMethodException { 133 private LuanTable responseTable() throws NoSuchMethodException {
95 LuanTable resp = new LuanTable(); 134 LuanTable resp = new LuanTable();
146 setCookie(request,response,name,value,isPersistent,domain); 185 setCookie(request,response,name,value,isPersistent,domain);
147 } 186 }
148 187
149 public void remove_cookie(String name, String domain) { 188 public void remove_cookie(String name, String domain) {
150 removeCookie(request,response,name,domain); 189 removeCookie(request,response,name,domain);
190 }
191
192 public Object get_session_attribute(String name) {
193 return request.getSession().getAttribute(name);
194 }
195
196 public void set_session_attribute(String name,Object value) {
197 request.getSession().setAttribute(name,value);
151 } 198 }
152 199
153 200
154 // static utils 201 // static utils
155 202