1 5 package com.opensymphony.webwork.dispatcher.multipart; 6 7 import com.opensymphony.webwork.config.Configuration; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import javax.servlet.http.HttpServletRequest ; 12 import java.io.File ; 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.*; 16 17 18 36 public class MultiPartRequestWrapper extends WebWorkRequestWrapper { 37 protected static final Log log = LogFactory.getLog(MultiPartRequestWrapper.class); 38 39 Collection errors; 40 MultiPartRequest multi; 41 42 49 public MultiPartRequestWrapper(HttpServletRequest request, String saveDir, int maxSize) { 50 super(request); 51 52 if (request instanceof MultiPartRequest) { 53 multi = (MultiPartRequest) request; 54 } else { 55 String parser = Configuration.getString("webwork.multipart.parser"); 56 57 if (parser.equals("")) { 59 log.warn("Property webwork.multipart.parser not set." + 60 " Using com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest"); 61 parser = "com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest"; 62 } 63 else if (parser.equals("pell")) { 65 parser = "com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest"; 66 } else if (parser.equals("cos")) { 67 parser = "com.opensymphony.webwork.dispatcher.multipart.CosMultiPartRequest"; 68 } else if (parser.equals("jakarta")) { 69 parser = "com.opensymphony.webwork.dispatcher.multipart.JakartaMultiPartRequest"; 70 } 71 72 try { 73 Class baseClazz = com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest.class; 74 75 Class clazz = Class.forName(parser); 76 77 if (!baseClazz.isAssignableFrom(clazz)) { 79 addError("Class '" + parser + "' does not extend MultiPartRequest"); 80 81 return; 82 } 83 84 Constructor ctor = clazz.getDeclaredConstructor(new Class []{ 86 Class.forName("javax.servlet.http.HttpServletRequest"), 87 java.lang.String .class, int.class 88 }); 89 90 Object [] parms = new Object []{ 92 request, saveDir, new Integer (maxSize) 93 }; 94 95 multi = (MultiPartRequest) ctor.newInstance(parms); 97 for (Iterator iter = multi.getErrors().iterator(); iter.hasNext();) { 98 String error = (String ) iter.next(); 99 addError(error); 100 } 101 } catch (ClassNotFoundException e) { 102 addError("Class: " + parser + " not found."); 103 } catch (NoSuchMethodException e) { 104 addError("Constructor error for " + parser + ": " + e); 105 } catch (InstantiationException e) { 106 addError("Error instantiating " + parser + ": " + e); 107 } catch (IllegalAccessException e) { 108 addError("Access errror for " + parser + ": " + e); 109 } catch (InvocationTargetException e) { 110 addError(e.getTargetException().toString()); 112 } 113 } 114 } 115 116 119 public Enumeration getFileNames() { 120 return getFileParameterNames(); 121 } 122 123 128 public Enumeration getFileParameterNames() { 129 if (multi == null) { 130 return null; 131 } 132 133 return multi.getFileParameterNames(); 134 } 135 136 139 public String getContentType(String fieldName) { 140 String [] contentTypes = getContentTypes(fieldName); 141 if (contentTypes != null && contentTypes.length > 0) { 142 return contentTypes[0]; 143 } 144 145 return null; 146 } 147 148 149 156 public String [] getContentTypes(String name) { 157 if (multi == null) { 158 return null; 159 } 160 161 return multi.getContentType(name); 162 } 163 164 167 public File getFile(String fieldName) { 168 File [] files = getFiles(fieldName); 169 if (files != null && files.length > 0) { 170 return files[0]; 171 } 172 173 return null; 174 } 175 176 182 public File [] getFiles(String fieldName) { 183 if (multi == null) { 184 return null; 185 } 186 187 return multi.getFile(fieldName); 188 } 189 190 195 public String [] getFileNames(String fieldName) { 196 if (multi == null) { 197 return null; 198 } 199 200 return multi.getFileNames(fieldName); 201 } 202 203 206 public String getFilesystemName(String fieldName) { 207 String [] names = getFileSystemNames(fieldName); 208 if (names != null && names.length > 0) { 209 return names[0]; 210 } 211 212 return null; 213 } 214 215 223 public String [] getFileSystemNames(String fieldName) { 224 if (multi == null) { 225 return null; 226 } 227 228 return multi.getFilesystemName(fieldName); 229 } 230 231 234 public String getParameter(String name) { 235 return ((multi == null) || (multi.getParameter(name) == null)) ? super.getParameter(name) : multi.getParameter(name); 236 } 237 238 241 public Map getParameterMap() { 242 Map map = new HashMap(); 243 Enumeration enumeration = getParameterNames(); 244 245 while (enumeration.hasMoreElements()) { 246 String name = (String ) enumeration.nextElement(); 247 map.put(name, this.getParameterValues(name)); 248 } 249 250 return map; 251 } 252 253 256 public Enumeration getParameterNames() { 257 if (multi == null) { 258 return super.getParameterNames(); 259 } else { 260 return mergeParams(multi.getParameterNames(), super.getParameterNames()); 261 } 262 } 263 264 267 public String [] getParameterValues(String name) { 268 return ((multi == null) || (multi.getParameterValues(name) == null)) ? super.getParameterValues(name) : multi.getParameterValues(name); 269 } 270 271 276 public boolean hasErrors() { 277 if ((errors == null) || errors.isEmpty()) { 278 return false; 279 } else { 280 return true; 281 } 282 } 283 284 289 public Collection getErrors() { 290 return errors; 291 } 292 293 298 protected void addError(String anErrorMessage) { 299 if (errors == null) { 300 errors = new ArrayList(); 301 } 302 303 errors.add(anErrorMessage); 304 } 305 306 313 protected Enumeration mergeParams(Enumeration params1, Enumeration params2) { 314 Vector temp = new Vector(); 315 316 while (params1.hasMoreElements()) { 317 temp.add(params1.nextElement()); 318 } 319 320 while (params2.hasMoreElements()) { 321 temp.add(params2.nextElement()); 322 } 323 324 return temp.elements(); 325 } 326 } 327 | Popular Tags |