1 9 package org.lucane.server.web; 10 11 12 import java.io.IOException ; 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 16 import javax.mail.MessagingException ; 17 import javax.mail.internet.MimeMultipart ; 18 import javax.servlet.ServletRequest ; 19 20 30 public class MultipartRequest { 31 32 private int m_Limit; 34 35 private ServletRequest m_Request; 36 private Hashtable m_Parameters; 37 private FormdataMultipart m_FormdataMultipart; 38 39 44 public MultipartRequest(ServletRequest request, int limit) 45 throws IOException { 46 47 m_Limit=limit; 49 50 m_Request=request; 51 processRequest(); 52 } 54 55 62 public Enumeration getParameterNames(){ 63 return m_Parameters.keys(); 64 } 66 77 public String getParameter(String name){ 78 if(m_Parameters.containsKey(name)) { 79 return ((String [])m_Parameters.get(name))[0]; 80 } else { 81 return null; 82 } 83 } 85 98 public String [] getParameterValues(String name){ 99 if(m_Parameters.containsKey(name)) { 100 return (String [])m_Parameters.get(name); 101 } else { 102 return null; 103 } 104 } 106 111 public boolean hasAttachments() { 112 try { 113 return (m_FormdataMultipart.getCount()>0); 114 } catch (MessagingException ex) { 115 return false; 116 } 117 } 119 125 public MimeMultipart getAttachments() { 126 return (MimeMultipart ) m_FormdataMultipart; 127 } 129 138 public void processRequest() 139 throws IOException { 140 141 int length=m_Request.getContentLength(); 143 if (length>m_Limit) { 144 throw new IOException ("Posted data exceeds limit of " + 145 m_Limit + " bytes."); 146 } 147 String ctype=m_Request.getContentType(); 149 if(ctype.indexOf("--")==-1) { 150 throw new IOException ("Data malformed, missing multipart boundary:"+ctype); 151 } 152 if(ctype.indexOf("multipart/form-data")==-1) { 153 throw new IOException ("Can only handle an incoming multipart/form-data stream:"+ctype); 154 } 155 156 MultipartInputStream m_InputStream= new MultipartInputStream( 157 m_Request.getInputStream(),ctype,m_Limit 158 ); 159 try { 160 m_FormdataMultipart=new FormdataMultipart(m_InputStream); 161 m_Parameters=m_FormdataMultipart.getParameters(); 162 163 } catch (MessagingException mex) { 164 throw new IOException (mex.getMessage()); 165 } 166 } 168 169 173 private static final int FORMDATA_LIMIT=1024 * 1024; 175 } 177 178 179 180 | Popular Tags |