comparison web/src/luan/modules/web/HttpLuan.java @ 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
comparison
equal deleted inserted replaced
224:05eb2837ddbf 225:7c768f63bbb3
88 throw luan.exception( "module 'web/Http' not defined" ); 88 throw luan.exception( "module 'web/Http' not defined" );
89 HttpLuan lib = new HttpLuan(request,response); 89 HttpLuan lib = new HttpLuan(request,response);
90 try { 90 try {
91 module.put( "request", lib.requestTable() ); 91 module.put( "request", lib.requestTable() );
92 module.put( "response", lib.responseTable() ); 92 module.put( "response", lib.responseTable() );
93 module.put( "cookie", lib.cookieTable() );
94 module.put( "session", lib.sessionTable() ); 93 module.put( "session", lib.sessionTable() );
95 /* 94 /*
96 module.put( "write", new LuanJavaFunction( 95 module.put( "write", new LuanJavaFunction(
97 HttpLuan.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib 96 HttpLuan.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib
98 ) ); 97 ) );
162 ) ); 161 ) );
163 add( tbl, "get_current_url" ); 162 add( tbl, "get_current_url" );
164 tbl.put( "get_remote_address", new LuanJavaFunction( 163 tbl.put( "get_remote_address", new LuanJavaFunction(
165 HttpServletRequest.class.getMethod("getRemoteAddr"), request 164 HttpServletRequest.class.getMethod("getRemoteAddr"), request
166 ) ); 165 ) );
166 LuanTable cookies = new AbstractLuanTable() {
167
168 @Override public final Object get(Object key) {
169 if( !(key instanceof String) )
170 return null;
171 String name = (String)key;
172 return getCookieValue(request,name);
173 }
174
175 @Override public final Iterator<Map.Entry<Object,Object>> iterator() {
176 return new Iterator<Map.Entry<Object,Object>>() {
177 final Cookie[] cookies = request.getCookies();
178 int i = 0;
179
180 @Override public boolean hasNext() {
181 return i < cookies.length;
182 }
183 @Override public Map.Entry<Object,Object> next() {
184 Cookie cookie = cookies[i++];
185 String name = cookie.getName();
186 Object val = unescape(cookie.getValue());
187 return new AbstractMap.SimpleEntry<Object,Object>(name,val);
188 }
189 @Override public void remove() {
190 throw new UnsupportedOperationException();
191 }
192 };
193 }
194
195 @Override protected String type() {
196 return "request.cookies-table";
197 }
198 };
199 tbl.put( "cookies", cookies );
167 return tbl; 200 return tbl;
168 } 201 }
169 202
170 private LuanTable responseTable() throws NoSuchMethodException { 203 private LuanTable responseTable() throws NoSuchMethodException {
171 LuanTable tbl = Luan.newTable(); 204 LuanTable tbl = Luan.newTable();
208 ) ); 241 ) );
209 tbl.put( "set_character_encoding", new LuanJavaFunction( 242 tbl.put( "set_character_encoding", new LuanJavaFunction(
210 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response 243 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response
211 ) ); 244 ) );
212 add( tbl, "text_writer" ); 245 add( tbl, "text_writer" );
213 return tbl; 246 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
214 } 247 add( tbl, "remove_cookie", String.class, String.class );
215
216 private LuanTable cookieTable() throws NoSuchMethodException {
217 LuanTable tbl = Luan.newTable();
218 tbl.put( "get", new LuanJavaFunction(
219 HttpLuan.class.getMethod("get_cookie",String.class), this
220 ) );
221 tbl.put( "set", new LuanJavaFunction(
222 HttpLuan.class.getMethod("set_cookie", String.class, String.class, Boolean.TYPE, String.class), this
223 ) );
224 tbl.put( "remove", new LuanJavaFunction(
225 HttpLuan.class.getMethod("remove_cookie", String.class, String.class), this
226 ) );
227 return tbl; 248 return tbl;
228 } 249 }
229 250
230 private LuanTable sessionTable() throws NoSuchMethodException { 251 private LuanTable sessionTable() throws NoSuchMethodException {
231 LuanTable tbl = Luan.newTable(); 252 LuanTable tbl = Luan.newTable();
271 } 292 }
272 293
273 public LuanTable get_parameter_values(String name) { 294 public LuanTable get_parameter_values(String name) {
274 Object[] a = request.getParameterValues(name); 295 Object[] a = request.getParameterValues(name);
275 return a==null ? null : TableLuan.pack(a); 296 return a==null ? null : TableLuan.pack(a);
276 }
277
278 public String get_cookie(String name) {
279 return getCookieValue(request, name);
280 } 297 }
281 298
282 public String get_current_url() { 299 public String get_current_url() {
283 return getCurrentURL(request); 300 return getCurrentURL(request);
284 } 301 }
393 410
394 public static void removeCookie(HttpServletRequest request, 411 public static void removeCookie(HttpServletRequest request,
395 HttpServletResponse response, 412 HttpServletResponse response,
396 String name, 413 String name,
397 String domain 414 String domain
398
399 ) { 415 ) {
400 Cookie cookie = getCookie(request, name); 416 Cookie cookie = getCookie(request, name);
401 if(cookie != null) { 417 if(cookie != null) {
402 Cookie delCookie = new Cookie(name, "delete"); 418 Cookie delCookie = new Cookie(name, "delete");
403 delCookie.setPath("/"); 419 delCookie.setPath("/");