diff web/src/luan/modules/web/HttpServicer.java @ 407:7fd9f1b7b878

replace LuanPropertyTable with LuanPropertyMeta
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 13:01:00 -0600
parents 3e68917a0dc6
children 23b99a5039b5
line wrap: on
line diff
--- a/web/src/luan/modules/web/HttpServicer.java	Wed Apr 29 11:24:00 2015 -0600
+++ b/web/src/luan/modules/web/HttpServicer.java	Wed Apr 29 13:01:00 2015 -0600
@@ -29,7 +29,7 @@
 import luan.LuanMeta;
 import luan.LuanJavaFunction;
 import luan.LuanExitException;
-import luan.LuanProperty;
+import luan.LuanPropertyMeta;
 import luan.DeepCloner;
 import luan.modules.PackageLuan;
 import luan.modules.IoLuan;
@@ -118,7 +118,8 @@
 	}
 
 	private LuanTable requestTable() throws NoSuchMethodException {
-		LuanTable tbl = Luan.newPropertyTable();
+		LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
+		LuanTable tbl = meta.newTable();
 		tbl.put("java",request);
 		LuanTable parameters = new NameMeta() {
 
@@ -153,29 +154,24 @@
 */
 		}.newTable();
 		tbl.put( "headers", headers );
-		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( "path", new LuanProperty() { public Object get() {
-			return request.getRequestURI();
-		} } );
-		tbl.put( "server_name", new LuanProperty() { public Object get() {
-			return request.getServerName();
-		} } );
-		tbl.put( "url", new LuanProperty() { public Object get() {
-			return getURL(request);
-		} } );
-		tbl.put( "query_string", new LuanProperty() { public Object get() {
-			return getQueryString(request);
-		} } );
-		tbl.put( "remote_address", new LuanProperty() { public Object get() {
-			return request.getRemoteAddr();
-		} } );
+		meta.getters().put( "method", new LuanJavaFunction(
+			HttpServletRequest.class.getMethod( "getMethod" ), request
+		) );
+		meta.getters().put( "path", new LuanJavaFunction(
+			HttpServletRequest.class.getMethod( "getRequestURI" ), request
+		) );
+		meta.getters().put( "server_name", new LuanJavaFunction(
+			HttpServletRequest.class.getMethod( "getServerName" ), request
+		) );
+		meta.getters().put( "url", new LuanJavaFunction(
+			HttpServicer.class.getMethod( "getURL" ), this
+		) );
+		meta.getters().put( "query_string", new LuanJavaFunction(
+			HttpServicer.class.getMethod( "getQueryString" ), this
+		) );
+		meta.getters().put( "remote_address", new LuanJavaFunction(
+			HttpServletRequest.class.getMethod( "getRemoteAddr" ), request
+		) );
 		LuanTable cookies = new LuanMeta() {
 
 			@Override public Object __index(LuanState luan,LuanTable tbl,Object key) {
@@ -227,19 +223,22 @@
 					if( filename == null ) {
 						value = new String(part.getBytes());
 					} else {
-						LuanTable partTbl = Luan.newPropertyTable();
+						LuanPropertyMeta partMeta = LuanPropertyMeta.newInstance();
+						LuanTable partTbl = partMeta.newTable();
 						partTbl.put("filename",filename);
 						partTbl.put("content_type",part.getContentType());
-						partTbl.put( "content", new LuanProperty() { public Object get() {
-							try {
-								InputStream in = part.getInputStream();
-								byte[] content = Utils.readAll(in);
-								in.close();
-								return content;
-							} catch(IOException e) {
-								throw new RuntimeException(e);
+						partMeta.getters().put( "content", new LuanFunction() {
+							@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+								try {
+									InputStream in = part.getInputStream();
+									byte[] content = Utils.readAll(in);
+									in.close();
+									return content;
+								} catch(IOException e) {
+									throw new RuntimeException(e);
+								}
 							}
-						} } );
+						} );
 						value = partTbl;
 					}
 					Object old = parameters.get(name);
@@ -267,7 +266,8 @@
 	}
 
 	private LuanTable responseTable() throws NoSuchMethodException {
-		LuanTable tbl = Luan.newPropertyTable();
+		LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
+		LuanTable tbl = meta.newTable();
 		tbl.put("java",response);
 		add( tbl, "send_redirect", String.class );
 		add( tbl, "send_error", Integer.TYPE, String.class );
@@ -307,37 +307,27 @@
 */
 		}.newTable();
 		tbl.put( "headers", headers );
-		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;
-			}
-		} );
+		meta.getters().put( "content_type", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "getContentType" ), response
+		) );
+		meta.setters().put( "content_type", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "setContentType", String.class ), response
+		) );
+		meta.getters().put( "character_encoding", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response
+		) );
+		meta.setters().put( "character_encoding", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response
+		) );
 		add( tbl, "text_writer" );
 		add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
 		add( tbl, "remove_cookie", String.class, String.class );
-		tbl.put( "status", new LuanProperty() {
-			@Override public Object get() {
-				return response.getStatus();
-			}
-			@Override public boolean set(Object value) {
-				Integer i = Luan.asInteger(value);
-				if( i==null )
-					throw new IllegalArgumentException("value must be an integer");
-				response.setStatus(i);
-				return true;
-			}
-		} );
+		meta.getters().put( "status", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "getStatus" ), response
+		) );
+		meta.setters().put( "status", new LuanJavaFunction(
+			HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response
+		) );
 		return tbl;
 	}
 
@@ -414,6 +404,10 @@
 
 	// static utils
 
+	public String getQueryString() {
+		return getQueryString(request);
+	}
+
 	public static String getQueryString(HttpServletRequest request) {
 		return getQueryString(request,0);
 	}
@@ -445,6 +439,10 @@
 		return queryBuf.toString();
 	}
 
+	public  String getURL() {
+		return getURL(request);
+	}
+
 	public static String getURL(HttpServletRequest request) {
 		return getURL(request,0);
 	}