comparison web/src/luan/modules/web/HttpLuan.java @ 217:a608066395c5

clean up HttpLuan.java git-svn-id: https://luan-java.googlecode.com/svn/trunk@218 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 15 Jul 2014 06:16:40 +0000
parents 75750ceb45ee
children 92f5d74697f8
comparison
equal deleted inserted replaced
216:a1b142f9c5c0 217:a608066395c5
84 throw luan.exception( "module 'web/Http' not defined" ); 84 throw luan.exception( "module 'web/Http' not defined" );
85 HttpLuan lib = new HttpLuan(request,response); 85 HttpLuan lib = new HttpLuan(request,response);
86 try { 86 try {
87 module.put( "request", lib.requestTable() ); 87 module.put( "request", lib.requestTable() );
88 module.put( "response", lib.responseTable() ); 88 module.put( "response", lib.responseTable() );
89 module.put( "cookie", lib.cookieTable() );
90 module.put( "session_attribute", lib.sessionTable() );
89 /* 91 /*
90 module.put( "write", new LuanJavaFunction( 92 module.put( "write", new LuanJavaFunction(
91 HttpLuan.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib 93 HttpLuan.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib
92 ) ); 94 ) );
93 */ 95 */
110 this.request = request; 112 this.request = request;
111 this.response = response; 113 this.response = response;
112 } 114 }
113 115
114 private LuanTable requestTable() throws NoSuchMethodException { 116 private LuanTable requestTable() throws NoSuchMethodException {
115 LuanTable req = new LuanTable(); 117 LuanTable tbl = new LuanTable();
116 req.put("java",request); 118 tbl.put("java",request);
117 req.put( "get_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("getAttribute",String.class),request) ); 119 add( tbl, "get_parameter", String.class );
118 req.put( "set_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("setAttribute",String.class,Object.class),request) ); 120 tbl.put( "get_header", new LuanJavaFunction(
119 req.put( "get_parameter", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameter",String.class),request) ); 121 HttpServletRequest.class.getMethod("getHeader",String.class), request
120 req.put( "get_parameter_values", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameterValues",String.class),request) ); 122 ) );
121 req.put( "get_header", new LuanJavaFunction(HttpServletRequest.class.getMethod("getHeader",String.class),request) ); 123 tbl.put( "get_method", new LuanJavaFunction(
122 add( req, "get_cookie_value", String.class ); 124 HttpServletRequest.class.getMethod("getMethod"), request
123 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) ); 125 ) );
124 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) ); 126 tbl.put( "get_servlet_path", new LuanJavaFunction(
125 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) ); 127 HttpServletRequest.class.getMethod("getServletPath"), request
126 add( req, "current_url" ); 128 ) );
127 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) ); 129 tbl.put( "get_server_name", new LuanJavaFunction(
128 add( req, "get_session_attribute", String.class ); 130 HttpServletRequest.class.getMethod("getServerName"), request
129 add( req, "set_session_attribute", String.class, Object.class ); 131 ) );
130 return req; 132 add( tbl, "get_current_url" );
133 tbl.put( "get_remote_address", new LuanJavaFunction(
134 HttpServletRequest.class.getMethod("getRemoteAddr"), request
135 ) );
136 return tbl;
131 } 137 }
132 138
133 private LuanTable responseTable() throws NoSuchMethodException { 139 private LuanTable responseTable() throws NoSuchMethodException {
134 LuanTable resp = new LuanTable(); 140 LuanTable tbl = new LuanTable();
135 resp.put("java",response); 141 tbl.put("java",response);
136 add( resp, "send_redirect", String.class ); 142 add( tbl, "send_redirect", String.class );
137 add( resp, "send_error", Integer.TYPE, String.class ); 143 add( tbl, "send_error", Integer.TYPE, String.class );
138 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) ); 144 tbl.put( "contains_header", new LuanJavaFunction(
139 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); 145 HttpServletResponse.class.getMethod("containsHeader",String.class), response
140 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); 146 ) );
141 add( resp, "remove_cookie", String.class, String.class ); 147 tbl.put( "set_header", new LuanJavaFunction(
142 resp.put( "set_content_type", new LuanJavaFunction(HttpServletResponse.class.getMethod("setContentType",String.class),response) ); 148 HttpServletResponse.class.getMethod("setHeader",String.class,String.class), response
143 add( resp, "text_writer" ); 149 ) );
144 return resp; 150 tbl.put( "set_content_type", new LuanJavaFunction(
151 HttpServletResponse.class.getMethod("setContentType",String.class), response
152 ) );
153 tbl.put( "set_character_encoding", new LuanJavaFunction(
154 HttpServletResponse.class.getMethod("setCharacterEncoding",String.class), response
155 ) );
156 add( tbl, "text_writer" );
157 return tbl;
158 }
159
160 private LuanTable cookieTable() throws NoSuchMethodException {
161 LuanTable tbl = new LuanTable();
162 tbl.put( "get", new LuanJavaFunction(
163 HttpLuan.class.getMethod("get_cookie",String.class), this
164 ) );
165 tbl.put( "set", new LuanJavaFunction(
166 HttpLuan.class.getMethod("set_cookie", String.class, String.class, Boolean.TYPE, String.class), this
167 ) );
168 tbl.put( "remove", new LuanJavaFunction(
169 HttpLuan.class.getMethod("remove_cookie", String.class, String.class), this
170 ) );
171 return tbl;
172 }
173
174 private LuanTable sessionTable() throws NoSuchMethodException {
175 LuanTable tbl = new LuanTable();
176 tbl.put( "get", new LuanJavaFunction(
177 HttpLuan.class.getMethod("get_session_attribute",String.class), this
178 ) );
179 tbl.put( "set", new LuanJavaFunction(
180 HttpLuan.class.getMethod("set_session_attribute",String.class, Object.class), this
181 ) );
182 return tbl;
145 } 183 }
146 184
147 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 185 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
148 t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),this) ); 186 t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),this) );
149 } 187 }
158 */ 196 */
159 public LuanTable text_writer() throws IOException { 197 public LuanTable text_writer() throws IOException {
160 return IoLuan.textWriter(response.getWriter()); 198 return IoLuan.textWriter(response.getWriter());
161 } 199 }
162 200
163 public String get_cookie_value(String name) { 201 public Object get_parameter(String name) {
202 String[] a = request.getParameterValues(name);
203 return a==null ? null : a.length==1 ? a[0] : a;
204 }
205
206 public String get_cookie(String name) {
164 return getCookieValue(request, name); 207 return getCookieValue(request, name);
165 } 208 }
166 209
167 public String current_url() { 210 public String get_current_url() {
168 return getCurrentURL(request); 211 return getCurrentURL(request);
169 } 212 }
170 213
171 public void send_redirect(String redirectUrl) 214 public void send_redirect(String redirectUrl)
172 throws IOException 215 throws IOException