Mercurial Hosting > luan
comparison web/src/luan/modules/web/HttpServicer.java @ 427:dae264ad6a7b
fix LuanTable.put() to use metatables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2015 19:29:07 -0600 |
parents | 23a93c118042 |
children | df95199ca4c0 |
comparison
equal
deleted
inserted
replaced
426:23a93c118042 | 427:dae264ad6a7b |
---|---|
72 } | 72 } |
73 | 73 |
74 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:web/Http"); | 74 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:web/Http"); |
75 HttpServicer lib = new HttpServicer(request,response); | 75 HttpServicer lib = new HttpServicer(request,response); |
76 try { | 76 try { |
77 module.put( "request", lib.requestTable() ); | 77 module.put( luan, "request", lib.requestTable() ); |
78 module.put( "response", lib.responseTable() ); | 78 module.put( luan, "response", lib.responseTable() ); |
79 module.put( "session", lib.sessionTable() ); | 79 module.put( luan, "session", lib.sessionTable() ); |
80 /* | 80 /* |
81 module.put( "write", new LuanJavaFunction( | 81 module.put( "write", new LuanJavaFunction( |
82 HttpServicer.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib | 82 HttpServicer.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib |
83 ) ); | 83 ) ); |
84 */ | 84 */ |
117 } | 117 } |
118 | 118 |
119 private LuanTable requestTable() throws NoSuchMethodException { | 119 private LuanTable requestTable() throws NoSuchMethodException { |
120 LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); | 120 LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); |
121 LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); | 121 LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); |
122 tbl.put("java",request); | 122 tbl.rawPut("java",request); |
123 LuanTable parameters = new NameMeta() { | 123 LuanTable parameters = new NameMeta() { |
124 | 124 |
125 @Override Object get(String name) { | 125 @Override Object get(String name) { |
126 return request.getParameter(name); | 126 return request.getParameter(name); |
127 } | 127 } |
133 @Override protected String type(LuanTable tbl) { | 133 @Override protected String type(LuanTable tbl) { |
134 return "request.parameters"; | 134 return "request.parameters"; |
135 } | 135 } |
136 | 136 |
137 }.newTable(); | 137 }.newTable(); |
138 tbl.put( "parameters", parameters ); | 138 tbl.rawPut( "parameters", parameters ); |
139 add( tbl, "get_parameter_values", String.class ); | 139 add( tbl, "get_parameter_values", String.class ); |
140 LuanTable headers = new NameMeta() { | 140 LuanTable headers = new NameMeta() { |
141 | 141 |
142 @Override Object get(String name) { | 142 @Override Object get(String name) { |
143 return request.getHeader(name); | 143 return request.getHeader(name); |
150 @Override protected String type(LuanTable tbl) { | 150 @Override protected String type(LuanTable tbl) { |
151 return "request.headers"; | 151 return "request.headers"; |
152 } | 152 } |
153 | 153 |
154 }.newTable(); | 154 }.newTable(); |
155 tbl.put( "headers", headers ); | 155 tbl.rawPut( "headers", headers ); |
156 getters.put( "method", new LuanJavaFunction( | 156 getters.rawPut( "method", new LuanJavaFunction( |
157 HttpServletRequest.class.getMethod( "getMethod" ), request | 157 HttpServletRequest.class.getMethod( "getMethod" ), request |
158 ) ); | 158 ) ); |
159 getters.put( "path", new LuanJavaFunction( | 159 getters.rawPut( "path", new LuanJavaFunction( |
160 HttpServletRequest.class.getMethod( "getRequestURI" ), request | 160 HttpServletRequest.class.getMethod( "getRequestURI" ), request |
161 ) ); | 161 ) ); |
162 getters.put( "server_name", new LuanJavaFunction( | 162 getters.rawPut( "server_name", new LuanJavaFunction( |
163 HttpServletRequest.class.getMethod( "getServerName" ), request | 163 HttpServletRequest.class.getMethod( "getServerName" ), request |
164 ) ); | 164 ) ); |
165 getters.put( "url", new LuanJavaFunction( | 165 getters.rawPut( "url", new LuanJavaFunction( |
166 HttpServicer.class.getMethod( "getURL" ), this | 166 HttpServicer.class.getMethod( "getURL" ), this |
167 ) ); | 167 ) ); |
168 getters.put( "query_string", new LuanJavaFunction( | 168 getters.rawPut( "query_string", new LuanJavaFunction( |
169 HttpServicer.class.getMethod( "getQueryString" ), this | 169 HttpServicer.class.getMethod( "getQueryString" ), this |
170 ) ); | 170 ) ); |
171 getters.put( "remote_address", new LuanJavaFunction( | 171 getters.rawPut( "remote_address", new LuanJavaFunction( |
172 HttpServletRequest.class.getMethod( "getRemoteAddr" ), request | 172 HttpServletRequest.class.getMethod( "getRemoteAddr" ), request |
173 ) ); | 173 ) ); |
174 LuanTable cookies = new LuanMeta() { | 174 LuanTable cookies = new LuanMeta() { |
175 | 175 |
176 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) { | 176 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) { |
200 @Override protected String type(LuanTable tbl) { | 200 @Override protected String type(LuanTable tbl) { |
201 return "request.cookies"; | 201 return "request.cookies"; |
202 } | 202 } |
203 | 203 |
204 }.newTable(); | 204 }.newTable(); |
205 tbl.put( "cookies", cookies ); | 205 tbl.rawPut( "cookies", cookies ); |
206 | 206 |
207 String contentType = request.getContentType(); | 207 String contentType = request.getContentType(); |
208 if( contentType!=null && contentType.startsWith("multipart/form-data") ) { | 208 if( contentType!=null && contentType.startsWith("multipart/form-data") ) { |
209 try { | 209 try { |
210 InputStream in = new BufferedInputStream(request.getInputStream()); | 210 InputStream in = new BufferedInputStream(request.getInputStream()); |
219 String filename = part.getContentDispositionFilename(); | 219 String filename = part.getContentDispositionFilename(); |
220 if( filename == null ) { | 220 if( filename == null ) { |
221 value = new String(part.getBytes()); | 221 value = new String(part.getBytes()); |
222 } else { | 222 } else { |
223 LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable(); | 223 LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable(); |
224 partTbl.put("filename",filename); | 224 partTbl.rawPut("filename",filename); |
225 partTbl.put("content_type",part.getContentType()); | 225 partTbl.rawPut("content_type",part.getContentType()); |
226 LuanPropertyMeta.INSTANCE.getters(partTbl).put( "content", new LuanFunction() { | 226 LuanPropertyMeta.INSTANCE.getters(partTbl).rawPut( "content", new LuanFunction() { |
227 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 227 @Override public Object call(LuanState luan,Object[] args) throws LuanException { |
228 try { | 228 try { |
229 InputStream in = part.getInputStream(); | 229 InputStream in = part.getInputStream(); |
230 byte[] content = Utils.readAll(in); | 230 byte[] content = Utils.readAll(in); |
231 in.close(); | 231 in.close(); |
235 } | 235 } |
236 } | 236 } |
237 } ); | 237 } ); |
238 value = partTbl; | 238 value = partTbl; |
239 } | 239 } |
240 parameters.put(name,value); | 240 parameters.rawPut(name,value); |
241 Object old = map.get(name); | 241 Object old = map.get(name); |
242 if( old == null ) { | 242 if( old == null ) { |
243 map.put(name,value); | 243 map.put(name,value); |
244 } else if( old instanceof Object[] ) { | 244 } else if( old instanceof Object[] ) { |
245 Object[] aOld = (Object[])old; | 245 Object[] aOld = (Object[])old; |
249 map.put(name,aNew); | 249 map.put(name,aNew); |
250 } else { | 250 } else { |
251 map.put(name,new Object[]{old,value}); | 251 map.put(name,new Object[]{old,value}); |
252 } | 252 } |
253 } | 253 } |
254 tbl.put( "parameters", parameters ); | 254 tbl.rawPut( "parameters", parameters ); |
255 tbl.put( "get_parameter_values", new LuanFunction() { | 255 tbl.rawPut( "get_parameter_values", new LuanFunction() { |
256 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 256 @Override public Object call(LuanState luan,Object[] args) throws LuanException { |
257 return args.length==0 ? null : map.get(args[0]); | 257 return args.length==0 ? null : map.get(args[0]); |
258 } | 258 } |
259 } ); | 259 } ); |
260 } catch(IOException e) { | 260 } catch(IOException e) { |
269 | 269 |
270 private LuanTable responseTable() throws NoSuchMethodException { | 270 private LuanTable responseTable() throws NoSuchMethodException { |
271 LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); | 271 LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); |
272 LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); | 272 LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); |
273 LuanTable setters = LuanPropertyMeta.INSTANCE.setters(tbl); | 273 LuanTable setters = LuanPropertyMeta.INSTANCE.setters(tbl); |
274 tbl.put("java",response); | 274 tbl.rawPut("java",response); |
275 add( tbl, "send_redirect", String.class ); | 275 add( tbl, "send_redirect", String.class ); |
276 add( tbl, "send_error", Integer.TYPE, String.class ); | 276 add( tbl, "send_error", Integer.TYPE, String.class ); |
277 LuanTable headers = new NameMeta() { | 277 LuanTable headers = new NameMeta() { |
278 | 278 |
279 @Override Object get(String name) { | 279 @Override Object get(String name) { |
307 @Override protected String type(LuanTable tbl) { | 307 @Override protected String type(LuanTable tbl) { |
308 return "response.headers"; | 308 return "response.headers"; |
309 } | 309 } |
310 | 310 |
311 }.newTable(); | 311 }.newTable(); |
312 tbl.put( "headers", headers ); | 312 tbl.rawPut( "headers", headers ); |
313 getters.put( "content_type", new LuanJavaFunction( | 313 getters.rawPut( "content_type", new LuanJavaFunction( |
314 HttpServletResponse.class.getMethod( "getContentType" ), response | 314 HttpServletResponse.class.getMethod( "getContentType" ), response |
315 ) ); | 315 ) ); |
316 setters.put( "content_type", new LuanJavaFunction( | 316 setters.rawPut( "content_type", new LuanJavaFunction( |
317 HttpServletResponse.class.getMethod( "setContentType", String.class ), response | 317 HttpServletResponse.class.getMethod( "setContentType", String.class ), response |
318 ) ); | 318 ) ); |
319 getters.put( "character_encoding", new LuanJavaFunction( | 319 getters.rawPut( "character_encoding", new LuanJavaFunction( |
320 HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response | 320 HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response |
321 ) ); | 321 ) ); |
322 setters.put( "character_encoding", new LuanJavaFunction( | 322 setters.rawPut( "character_encoding", new LuanJavaFunction( |
323 HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response | 323 HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response |
324 ) ); | 324 ) ); |
325 add( tbl, "text_writer" ); | 325 add( tbl, "text_writer" ); |
326 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); | 326 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); |
327 add( tbl, "remove_cookie", String.class, String.class ); | 327 add( tbl, "remove_cookie", String.class, String.class ); |
328 getters.put( "status", new LuanJavaFunction( | 328 getters.rawPut( "status", new LuanJavaFunction( |
329 HttpServletResponse.class.getMethod( "getStatus" ), response | 329 HttpServletResponse.class.getMethod( "getStatus" ), response |
330 ) ); | 330 ) ); |
331 setters.put( "status", new LuanJavaFunction( | 331 setters.rawPut( "status", new LuanJavaFunction( |
332 HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response | 332 HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response |
333 ) ); | 333 ) ); |
334 return tbl; | 334 return tbl; |
335 } | 335 } |
336 | 336 |
360 @Override protected String type(LuanTable tbl) { | 360 @Override protected String type(LuanTable tbl) { |
361 return "session.attributes"; | 361 return "session.attributes"; |
362 } | 362 } |
363 | 363 |
364 }.newTable(); | 364 }.newTable(); |
365 tbl.put( "attributes", attributes ); | 365 tbl.rawPut( "attributes", attributes ); |
366 return tbl; | 366 return tbl; |
367 } | 367 } |
368 | 368 |
369 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 369 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
370 t.put( method, new LuanJavaFunction(HttpServicer.class.getMethod(method,parameterTypes),this) ); | 370 t.rawPut( method, new LuanJavaFunction(HttpServicer.class.getMethod(method,parameterTypes),this) ); |
371 } | 371 } |
372 /* | 372 /* |
373 public void text_write(LuanState luan,Object... args) throws LuanException, IOException { | 373 public void text_write(LuanState luan,Object... args) throws LuanException, IOException { |
374 if( writer == null ) | 374 if( writer == null ) |
375 writer = response.getWriter(); | 375 writer = response.getWriter(); |