Mercurial Hosting > luan
comparison src/luan/modules/HttpLuan.java @ 168:ebe9db183eb7
rename *Lib.java to *Luan.java
git-svn-id: https://luan-java.googlecode.com/svn/trunk@169 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 04:42:07 +0000 |
parents | src/luan/modules/HttpLib.java@4c0131c2b650 |
children |
comparison
equal
deleted
inserted
replaced
167:4c0131c2b650 | 168:ebe9db183eb7 |
---|---|
1 package luan.modules; | |
2 | |
3 import java.io.PrintWriter; | |
4 import java.io.IOException; | |
5 import java.util.Map; | |
6 import java.util.Set; | |
7 import java.util.Arrays; | |
8 import java.util.Enumeration; | |
9 import javax.servlet.ServletOutputStream; | |
10 import javax.servlet.http.Cookie; | |
11 import javax.servlet.http.HttpServletRequest; | |
12 import javax.servlet.http.HttpServletResponse; | |
13 import javax.servlet.http.HttpSession; | |
14 import luan.Luan; | |
15 import luan.LuanState; | |
16 import luan.LuanFunction; | |
17 import luan.LuanElement; | |
18 import luan.LuanException; | |
19 import luan.LuanTable; | |
20 import luan.LuanJavaFunction; | |
21 import luan.LuanExitException; | |
22 import luan.DeepCloner; | |
23 | |
24 | |
25 public final class HttpLuan { | |
26 | |
27 public static final LuanFunction LOADER = new LuanFunction() { | |
28 @Override public Object call(LuanState luan,Object[] args) { | |
29 return new LuanTable(); // starts empty | |
30 } | |
31 }; | |
32 | |
33 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | |
34 throws LuanException | |
35 { | |
36 LuanFunction fn; | |
37 synchronized(luan) { | |
38 Object mod = PackageLuan.require(luan,modName); | |
39 if( !(mod instanceof LuanTable) ) | |
40 throw luan.exception( "module '"+modName+"' must return a table" ); | |
41 LuanTable tbl = (LuanTable)mod; | |
42 if( Luan.toBoolean( tbl.get("per_session") ) ) { | |
43 HttpSession session = request.getSession(); | |
44 LuanState sessionLuan = (LuanState)session.getValue("luan"); | |
45 if( sessionLuan!=null ) { | |
46 luan = sessionLuan; | |
47 } else { | |
48 DeepCloner cloner = new DeepCloner(); | |
49 luan = cloner.deepClone(luan); | |
50 session.putValue("luan",luan); | |
51 } | |
52 tbl = (LuanTable)PackageLuan.require(luan,modName); | |
53 fn = (LuanFunction)tbl.get("page"); | |
54 } else { | |
55 fn = (LuanFunction)tbl.get("page"); | |
56 if( fn == null ) | |
57 throw luan.exception( "function 'page' is not defined" ); | |
58 DeepCloner cloner = new DeepCloner(); | |
59 luan = cloner.deepClone(luan); | |
60 fn = cloner.get(fn); | |
61 } | |
62 } | |
63 | |
64 LuanTable module = (LuanTable)luan.loaded().get("Http"); | |
65 if( module == null ) | |
66 throw luan.exception( "module 'Http' not defined" ); | |
67 HttpLuan lib = new HttpLuan(request,response); | |
68 try { | |
69 module.put( "request", lib.requestTable() ); | |
70 module.put( "response", lib.responseTable() ); | |
71 /* | |
72 module.put( "write", new LuanJavaFunction( | |
73 HttpLuan.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib | |
74 ) ); | |
75 */ | |
76 } catch(NoSuchMethodException e) { | |
77 throw new RuntimeException(e); | |
78 } | |
79 | |
80 luan.call(fn,"<http>"); | |
81 } | |
82 | |
83 | |
84 | |
85 private final HttpServletRequest request; | |
86 private final HttpServletResponse response; | |
87 // private PrintWriter writer = null; | |
88 // private ServletOutputStream sos = null; | |
89 | |
90 private HttpLuan(HttpServletRequest request,HttpServletResponse response) { | |
91 this.request = request; | |
92 this.response = response; | |
93 } | |
94 | |
95 private LuanTable requestTable() throws NoSuchMethodException { | |
96 LuanTable req = new LuanTable(); | |
97 req.put("java",request); | |
98 req.put( "get_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("getAttribute",String.class),request) ); | |
99 req.put( "set_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("setAttribute",String.class,Object.class),request) ); | |
100 req.put( "get_parameter", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameter",String.class),request) ); | |
101 req.put( "get_parameter_values", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameterValues",String.class),request) ); | |
102 req.put( "get_header", new LuanJavaFunction(HttpServletRequest.class.getMethod("getHeader",String.class),request) ); | |
103 add( req, "get_cookie_value", String.class ); | |
104 req.put( "method", new LuanJavaFunction(HttpServletRequest.class.getMethod("getMethod"),request) ); | |
105 req.put( "servlet_path", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServletPath"),request) ); | |
106 req.put( "server_name", new LuanJavaFunction(HttpServletRequest.class.getMethod("getServerName"),request) ); | |
107 add( req, "current_url" ); | |
108 req.put( "remote_address", new LuanJavaFunction(HttpServletRequest.class.getMethod("getRemoteAddr"),request) ); | |
109 add( req, "get_session_attribute", String.class ); | |
110 add( req, "set_session_attribute", String.class, Object.class ); | |
111 return req; | |
112 } | |
113 | |
114 private LuanTable responseTable() throws NoSuchMethodException { | |
115 LuanTable resp = new LuanTable(); | |
116 resp.put("java",response); | |
117 add( resp, "send_redirect", String.class ); | |
118 add( resp, "send_error", Integer.TYPE, String.class ); | |
119 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) ); | |
120 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); | |
121 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); | |
122 add( resp, "remove_cookie", String.class, String.class ); | |
123 resp.put( "set_content_type", new LuanJavaFunction(HttpServletResponse.class.getMethod("setContentType",String.class),response) ); | |
124 add( resp, "text_writer" ); | |
125 return resp; | |
126 } | |
127 | |
128 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | |
129 t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),this) ); | |
130 } | |
131 /* | |
132 public void text_write(LuanState luan,Object... args) throws LuanException, IOException { | |
133 if( writer == null ) | |
134 writer = response.getWriter(); | |
135 for( Object obj : args ) { | |
136 writer.print( luan.toString(obj) ); | |
137 } | |
138 } | |
139 */ | |
140 public LuanTable text_writer() throws IOException { | |
141 return IoLuan.textWriter(response.getWriter()); | |
142 } | |
143 | |
144 public String get_cookie_value(String name) { | |
145 return getCookieValue(request, name); | |
146 } | |
147 | |
148 public String current_url() { | |
149 return getCurrentURL(request); | |
150 } | |
151 | |
152 public void send_redirect(String redirectUrl) | |
153 throws IOException | |
154 { | |
155 response.sendRedirect(redirectUrl); | |
156 throw new LuanExitException(); | |
157 } | |
158 | |
159 public void send_error(int code,String text) | |
160 throws IOException | |
161 { | |
162 response.sendError(code, text); | |
163 throw new LuanExitException(); | |
164 } | |
165 | |
166 public void set_cookie(String name,String value,boolean isPersistent, String domain) { | |
167 setCookie(request,response,name,value,isPersistent,domain); | |
168 } | |
169 | |
170 public void remove_cookie(String name, String domain) { | |
171 removeCookie(request,response,name,domain); | |
172 } | |
173 | |
174 public Object get_session_attribute(String name) { | |
175 return request.getSession().getAttribute(name); | |
176 } | |
177 | |
178 public void set_session_attribute(String name,Object value) { | |
179 request.getSession().setAttribute(name,value); | |
180 } | |
181 | |
182 | |
183 // static utils | |
184 | |
185 public static String getQueryString(HttpServletRequest request) { | |
186 return getQueryString(request,0); | |
187 } | |
188 | |
189 public static String getQueryString(HttpServletRequest request,int maxValueLen) { | |
190 String method = request.getMethod(); | |
191 if( method.equals("GET") ) | |
192 return request.getQueryString(); | |
193 if( !method.equals("POST") && !method.equals("HEAD") ) | |
194 throw new RuntimeException(method); | |
195 Enumeration en = request.getParameterNames(); | |
196 StringBuilder queryBuf = new StringBuilder(); | |
197 if( !en.hasMoreElements() ) | |
198 return null; | |
199 do { | |
200 String param = (String)en.nextElement(); | |
201 String value = request.getParameter(param); | |
202 if( maxValueLen > 0 ) { | |
203 int len = value.length(); | |
204 if( len > maxValueLen ) | |
205 value = value.substring(0,maxValueLen) + "..." + (len-maxValueLen); | |
206 } | |
207 queryBuf.append(param); | |
208 queryBuf.append('='); | |
209 queryBuf.append(value); | |
210 queryBuf.append('&'); | |
211 } while( en.hasMoreElements() ); | |
212 queryBuf.deleteCharAt(queryBuf.length() - 1); | |
213 return queryBuf.toString(); | |
214 } | |
215 | |
216 public static String getCurrentURL(HttpServletRequest request) { | |
217 return getCurrentURL(request,0); | |
218 } | |
219 | |
220 public static String getCurrentURL(HttpServletRequest request,int maxValueLen) { | |
221 // StringBuffer buf = HttpUtils.getRequestURL(request); | |
222 StringBuffer buf = request.getRequestURL(); | |
223 String qStr = getQueryString(request,maxValueLen); | |
224 if(qStr != null && qStr.length() > 0) { | |
225 buf.append('?'); | |
226 buf.append(qStr); | |
227 } | |
228 return buf.toString(); | |
229 } | |
230 | |
231 private static String escape(String value) { | |
232 return value.replaceAll(";", "%3B"); | |
233 } | |
234 | |
235 private static String unescape(String value) { | |
236 return value.replaceAll("%3B", ";"); | |
237 } | |
238 | |
239 private static Cookie getCookie(HttpServletRequest request,String name) { | |
240 Cookie[] cookies = request.getCookies(); | |
241 if( cookies == null ) | |
242 return null; | |
243 for (Cookie cookie : cookies) { | |
244 if (cookie.getName().equals(name)) | |
245 return cookie; | |
246 } | |
247 return null; | |
248 } | |
249 | |
250 public static String getCookieValue(HttpServletRequest request,String name) { | |
251 Cookie cookie = getCookie(request,name); | |
252 return cookie==null ? null : unescape(cookie.getValue()); | |
253 } | |
254 | |
255 public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,boolean isPersistent, String domain) { | |
256 Cookie cookie = getCookie(request,name); | |
257 if( cookie==null || !cookie.getValue().equals(value) ) { | |
258 cookie = new Cookie(name, escape(value)); | |
259 cookie.setPath("/"); | |
260 if (domain != null && domain.length() > 0) | |
261 cookie.setDomain(domain); | |
262 if( isPersistent ) | |
263 cookie.setMaxAge(10000000); | |
264 response.addCookie(cookie); | |
265 } | |
266 } | |
267 | |
268 public static void removeCookie(HttpServletRequest request, | |
269 HttpServletResponse response, | |
270 String name, | |
271 String domain | |
272 | |
273 ) { | |
274 Cookie cookie = getCookie(request, name); | |
275 if(cookie != null) { | |
276 Cookie delCookie = new Cookie(name, "delete"); | |
277 delCookie.setPath("/"); | |
278 delCookie.setMaxAge(0); | |
279 if (domain != null && domain.length() > 0) | |
280 delCookie.setDomain(domain); | |
281 response.addCookie(delCookie); | |
282 } | |
283 } | |
284 | |
285 } |