comparison web/src/luan/modules/web/HttpServicer.java @ 361:0581238084ad

fix HTTP parameters for multipart
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 15 Apr 2015 16:58:51 -0600
parents 8848edb0e6bf
children 9dbf3433f70f
comparison
equal deleted inserted replaced
360:cbb94a7c7a9e 361:0581238084ad
109 109
110 private final HttpServletRequest request; 110 private final HttpServletRequest request;
111 private final HttpServletResponse response; 111 private final HttpServletResponse response;
112 // private PrintWriter writer = null; 112 // private PrintWriter writer = null;
113 // private ServletOutputStream sos = null; 113 // private ServletOutputStream sos = null;
114 private LuanTable parts = null;
115 114
116 private HttpServicer(HttpServletRequest request,HttpServletResponse response) { 115 private HttpServicer(HttpServletRequest request,HttpServletResponse response) {
117 this.request = request; 116 this.request = request;
118 this.response = response; 117 this.response = response;
119 } 118 }
208 return "request.cookies-table"; 207 return "request.cookies-table";
209 } 208 }
210 }; 209 };
211 tbl.put( "cookies", cookies ); 210 tbl.put( "cookies", cookies );
212 211
213 tbl.put( "parts", new LuanProperty() { public Object get() { 212 String contentType = request.getContentType();
214 if( parts == null ) { 213 if( contentType!=null && contentType.startsWith("multipart/form-data") ) {
215 String contentType = request.getContentType(); 214 try {
216 if( contentType!=null && contentType.startsWith("multipart/form-data") ) { 215 InputStream in = new BufferedInputStream(request.getInputStream());
217 try { 216 final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null);
218 InputStream in = new BufferedInputStream(request.getInputStream()); 217 mpis.setDeleteOnExit(true);
219 final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null); 218 parameters = Luan.newTable();
220 mpis.setDeleteOnExit(true); 219 LuanTable parts = Luan.newTable();
221 parts = new NameTable() { 220 for( Part p : mpis.getParts() ) {
222 221 final MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)p;
223 @Override Object get(String name) { 222 String name = part.getName();
224 try { 223 String filename = part.getContentDispositionFilename();
225 MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)mpis.getPart(name); 224 if( filename == null ) {
226 if( part==null ) 225 String value = new String(part.getBytes());
227 return null; 226 parameters.put(name,value);
228 LuanTable tbl = Luan.newTable(); 227 } else {
229 tbl.put("filename",part.getContentDispositionFilename()); 228 LuanTable partTbl = Luan.newPropertyTable();
230 tbl.put("content_type",part.getContentType()); 229 partTbl.put("filename",filename);
231 InputStream in = part.getInputStream(); 230 partTbl.put("content_type",part.getContentType());
232 byte[] content = Utils.readAll(in); 231 partTbl.put( "content", new LuanProperty() { public Object get() {
233 in.close(); 232 try {
234 tbl.put("content",content); 233 InputStream in = part.getInputStream();
235 return tbl; 234 byte[] content = Utils.readAll(in);
236 } catch(IOException e) { 235 in.close();
237 throw new RuntimeException(e); 236 return content;
238 } catch(ServletException e) { 237 } catch(IOException e) {
239 throw new RuntimeException(e); 238 throw new RuntimeException(e);
240 }
241 } 239 }
242 240 } } );
243 @Override Iterator<String> names() { 241 parts.put(name,partTbl);
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 } 242 }
264 } 243 }
265 } 244 tbl.put( "parameters", parameters );
266 return parts; 245 tbl.put( "parts", parts );
267 } } ); 246 } catch(IOException e) {
247 throw new RuntimeException(e);
248 } catch(ServletException e) {
249 throw new RuntimeException(e);
250 }
251 }
268 252
269 return tbl; 253 return tbl;
270 } 254 }
271 255
272 private LuanTable responseTable() throws NoSuchMethodException { 256 private LuanTable responseTable() throws NoSuchMethodException {