1 41 package com.tonbeller.wcf.controller; 42 43 import java.io.UnsupportedEncodingException ; 44 import java.util.ArrayList ; 45 import java.util.Enumeration ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.MissingResourceException ; 51 import java.util.Vector ; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletRequestWrapper ; 55 56 import org.apache.commons.fileupload.DiskFileUpload; 57 import org.apache.commons.fileupload.FileItem; 58 import org.apache.commons.fileupload.FileUpload; 59 import org.apache.commons.fileupload.FileUploadException; 60 import org.apache.log4j.Logger; 61 62 import com.tonbeller.tbutils.res.Resources; 63 64 71 public class MultiPartEnabledRequest extends HttpServletRequestWrapper { 72 73 private static Logger logger = Logger.getLogger(MultiPartEnabledRequest.class); 74 private static Resources res = Resources.instance(MultiPartEnabledRequest.class); 75 76 private boolean multipart; 77 78 private Map fileItems = new HashMap (); 79 private Map httpParams = new HashMap (); 80 81 public MultiPartEnabledRequest(HttpServletRequest req) { 82 super(req); 83 this.multipart = FileUpload.isMultipartContent(req); 84 if (multipart) { 85 try { 86 readHttpParams(req); 87 } catch (FileUploadException e) { 88 logger.error("", e); 89 e.printStackTrace(); 90 } 91 } 92 } 93 94 private void readHttpParams(HttpServletRequest req) throws FileUploadException { 95 List all = uploadFiles(req); 96 97 for (Iterator it = all.iterator(); it.hasNext();) { 99 FileItem item = (FileItem) it.next(); 100 101 if (item.isFormField()) { 102 List valList = valueList(httpParams, item.getFieldName()); 103 if (req.getCharacterEncoding() != null) { 104 try { 105 valList.add(item.getString(req.getCharacterEncoding())); 106 } catch (UnsupportedEncodingException e) { 107 logger.error(null, e); 108 valList.add(item.getString()); 109 } 110 } 111 else 112 valList.add(item.getString()); 113 } else { 114 List valList = valueList(fileItems, item.getFieldName()); 115 valList.add(item); 116 } 117 } 118 119 for (Iterator it = httpParams.keySet().iterator(); it.hasNext();) { 121 String name = (String ) it.next(); 122 List valList = (List ) httpParams.get(name); 123 httpParams.put(name, toStringArray(valList)); 124 } 125 126 for (Iterator it = fileItems.keySet().iterator(); it.hasNext();) { 127 String name = (String ) it.next(); 128 List valList = (List ) fileItems.get(name); 129 fileItems.put(name, toFileItemArray(valList)); 130 } 131 } 132 133 private List valueList(Map params, String name) { 134 List valList = (List ) params.get(name); 135 if (valList == null) { 136 valList = new ArrayList (); 137 params.put(name, valList); 138 } 139 return valList; 140 } 141 142 private String [] toStringArray(List valList) { 143 String [] vals = new String [valList.size()]; 144 for (int i = 0; i < vals.length; i++) 145 vals[i] = (String ) valList.get(i); 146 return vals; 147 } 148 149 private FileItem[] toFileItemArray(List valList) { 150 FileItem[] vals = new FileItem[valList.size()]; 151 for (int i = 0; i < vals.length; i++) 152 vals[i] = (FileItem) valList.get(i); 153 return vals; 154 } 155 156 private List uploadFiles(HttpServletRequest req) throws FileUploadException { 157 158 DiskFileUpload upload = new DiskFileUpload(); 159 160 try { 161 upload.setSizeThreshold(res.getInteger("file.upload.size.threshold")); 162 } catch (MissingResourceException e) { 163 } 165 166 try { 167 upload.setSizeMax(res.getInteger("file.upload.size.max")); 168 } catch (MissingResourceException e) { 169 } 171 172 try { 173 upload.setRepositoryPath(res.getString("file.upload.repository")); 174 } catch (MissingResourceException e) { 175 } 177 178 List all = new DiskFileUpload().parseRequest(req); 179 return all; 180 } 181 182 public boolean isMultipart() { 183 return multipart; 184 } 185 186 public String getParameter(String name) { 187 if (!isMultipart()) 188 return super.getParameter(name); 189 190 String [] vals = (String []) httpParams.get(name); 191 if (vals == null) 192 return null; 193 194 return vals[0]; 195 } 196 197 public FileItem getFileParameter(String name) { 198 FileItem[] vals = (FileItem[]) fileItems.get(name); 199 if (vals == null) 200 return null; 201 202 return vals[0]; 203 } 204 205 public Map getParameterMap() { 206 if (!isMultipart()) 207 return super.getParameterMap(); 208 209 return new HashMap (httpParams); 210 } 211 212 public Map getFileParameterMap() { 213 return new HashMap (fileItems); 214 } 215 216 public Enumeration getParameterNames() { 217 if (!isMultipart()) 218 return super.getParameterNames(); 219 220 return new Vector (httpParams.keySet()).elements(); 221 } 222 223 public Enumeration getFileParameterNames() { 224 return new Vector (fileItems.keySet()).elements(); 225 } 226 227 public String [] getParameterValues(String name) { 228 if (!isMultipart()) 229 return super.getParameterValues(name); 230 231 return (String []) httpParams.get(name); 232 } 233 234 public FileItem[] getFileParameterValues(String name) { 235 return (FileItem[]) fileItems.get(name); 236 } 237 238 } | Popular Tags |