1 25 package org.snipsnap.net.filter; 26 27 import org.radeox.util.logging.Logger; 28 import org.snipsnap.util.mail.InputStreamDataSource; 29 30 import javax.mail.BodyPart ; 31 import javax.mail.MessagingException ; 32 import javax.mail.internet.ContentDisposition ; 33 import javax.mail.internet.HeaderTokenizer ; 34 import javax.mail.internet.MimeBodyPart ; 35 import javax.mail.internet.MimeMultipart ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletRequestWrapper ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.BufferedInputStream ; 41 import java.util.Enumeration ; 42 import java.util.HashMap ; 43 import java.util.Hashtable ; 44 import java.util.Map ; 45 46 53 public class MultipartWrapper extends HttpServletRequestWrapper { 54 55 private String encoding = "UTF-8"; 56 private MimeMultipart multipart = null; 57 private Hashtable params = null; 58 private Map files = new HashMap (); 59 60 public MultipartWrapper(HttpServletRequest request, String enc) throws IOException , IllegalArgumentException { 61 super(request); 62 63 if(null != enc) { 64 encoding = enc; 65 "".getBytes(enc); 66 } 67 68 Logger.debug("MultipartWrapper: parsing input data ..."); 69 InputStreamDataSource ds = new InputStreamDataSource(new BufferedInputStream (request.getInputStream()), request.getContentType()); 70 try { 71 multipart = new MimeMultipart (ds); 72 params = new Hashtable (request.getParameterMap()); 73 74 int count = multipart.getCount(); 75 for (int i = 0; i < count; i++) { 76 MimeBodyPart body = (MimeBodyPart ) multipart.getBodyPart(i); 77 ContentDisposition disp = new ContentDisposition (body.getHeader("content-disposition", null)); 78 String name = disp.getParameter("name"); 79 if (body.getContentType().startsWith("text/plain")) { 80 String value = new String (((String ) body.getContent()).getBytes("iso-8859-1"), encoding); 81 String [] values = (String []) params.get(name); 82 if (null == values) { 83 params.put(name, new String []{value}); 84 } else { 85 String [] tmp = new String [values.length + 1]; 86 System.arraycopy(values, 0, tmp, 0, values.length); 87 tmp[tmp.length - 1] = value; 88 params.put(name, tmp); 89 } 90 } 91 files.put(name, body); 92 } 93 } catch (MessagingException e) { 94 Logger.warn("Error parsing request (not multipart/form-data)", e); 95 throw new IllegalArgumentException ("Error parsing request (not multipart/form-data?)"); 96 } 97 Logger.debug("MultipartWrapper: finished parsing input data ..."); 98 } 99 100 public Enumeration getParameterNames() { 101 return params.keys(); 102 } 103 104 public String getParameter(String name) { 105 String [] values = (String []) params.get(name); 106 if (values != null && values.length > 0) { 107 return values[0]; 108 } 109 return null; 110 } 111 112 public String [] getParameterValues(String name) { 113 return (String []) params.get(name); 114 } 115 116 119 public Map getParameterMap() { 120 return params; 121 } 122 123 129 public BodyPart getBodyPart(String name) { 130 return (BodyPart ) files.get(name); 131 } 132 133 137 public String getFileName(String name) throws IOException { 138 139 String fileName = null; 140 MimeBodyPart part = (MimeBodyPart ) getBodyPart(name); 141 try { 142 String contentDisposition = new String (part.getHeader("content-disposition", null).getBytes("iso-8859-1"), encoding); 145 Logger.log(Logger.DEBUG, "content-disposition: " + contentDisposition); 146 HeaderTokenizer tokenizer = new HeaderTokenizer (contentDisposition, HeaderTokenizer.MIME, true); 147 HeaderTokenizer.Token token = null; 148 while ((token = tokenizer.next()).getType() != HeaderTokenizer.Token.EOF) { 149 if (token.getType() == HeaderTokenizer.Token.ATOM && "filename".equals(token.getValue())) { 150 String remainder = tokenizer.getRemainder(); 151 int quoteStart = remainder.indexOf('"'); 152 int quoteEnd = remainder.indexOf('"', quoteStart + 1); 153 fileName = remainder.substring(quoteStart + 1, quoteEnd); 154 } 155 } 156 } catch (Exception e) { 157 Logger.log(Logger.FATAL, "error parsing uploaded file name: "+e.getMessage()); 158 } 159 160 if (null == fileName && part != null) { 161 try { 162 fileName = new String (part.getFileName().getBytes("iso-8859-1"), encoding); 163 } catch (Exception e) { 164 e.printStackTrace(); 165 throw new IOException (e.getMessage()); 166 } 167 } 168 Logger.log(Logger.DEBUG, "file name: '" + fileName + "'"); 169 return fileName; 170 } 171 172 177 public String getFileContentType(String name) { 178 BodyPart part = getBodyPart(name); 179 if (part != null) { 180 try { 181 return part.getContentType(); 182 } catch (MessagingException e) { 183 } 185 } 186 return null; 187 } 188 189 public InputStream getFileInputStream(String name) throws IOException { 190 BodyPart part = getBodyPart(name); 191 try { 192 return part.getInputStream(); 193 } catch (MessagingException e) { 194 throw new IOException (e.getMessage()); 195 } 196 } 197 198 public int getFileContentLength(String name) throws IOException { 199 BodyPart part = getBodyPart(name); 200 if (part != null) { 201 try { 202 return part.getSize(); 203 } catch (MessagingException e) { 204 } 206 } 207 return -1; 208 209 } 210 211 } 212 213 | Popular Tags |