comparison 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
comparison
equal deleted inserted replaced
406:9321a33b9b1c 407:7fd9f1b7b878
27 import luan.LuanException; 27 import luan.LuanException;
28 import luan.LuanTable; 28 import luan.LuanTable;
29 import luan.LuanMeta; 29 import luan.LuanMeta;
30 import luan.LuanJavaFunction; 30 import luan.LuanJavaFunction;
31 import luan.LuanExitException; 31 import luan.LuanExitException;
32 import luan.LuanProperty; 32 import luan.LuanPropertyMeta;
33 import luan.DeepCloner; 33 import luan.DeepCloner;
34 import luan.modules.PackageLuan; 34 import luan.modules.PackageLuan;
35 import luan.modules.IoLuan; 35 import luan.modules.IoLuan;
36 import luan.modules.TableLuan; 36 import luan.modules.TableLuan;
37 import luan.modules.Utils; 37 import luan.modules.Utils;
116 this.request = request; 116 this.request = request;
117 this.response = response; 117 this.response = response;
118 } 118 }
119 119
120 private LuanTable requestTable() throws NoSuchMethodException { 120 private LuanTable requestTable() throws NoSuchMethodException {
121 LuanTable tbl = Luan.newPropertyTable(); 121 LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
122 LuanTable tbl = meta.newTable();
122 tbl.put("java",request); 123 tbl.put("java",request);
123 LuanTable parameters = new NameMeta() { 124 LuanTable parameters = new NameMeta() {
124 125
125 @Override Object get(String name) { 126 @Override Object get(String name) {
126 String[] a = request.getParameterValues(name); 127 String[] a = request.getParameterValues(name);
151 return "request.headers-table"; 152 return "request.headers-table";
152 } 153 }
153 */ 154 */
154 }.newTable(); 155 }.newTable();
155 tbl.put( "headers", headers ); 156 tbl.put( "headers", headers );
156 tbl.put( "method", new LuanProperty() { public Object get() { 157 meta.getters().put( "method", new LuanJavaFunction(
157 return request.getMethod(); 158 HttpServletRequest.class.getMethod( "getMethod" ), request
158 } } ); 159 ) );
159 /* 160 meta.getters().put( "path", new LuanJavaFunction(
160 tbl.put( "servlet_path", new LuanProperty() { public Object get() { 161 HttpServletRequest.class.getMethod( "getRequestURI" ), request
161 return request.getServletPath(); 162 ) );
162 } } ); 163 meta.getters().put( "server_name", new LuanJavaFunction(
163 */ 164 HttpServletRequest.class.getMethod( "getServerName" ), request
164 tbl.put( "path", new LuanProperty() { public Object get() { 165 ) );
165 return request.getRequestURI(); 166 meta.getters().put( "url", new LuanJavaFunction(
166 } } ); 167 HttpServicer.class.getMethod( "getURL" ), this
167 tbl.put( "server_name", new LuanProperty() { public Object get() { 168 ) );
168 return request.getServerName(); 169 meta.getters().put( "query_string", new LuanJavaFunction(
169 } } ); 170 HttpServicer.class.getMethod( "getQueryString" ), this
170 tbl.put( "url", new LuanProperty() { public Object get() { 171 ) );
171 return getURL(request); 172 meta.getters().put( "remote_address", new LuanJavaFunction(
172 } } ); 173 HttpServletRequest.class.getMethod( "getRemoteAddr" ), request
173 tbl.put( "query_string", new LuanProperty() { public Object get() { 174 ) );
174 return getQueryString(request);
175 } } );
176 tbl.put( "remote_address", new LuanProperty() { public Object get() {
177 return request.getRemoteAddr();
178 } } );
179 LuanTable cookies = new LuanMeta() { 175 LuanTable cookies = new LuanMeta() {
180 176
181 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) { 177 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) {
182 if( !(key instanceof String) ) 178 if( !(key instanceof String) )
183 return null; 179 return null;
225 Object value; 221 Object value;
226 String filename = part.getContentDispositionFilename(); 222 String filename = part.getContentDispositionFilename();
227 if( filename == null ) { 223 if( filename == null ) {
228 value = new String(part.getBytes()); 224 value = new String(part.getBytes());
229 } else { 225 } else {
230 LuanTable partTbl = Luan.newPropertyTable(); 226 LuanPropertyMeta partMeta = LuanPropertyMeta.newInstance();
227 LuanTable partTbl = partMeta.newTable();
231 partTbl.put("filename",filename); 228 partTbl.put("filename",filename);
232 partTbl.put("content_type",part.getContentType()); 229 partTbl.put("content_type",part.getContentType());
233 partTbl.put( "content", new LuanProperty() { public Object get() { 230 partMeta.getters().put( "content", new LuanFunction() {
234 try { 231 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
235 InputStream in = part.getInputStream(); 232 try {
236 byte[] content = Utils.readAll(in); 233 InputStream in = part.getInputStream();
237 in.close(); 234 byte[] content = Utils.readAll(in);
238 return content; 235 in.close();
239 } catch(IOException e) { 236 return content;
240 throw new RuntimeException(e); 237 } catch(IOException e) {
238 throw new RuntimeException(e);
239 }
241 } 240 }
242 } } ); 241 } );
243 value = partTbl; 242 value = partTbl;
244 } 243 }
245 Object old = parameters.get(name); 244 Object old = parameters.get(name);
246 if( old == null ) { 245 if( old == null ) {
247 parameters.put(name,value); 246 parameters.put(name,value);
265 264
266 return tbl; 265 return tbl;
267 } 266 }
268 267
269 private LuanTable responseTable() throws NoSuchMethodException { 268 private LuanTable responseTable() throws NoSuchMethodException {
270 LuanTable tbl = Luan.newPropertyTable(); 269 LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
270 LuanTable tbl = meta.newTable();
271 tbl.put("java",response); 271 tbl.put("java",response);
272 add( tbl, "send_redirect", String.class ); 272 add( tbl, "send_redirect", String.class );
273 add( tbl, "send_error", Integer.TYPE, String.class ); 273 add( tbl, "send_error", Integer.TYPE, String.class );
274 LuanTable headers = new NameMeta() { 274 LuanTable headers = new NameMeta() {
275 275
305 return "response.headers-table"; 305 return "response.headers-table";
306 } 306 }
307 */ 307 */
308 }.newTable(); 308 }.newTable();
309 tbl.put( "headers", headers ); 309 tbl.put( "headers", headers );
310 tbl.put( "content_type", new LuanProperty() { 310 meta.getters().put( "content_type", new LuanJavaFunction(
311 @Override public Object get() { 311 HttpServletResponse.class.getMethod( "getContentType" ), response
312 return response.getContentType(); 312 ) );
313 } 313 meta.setters().put( "content_type", new LuanJavaFunction(
314 @Override public boolean set(Object value) { 314 HttpServletResponse.class.getMethod( "setContentType", String.class ), response
315 response.setContentType(string(value)); return true; 315 ) );
316 } 316 meta.getters().put( "character_encoding", new LuanJavaFunction(
317 } ); 317 HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response
318 tbl.put( "character_encoding", new LuanProperty() { 318 ) );
319 @Override public Object get() { 319 meta.setters().put( "character_encoding", new LuanJavaFunction(
320 return response.getCharacterEncoding(); 320 HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response
321 } 321 ) );
322 @Override public boolean set(Object value) {
323 response.setCharacterEncoding(string(value)); return true;
324 }
325 } );
326 add( tbl, "text_writer" ); 322 add( tbl, "text_writer" );
327 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); 323 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
328 add( tbl, "remove_cookie", String.class, String.class ); 324 add( tbl, "remove_cookie", String.class, String.class );
329 tbl.put( "status", new LuanProperty() { 325 meta.getters().put( "status", new LuanJavaFunction(
330 @Override public Object get() { 326 HttpServletResponse.class.getMethod( "getStatus" ), response
331 return response.getStatus(); 327 ) );
332 } 328 meta.setters().put( "status", new LuanJavaFunction(
333 @Override public boolean set(Object value) { 329 HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response
334 Integer i = Luan.asInteger(value); 330 ) );
335 if( i==null )
336 throw new IllegalArgumentException("value must be an integer");
337 response.setStatus(i);
338 return true;
339 }
340 } );
341 return tbl; 331 return tbl;
342 } 332 }
343 333
344 private LuanTable sessionTable() throws NoSuchMethodException { 334 private LuanTable sessionTable() throws NoSuchMethodException {
345 LuanTable tbl = Luan.newTable(); 335 LuanTable tbl = Luan.newTable();
411 removeCookie(request,response,name,domain); 401 removeCookie(request,response,name,domain);
412 } 402 }
413 403
414 404
415 // static utils 405 // static utils
406
407 public String getQueryString() {
408 return getQueryString(request);
409 }
416 410
417 public static String getQueryString(HttpServletRequest request) { 411 public static String getQueryString(HttpServletRequest request) {
418 return getQueryString(request,0); 412 return getQueryString(request,0);
419 } 413 }
420 414
443 } while( en.hasMoreElements() ); 437 } while( en.hasMoreElements() );
444 queryBuf.deleteCharAt(queryBuf.length() - 1); 438 queryBuf.deleteCharAt(queryBuf.length() - 1);
445 return queryBuf.toString(); 439 return queryBuf.toString();
446 } 440 }
447 441
442 public String getURL() {
443 return getURL(request);
444 }
445
448 public static String getURL(HttpServletRequest request) { 446 public static String getURL(HttpServletRequest request) {
449 return getURL(request,0); 447 return getURL(request,0);
450 } 448 }
451 449
452 public static String getURL(HttpServletRequest request,int maxValueLen) { 450 public static String getURL(HttpServletRequest request,int maxValueLen) {