Mercurial Hosting > luan
changeset 357:496d61b1fb5a
implement Http.request.parts
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 14 Apr 2015 11:21:10 -0600 |
parents | 5e34702423a0 |
children | 6fc33c20441b |
files | web/src/luan/modules/web/HttpServicer.java |
diffstat | 1 files changed, 66 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/web/src/luan/modules/web/HttpServicer.java Tue Apr 14 09:44:07 2015 -0600 +++ b/web/src/luan/modules/web/HttpServicer.java Tue Apr 14 11:21:10 2015 -0600 @@ -1,18 +1,25 @@ package luan.modules.web; +import java.io.InputStream; +import java.io.BufferedInputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.Map; import java.util.AbstractMap; import java.util.Set; +import java.util.List; +import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.Enumeration; import javax.servlet.ServletOutputStream; +import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import javax.servlet.http.Part; +import org.eclipse.jetty.util.MultiPartInputStream; import luan.Luan; import luan.LuanState; import luan.LuanFunction; @@ -27,6 +34,7 @@ import luan.modules.PackageLuan; import luan.modules.IoLuan; import luan.modules.TableLuan; +import luan.modules.Utils; public final class HttpServicer { @@ -103,6 +111,8 @@ private final HttpServletResponse response; // private PrintWriter writer = null; // private ServletOutputStream sos = null; + private MultiPartInputStream mpis = null; + private LuanTable parts = null; private HttpServicer(HttpServletRequest request,HttpServletResponse response) { this.request = request; @@ -200,6 +210,62 @@ } }; tbl.put( "cookies", cookies ); + + tbl.put( "parts", new LuanProperty() { public Object get() { + if( parts == null ) { + String contentType = request.getContentType(); + if( contentType!=null && contentType.startsWith("multipart/form-data") ) { + try { + InputStream in = new BufferedInputStream(request.getInputStream()); + final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null); + parts = new NameTable() { + + @Override Object get(String name) { + try { + MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)mpis.getPart(name); + if( part==null ) + return null; + LuanTable tbl = Luan.newTable(); + tbl.put("filename",part.getContentDispositionFilename()); + tbl.put("content_type",part.getContentType()); + InputStream in = part.getInputStream(); + byte[] content = Utils.readAll(in); + in.close(); + tbl.put("content",content); + return tbl; + } catch(IOException e) { + throw new RuntimeException(e); + } catch(ServletException e) { + throw new RuntimeException(e); + } + } + + @Override Iterator<String> names() { + try { + List<String> names = new ArrayList<String>(); + for( Part part : mpis.getParts() ) { + names.add(part.getName()); + } + return names.iterator(); + } catch(IOException e) { + throw new RuntimeException(e); + } catch(ServletException e) { + throw new RuntimeException(e); + } + } + + @Override protected String type() { + return "request.parts-table"; + } + }; + } catch(IOException e) { + throw new RuntimeException(e); + } + } + } + return parts; + } } ); + return tbl; }