changeset 225:7c768f63bbb3

better cookie API git-svn-id: https://luan-java.googlecode.com/svn/trunk@226 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 22 Jul 2014 03:06:27 +0000
parents 05eb2837ddbf
children 392105b660d7
files web/src/luan/modules/web/HttpLuan.java
diffstat 1 files changed, 36 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
diff -r 05eb2837ddbf -r 7c768f63bbb3 web/src/luan/modules/web/HttpLuan.java
--- a/web/src/luan/modules/web/HttpLuan.java	Mon Jul 21 09:27:06 2014 +0000
+++ b/web/src/luan/modules/web/HttpLuan.java	Tue Jul 22 03:06:27 2014 +0000
@@ -90,7 +90,6 @@
 		try {
 			module.put( "request", lib.requestTable() );
 			module.put( "response", lib.responseTable() );
-			module.put( "cookie", lib.cookieTable() );
 			module.put( "session", lib.sessionTable() );
 /*
 			module.put( "write", new LuanJavaFunction(
@@ -164,6 +163,40 @@
 		tbl.put( "get_remote_address", new LuanJavaFunction(
 			HttpServletRequest.class.getMethod("getRemoteAddr"), request
 		) );
+		LuanTable cookies = new AbstractLuanTable() {
+
+			@Override public final Object get(Object key) {
+				if( !(key instanceof String) )
+					return null;
+				String name = (String)key;
+				return getCookieValue(request,name);
+			}
+
+			@Override public final Iterator<Map.Entry<Object,Object>> iterator() {
+				return new Iterator<Map.Entry<Object,Object>>() {
+					final Cookie[] cookies = request.getCookies();
+					int i = 0;
+	
+					@Override public boolean hasNext() {
+						return i < cookies.length;
+					}
+					@Override public Map.Entry<Object,Object> next() {
+						Cookie cookie = cookies[i++];
+						String name = cookie.getName();
+						Object val = unescape(cookie.getValue());
+						return new AbstractMap.SimpleEntry<Object,Object>(name,val);
+					}
+					@Override public void remove() {
+						throw new UnsupportedOperationException();
+					}
+				};
+			}
+
+			@Override protected String type() {
+				return "request.cookies-table";
+			}
+		};
+		tbl.put( "cookies", cookies );
 		return tbl;
 	}
 
@@ -210,20 +243,8 @@
 			HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response
 		) );
 		add( tbl, "text_writer" );
-		return tbl;
-	}
-
-	private LuanTable cookieTable() throws NoSuchMethodException {
-		LuanTable tbl = Luan.newTable();
-		tbl.put( "get", new LuanJavaFunction(
-			HttpLuan.class.getMethod("get_cookie",String.class), this
-		) );
-		tbl.put( "set", new LuanJavaFunction(
-			HttpLuan.class.getMethod("set_cookie", String.class, String.class, Boolean.TYPE, String.class), this
-		) );
-		tbl.put( "remove", new LuanJavaFunction(
-			HttpLuan.class.getMethod("remove_cookie", String.class, String.class), this
-		) );
+		add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
+		add( tbl, "remove_cookie", String.class, String.class );
 		return tbl;
 	}
 
@@ -275,10 +296,6 @@
 		return a==null ? null : TableLuan.pack(a);
 	}
 
-	public String get_cookie(String name) {
-		return getCookieValue(request, name);
-	}
-
 	public String get_current_url() {
 		return getCurrentURL(request);
 	}
@@ -395,7 +412,6 @@
 									HttpServletResponse response,
 									String name,
 									String domain
-
 	) {
 		Cookie cookie = getCookie(request, name);
 		if(cookie != null) {