comparison web/src/luan/modules/web/HttpLuan.java @ 226:392105b660d7

add LuanProperty git-svn-id: https://luan-java.googlecode.com/svn/trunk@227 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 22 Jul 2014 06:23:13 +0000
parents 7c768f63bbb3
children c0f87c1ba99f
comparison
equal deleted inserted replaced
225:7c768f63bbb3 226:392105b660d7
20 import luan.LuanException; 20 import luan.LuanException;
21 import luan.LuanTable; 21 import luan.LuanTable;
22 import luan.AbstractLuanTable; 22 import luan.AbstractLuanTable;
23 import luan.LuanJavaFunction; 23 import luan.LuanJavaFunction;
24 import luan.LuanExitException; 24 import luan.LuanExitException;
25 import luan.LuanProperty;
25 import luan.DeepCloner; 26 import luan.DeepCloner;
26 import luan.modules.PackageLuan; 27 import luan.modules.PackageLuan;
27 import luan.modules.IoLuan; 28 import luan.modules.IoLuan;
28 import luan.modules.TableLuan; 29 import luan.modules.TableLuan;
29 30
115 this.request = request; 116 this.request = request;
116 this.response = response; 117 this.response = response;
117 } 118 }
118 119
119 private LuanTable requestTable() throws NoSuchMethodException { 120 private LuanTable requestTable() throws NoSuchMethodException {
120 LuanTable tbl = Luan.newTable(); 121 LuanTable tbl = Luan.newPropertyTable();
121 tbl.put("java",request); 122 tbl.put("java",request);
122 LuanTable parameters = new NameTable() { 123 LuanTable parameters = new NameTable() {
123 124
124 @Override Object get(String name) { 125 @Override Object get(String name) {
125 return request.getParameter(name); 126 return request.getParameter(name);
148 @Override protected String type() { 149 @Override protected String type() {
149 return "request.headers-table"; 150 return "request.headers-table";
150 } 151 }
151 }; 152 };
152 tbl.put( "headers", headers ); 153 tbl.put( "headers", headers );
153 tbl.put( "get_method", new LuanJavaFunction( 154 tbl.put( "method", new LuanProperty(){ public Object get() {
154 HttpServletRequest.class.getMethod("getMethod"), request 155 return request.getMethod();
155 ) ); 156 } } );
156 tbl.put( "get_servlet_path", new LuanJavaFunction( 157 tbl.put( "servlet_path", new LuanProperty(){ public Object get() {
157 HttpServletRequest.class.getMethod("getServletPath"), request 158 return request.getServletPath();
158 ) ); 159 } } );
159 tbl.put( "get_server_name", new LuanJavaFunction( 160 tbl.put( "server_name", new LuanProperty(){ public Object get() {
160 HttpServletRequest.class.getMethod("getServerName"), request 161 return request.getServerName();
161 ) ); 162 } } );
162 add( tbl, "get_current_url" ); 163 tbl.put( "current_url", new LuanProperty(){ public Object get() {
163 tbl.put( "get_remote_address", new LuanJavaFunction( 164 return getCurrentURL(request);
164 HttpServletRequest.class.getMethod("getRemoteAddr"), request 165 } } );
165 ) ); 166 tbl.put( "remote_address", new LuanProperty(){ public Object get() {
167 return request.getRemoteAddr();
168 } } );
166 LuanTable cookies = new AbstractLuanTable() { 169 LuanTable cookies = new AbstractLuanTable() {
167 170
168 @Override public final Object get(Object key) { 171 @Override public final Object get(Object key) {
169 if( !(key instanceof String) ) 172 if( !(key instanceof String) )
170 return null; 173 return null;
199 tbl.put( "cookies", cookies ); 202 tbl.put( "cookies", cookies );
200 return tbl; 203 return tbl;
201 } 204 }
202 205
203 private LuanTable responseTable() throws NoSuchMethodException { 206 private LuanTable responseTable() throws NoSuchMethodException {
204 LuanTable tbl = Luan.newTable(); 207 LuanTable tbl = Luan.newPropertyTable();
205 tbl.put("java",response); 208 tbl.put("java",response);
206 add( tbl, "send_redirect", String.class ); 209 add( tbl, "send_redirect", String.class );
207 add( tbl, "send_error", Integer.TYPE, String.class ); 210 add( tbl, "send_error", Integer.TYPE, String.class );
208 LuanTable headers = new NameTable() { 211 LuanTable headers = new NameTable() {
209 212
234 @Override protected String type() { 237 @Override protected String type() {
235 return "response.headers-table"; 238 return "response.headers-table";
236 } 239 }
237 }; 240 };
238 tbl.put( "headers", headers ); 241 tbl.put( "headers", headers );
239 tbl.put( "set_content_type", new LuanJavaFunction( 242 tbl.put( "content_type", new LuanProperty(){
240 HttpServletResponse.class.getMethod("setContentType",String.class), response 243 @Override public Object get() {
241 ) ); 244 return response.getContentType();
242 tbl.put( "set_character_encoding", new LuanJavaFunction( 245 }
243 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response 246 @Override public boolean set(Object value) {
244 ) ); 247 response.setContentType(string(value)); return true;
248 }
249 } );
250 tbl.put( "character_encoding", new LuanProperty(){
251 @Override public Object get() {
252 return response.getCharacterEncoding();
253 }
254 @Override public boolean set(Object value) {
255 response.setCharacterEncoding(string(value)); return true;
256 }
257 } );
245 add( tbl, "text_writer" ); 258 add( tbl, "text_writer" );
246 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); 259 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
247 add( tbl, "remove_cookie", String.class, String.class ); 260 add( tbl, "remove_cookie", String.class, String.class );
248 return tbl; 261 return tbl;
249 } 262 }
292 } 305 }
293 306
294 public LuanTable get_parameter_values(String name) { 307 public LuanTable get_parameter_values(String name) {
295 Object[] a = request.getParameterValues(name); 308 Object[] a = request.getParameterValues(name);
296 return a==null ? null : TableLuan.pack(a); 309 return a==null ? null : TableLuan.pack(a);
297 }
298
299 public String get_current_url() {
300 return getCurrentURL(request);
301 } 310 }
302 311
303 public void send_redirect(String redirectUrl) 312 public void send_redirect(String redirectUrl)
304 throws IOException 313 throws IOException
305 { 314 {
476 } 485 }
477 }; 486 };
478 } 487 }
479 }; 488 };
480 489
481 490 private static String string(Object value) {
491 if( !(value instanceof String) )
492 throw new IllegalArgumentException("value must be string");
493 return (String)value;
494 }
482 } 495 }