Mercurial Hosting > luan
comparison web/src/luan/modules/web/HttpLuan.java @ 222:b76fcb72d97d
add AbstractLuanTable and improve HttpLuan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@223 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 21 Jul 2014 02:23:05 +0000 |
parents | ec016471c6eb |
children | a9fe265dbac3 |
comparison
equal
deleted
inserted
replaced
221:ec016471c6eb | 222:b76fcb72d97d |
---|---|
1 package luan.modules.web; | 1 package luan.modules.web; |
2 | 2 |
3 import java.io.PrintWriter; | 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.AbstractMap; | |
6 import java.util.Set; | 7 import java.util.Set; |
7 import java.util.Arrays; | 8 import java.util.Arrays; |
9 import java.util.Iterator; | |
8 import java.util.Enumeration; | 10 import java.util.Enumeration; |
9 import javax.servlet.ServletOutputStream; | 11 import javax.servlet.ServletOutputStream; |
10 import javax.servlet.http.Cookie; | 12 import javax.servlet.http.Cookie; |
11 import javax.servlet.http.HttpServletRequest; | 13 import javax.servlet.http.HttpServletRequest; |
12 import javax.servlet.http.HttpServletResponse; | 14 import javax.servlet.http.HttpServletResponse; |
15 import luan.LuanState; | 17 import luan.LuanState; |
16 import luan.LuanFunction; | 18 import luan.LuanFunction; |
17 import luan.LuanElement; | 19 import luan.LuanElement; |
18 import luan.LuanException; | 20 import luan.LuanException; |
19 import luan.LuanTable; | 21 import luan.LuanTable; |
22 import luan.AbstractLuanTable; | |
20 import luan.LuanJavaFunction; | 23 import luan.LuanJavaFunction; |
21 import luan.LuanExitException; | 24 import luan.LuanExitException; |
22 import luan.DeepCloner; | 25 import luan.DeepCloner; |
23 import luan.modules.PackageLuan; | 26 import luan.modules.PackageLuan; |
24 import luan.modules.IoLuan; | 27 import luan.modules.IoLuan; |
28 import luan.modules.TableLuan; | |
25 | 29 |
26 | 30 |
27 public final class HttpLuan { | 31 public final class HttpLuan { |
28 | 32 |
29 public static final LuanFunction LOADER = new LuanFunction() { | 33 public static final LuanFunction LOADER = new LuanFunction() { |
114 } | 118 } |
115 | 119 |
116 private LuanTable requestTable() throws NoSuchMethodException { | 120 private LuanTable requestTable() throws NoSuchMethodException { |
117 LuanTable tbl = Luan.newTable(); | 121 LuanTable tbl = Luan.newTable(); |
118 tbl.put("java",request); | 122 tbl.put("java",request); |
119 add( tbl, "get_parameter", String.class ); | 123 LuanTable parameters = new NameTable() { |
120 tbl.put( "get_header", new LuanJavaFunction( | 124 |
121 HttpServletRequest.class.getMethod("getHeader",String.class), request | 125 @Override Object get(String name) { |
122 ) ); | 126 return request.getParameter(name); |
127 } | |
128 | |
129 @Override Iterator<String> names() { | |
130 return new EnumerationIterator(request.getParameterNames()); | |
131 } | |
132 | |
133 @Override protected String type() { | |
134 return "request.parameters-table"; | |
135 } | |
136 }; | |
137 tbl.put( "parameters", parameters ); | |
138 add( tbl, "get_parameter_values", String.class ); | |
139 LuanTable headers = new NameTable() { | |
140 | |
141 @Override Object get(String name) { | |
142 return request.getHeader(name); | |
143 } | |
144 | |
145 @Override Iterator<String> names() { | |
146 return new EnumerationIterator(request.getHeaderNames()); | |
147 } | |
148 | |
149 @Override protected String type() { | |
150 return "request.headers-table"; | |
151 } | |
152 }; | |
153 tbl.put( "headers", headers ); | |
123 tbl.put( "get_method", new LuanJavaFunction( | 154 tbl.put( "get_method", new LuanJavaFunction( |
124 HttpServletRequest.class.getMethod("getMethod"), request | 155 HttpServletRequest.class.getMethod("getMethod"), request |
125 ) ); | 156 ) ); |
126 tbl.put( "get_servlet_path", new LuanJavaFunction( | 157 tbl.put( "get_servlet_path", new LuanJavaFunction( |
127 HttpServletRequest.class.getMethod("getServletPath"), request | 158 HttpServletRequest.class.getMethod("getServletPath"), request |
145 HttpServletResponse.class.getMethod("containsHeader",String.class), response | 176 HttpServletResponse.class.getMethod("containsHeader",String.class), response |
146 ) ); | 177 ) ); |
147 tbl.put( "set_header", new LuanJavaFunction( | 178 tbl.put( "set_header", new LuanJavaFunction( |
148 HttpServletResponse.class.getMethod("setHeader",String.class,String.class), response | 179 HttpServletResponse.class.getMethod("setHeader",String.class,String.class), response |
149 ) ); | 180 ) ); |
181 /* | |
182 LuanTable headers = new NameTable() { | |
183 | |
184 @Override Object get(String name) { | |
185 return response.getHeader(name); | |
186 } | |
187 | |
188 @Override Iterator<String> names() { | |
189 return response.getHeaderNames().iterator(); | |
190 } | |
191 | |
192 @Override protected String type() { | |
193 return "response.headers-table"; | |
194 } | |
195 }; | |
196 tbl.put( "headers", headers ); | |
197 */ | |
150 tbl.put( "set_content_type", new LuanJavaFunction( | 198 tbl.put( "set_content_type", new LuanJavaFunction( |
151 HttpServletResponse.class.getMethod("setContentType",String.class), response | 199 HttpServletResponse.class.getMethod("setContentType",String.class), response |
152 ) ); | 200 ) ); |
153 tbl.put( "set_character_encoding", new LuanJavaFunction( | 201 tbl.put( "set_character_encoding", new LuanJavaFunction( |
154 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response | 202 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response |
171 return tbl; | 219 return tbl; |
172 } | 220 } |
173 | 221 |
174 private LuanTable sessionTable() throws NoSuchMethodException { | 222 private LuanTable sessionTable() throws NoSuchMethodException { |
175 LuanTable tbl = Luan.newTable(); | 223 LuanTable tbl = Luan.newTable(); |
176 tbl.put( "get_attribute", new LuanJavaFunction( | 224 LuanTable attributes = new NameTable() { |
177 HttpLuan.class.getMethod("get_session_attribute",String.class), this | 225 |
178 ) ); | 226 @Override Object get(String name) { |
179 tbl.put( "set_attribute", new LuanJavaFunction( | 227 return request.getSession().getAttribute(name); |
180 HttpLuan.class.getMethod("set_session_attribute",String.class, Object.class), this | 228 } |
181 ) ); | 229 |
230 @Override Iterator<String> names() { | |
231 return new EnumerationIterator(request.getSession().getAttributeNames()); | |
232 } | |
233 | |
234 @Override public Object put(Object key,Object val) { | |
235 if( !(key instanceof String) ) | |
236 throw new IllegalArgumentException("key must be string for session attributes table"); | |
237 String name = (String)key; | |
238 request.getSession().setAttribute(name,val); | |
239 return null; | |
240 } | |
241 | |
242 @Override protected String type() { | |
243 return "session.attributes-table"; | |
244 } | |
245 }; | |
246 tbl.put( "attributes", attributes ); | |
182 return tbl; | 247 return tbl; |
183 } | 248 } |
184 | 249 |
185 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 250 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
186 t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),this) ); | 251 t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),this) ); |
196 */ | 261 */ |
197 public LuanTable text_writer() throws IOException { | 262 public LuanTable text_writer() throws IOException { |
198 return IoLuan.textWriter(response.getWriter()); | 263 return IoLuan.textWriter(response.getWriter()); |
199 } | 264 } |
200 | 265 |
201 public Object get_parameter(String name) { | 266 public LuanTable get_parameter_values(String name) { |
202 String[] a = request.getParameterValues(name); | 267 Object[] a = request.getParameterValues(name); |
203 return a==null ? null : a.length==1 ? a[0] : a; | 268 return a==null ? null : TableLuan.pack(a); |
204 } | 269 } |
205 | 270 |
206 public String get_cookie(String name) { | 271 public String get_cookie(String name) { |
207 return getCookieValue(request, name); | 272 return getCookieValue(request, name); |
208 } | 273 } |
229 setCookie(request,response,name,value,isPersistent,domain); | 294 setCookie(request,response,name,value,isPersistent,domain); |
230 } | 295 } |
231 | 296 |
232 public void remove_cookie(String name, String domain) { | 297 public void remove_cookie(String name, String domain) { |
233 removeCookie(request,response,name,domain); | 298 removeCookie(request,response,name,domain); |
234 } | |
235 | |
236 public Object get_session_attribute(String name) { | |
237 return request.getSession().getAttribute(name); | |
238 } | |
239 | |
240 public void set_session_attribute(String name,Object value) { | |
241 request.getSession().setAttribute(name,value); | |
242 } | 299 } |
243 | 300 |
244 | 301 |
245 // static utils | 302 // static utils |
246 | 303 |
342 delCookie.setDomain(domain); | 399 delCookie.setDomain(domain); |
343 response.addCookie(delCookie); | 400 response.addCookie(delCookie); |
344 } | 401 } |
345 } | 402 } |
346 | 403 |
404 | |
405 | |
406 // util classes | |
407 | |
408 static final class EnumerationIterator<E> implements Iterator<E> { | |
409 private final Enumeration<E> en; | |
410 | |
411 EnumerationIterator(Enumeration<E> en) { | |
412 this.en = en; | |
413 } | |
414 | |
415 @Override public boolean hasNext() { | |
416 return en.hasMoreElements(); | |
417 } | |
418 | |
419 @Override public E next() { | |
420 return en.nextElement(); | |
421 } | |
422 | |
423 @Override public void remove() { | |
424 throw new UnsupportedOperationException(); | |
425 } | |
426 } | |
427 | |
428 private static abstract class NameTable extends AbstractLuanTable { | |
429 abstract Object get(String name); | |
430 abstract Iterator<String> names(); | |
431 | |
432 @Override public final Object get(Object key) { | |
433 if( !(key instanceof String) ) | |
434 return null; | |
435 String name = (String)key; | |
436 return get(name); | |
437 } | |
438 | |
439 @Override public final Iterator<Map.Entry<Object,Object>> iterator() { | |
440 return new Iterator<Map.Entry<Object,Object>>() { | |
441 Iterator<String> names = names(); | |
442 | |
443 @Override public boolean hasNext() { | |
444 return names.hasNext(); | |
445 } | |
446 @Override public Map.Entry<Object,Object> next() { | |
447 String name = names.next(); | |
448 Object val = get(name); | |
449 return new AbstractMap.SimpleEntry<Object,Object>(name,val); | |
450 } | |
451 @Override public void remove() { | |
452 throw new UnsupportedOperationException(); | |
453 } | |
454 }; | |
455 } | |
456 }; | |
457 | |
458 | |
347 } | 459 } |