diff 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
line wrap: on
line diff
--- a/web/src/luan/modules/web/HttpLuan.java	Tue Jul 22 03:06:27 2014 +0000
+++ b/web/src/luan/modules/web/HttpLuan.java	Tue Jul 22 06:23:13 2014 +0000
@@ -22,6 +22,7 @@
 import luan.AbstractLuanTable;
 import luan.LuanJavaFunction;
 import luan.LuanExitException;
+import luan.LuanProperty;
 import luan.DeepCloner;
 import luan.modules.PackageLuan;
 import luan.modules.IoLuan;
@@ -117,7 +118,7 @@
 	}
 
 	private LuanTable requestTable() throws NoSuchMethodException {
-		LuanTable tbl = Luan.newTable();
+		LuanTable tbl = Luan.newPropertyTable();
 		tbl.put("java",request);
 		LuanTable parameters = new NameTable() {
 
@@ -150,19 +151,21 @@
 			}
 		};
 		tbl.put( "headers", headers );
-		tbl.put( "get_method", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod("getMethod"), request
-		) );
-		tbl.put( "get_servlet_path", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod("getServletPath"), request
-		) );
-		tbl.put( "get_server_name", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod("getServerName"), request
-		) );
-		add( tbl, "get_current_url" );
-		tbl.put( "get_remote_address", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod("getRemoteAddr"), request
-		) );
+		tbl.put( "method", new LuanProperty(){ public Object get() {
+			return request.getMethod();
+		} } );
+		tbl.put( "servlet_path", new LuanProperty(){ public Object get() {
+			return request.getServletPath();
+		} } );
+		tbl.put( "server_name", new LuanProperty(){ public Object get() {
+			return request.getServerName();
+		} } );
+		tbl.put( "current_url", new LuanProperty(){ public Object get() {
+			return getCurrentURL(request);
+		} } );
+		tbl.put( "remote_address", new LuanProperty(){ public Object get() {
+			return request.getRemoteAddr();
+		} } );
 		LuanTable cookies = new AbstractLuanTable() {
 
 			@Override public final Object get(Object key) {
@@ -201,7 +204,7 @@
 	}
 
 	private LuanTable responseTable() throws NoSuchMethodException {
-		LuanTable tbl = Luan.newTable();
+		LuanTable tbl = Luan.newPropertyTable();
 		tbl.put("java",response);
 		add( tbl, "send_redirect", String.class );
 		add( tbl, "send_error", Integer.TYPE, String.class );
@@ -236,12 +239,22 @@
 			}
 		};
 		tbl.put( "headers", headers );
-		tbl.put( "set_content_type", new LuanJavaFunction(
-			HttpServletResponse.class.getMethod("setContentType",String.class), response
-		) );
-		tbl.put( "set_character_encoding", new LuanJavaFunction(
-			HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response
-		) );
+		tbl.put( "content_type", new LuanProperty(){
+			@Override public Object get() {
+				return response.getContentType();
+			}
+			@Override public boolean set(Object value) {
+				response.setContentType(string(value));  return true;
+			}
+		} );
+		tbl.put( "character_encoding", new LuanProperty(){
+			@Override public Object get() {
+				return response.getCharacterEncoding();
+			}
+			@Override public boolean set(Object value) {
+				response.setCharacterEncoding(string(value));  return true;
+			}
+		} );
 		add( tbl, "text_writer" );
 		add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
 		add( tbl, "remove_cookie", String.class, String.class );
@@ -296,10 +309,6 @@
 		return a==null ? null : TableLuan.pack(a);
 	}
 
-	public String get_current_url() {
-		return getCurrentURL(request);
-	}
-
 	public void send_redirect(String redirectUrl)
 		throws IOException
 	{
@@ -478,5 +487,9 @@
 		}
 	};
 
-
+	private static String string(Object value) {
+		if( !(value instanceof String) )
+			throw new IllegalArgumentException("value must be string");
+		return (String)value;
+	}
 }