comparison web/src/luan/modules/web/HttpServicer.java @ 357:496d61b1fb5a

implement Http.request.parts
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2015 11:21:10 -0600
parents 78a6a71afbfd
children 6fc33c20441b
comparison
equal deleted inserted replaced
356:5e34702423a0 357:496d61b1fb5a
1 package luan.modules.web; 1 package luan.modules.web;
2 2
3 import java.io.InputStream;
4 import java.io.BufferedInputStream;
3 import java.io.PrintWriter; 5 import java.io.PrintWriter;
4 import java.io.IOException; 6 import java.io.IOException;
5 import java.util.Map; 7 import java.util.Map;
6 import java.util.AbstractMap; 8 import java.util.AbstractMap;
7 import java.util.Set; 9 import java.util.Set;
10 import java.util.List;
11 import java.util.ArrayList;
8 import java.util.Arrays; 12 import java.util.Arrays;
9 import java.util.Iterator; 13 import java.util.Iterator;
10 import java.util.Enumeration; 14 import java.util.Enumeration;
11 import javax.servlet.ServletOutputStream; 15 import javax.servlet.ServletOutputStream;
16 import javax.servlet.ServletException;
12 import javax.servlet.http.Cookie; 17 import javax.servlet.http.Cookie;
13 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse; 19 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession; 20 import javax.servlet.http.HttpSession;
21 import javax.servlet.http.Part;
22 import org.eclipse.jetty.util.MultiPartInputStream;
16 import luan.Luan; 23 import luan.Luan;
17 import luan.LuanState; 24 import luan.LuanState;
18 import luan.LuanFunction; 25 import luan.LuanFunction;
19 import luan.LuanElement; 26 import luan.LuanElement;
20 import luan.LuanException; 27 import luan.LuanException;
25 import luan.LuanProperty; 32 import luan.LuanProperty;
26 import luan.DeepCloner; 33 import luan.DeepCloner;
27 import luan.modules.PackageLuan; 34 import luan.modules.PackageLuan;
28 import luan.modules.IoLuan; 35 import luan.modules.IoLuan;
29 import luan.modules.TableLuan; 36 import luan.modules.TableLuan;
37 import luan.modules.Utils;
30 38
31 39
32 public final class HttpServicer { 40 public final class HttpServicer {
33 41
34 public static boolean service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) 42 public static boolean service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName)
101 109
102 private final HttpServletRequest request; 110 private final HttpServletRequest request;
103 private final HttpServletResponse response; 111 private final HttpServletResponse response;
104 // private PrintWriter writer = null; 112 // private PrintWriter writer = null;
105 // private ServletOutputStream sos = null; 113 // private ServletOutputStream sos = null;
114 private MultiPartInputStream mpis = null;
115 private LuanTable parts = null;
106 116
107 private HttpServicer(HttpServletRequest request,HttpServletResponse response) { 117 private HttpServicer(HttpServletRequest request,HttpServletResponse response) {
108 this.request = request; 118 this.request = request;
109 this.response = response; 119 this.response = response;
110 } 120 }
198 @Override protected String type() { 208 @Override protected String type() {
199 return "request.cookies-table"; 209 return "request.cookies-table";
200 } 210 }
201 }; 211 };
202 tbl.put( "cookies", cookies ); 212 tbl.put( "cookies", cookies );
213
214 tbl.put( "parts", new LuanProperty() { public Object get() {
215 if( parts == null ) {
216 String contentType = request.getContentType();
217 if( contentType!=null && contentType.startsWith("multipart/form-data") ) {
218 try {
219 InputStream in = new BufferedInputStream(request.getInputStream());
220 final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null);
221 parts = new NameTable() {
222
223 @Override Object get(String name) {
224 try {
225 MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)mpis.getPart(name);
226 if( part==null )
227 return null;
228 LuanTable tbl = Luan.newTable();
229 tbl.put("filename",part.getContentDispositionFilename());
230 tbl.put("content_type",part.getContentType());
231 InputStream in = part.getInputStream();
232 byte[] content = Utils.readAll(in);
233 in.close();
234 tbl.put("content",content);
235 return tbl;
236 } catch(IOException e) {
237 throw new RuntimeException(e);
238 } catch(ServletException e) {
239 throw new RuntimeException(e);
240 }
241 }
242
243 @Override Iterator<String> names() {
244 try {
245 List<String> names = new ArrayList<String>();
246 for( Part part : mpis.getParts() ) {
247 names.add(part.getName());
248 }
249 return names.iterator();
250 } catch(IOException e) {
251 throw new RuntimeException(e);
252 } catch(ServletException e) {
253 throw new RuntimeException(e);
254 }
255 }
256
257 @Override protected String type() {
258 return "request.parts-table";
259 }
260 };
261 } catch(IOException e) {
262 throw new RuntimeException(e);
263 }
264 }
265 }
266 return parts;
267 } } );
268
203 return tbl; 269 return tbl;
204 } 270 }
205 271
206 private LuanTable responseTable() throws NoSuchMethodException { 272 private LuanTable responseTable() throws NoSuchMethodException {
207 LuanTable tbl = Luan.newPropertyTable(); 273 LuanTable tbl = Luan.newPropertyTable();