1 5 package com.opensymphony.webwork.interceptor; 6 7 import com.opensymphony.webwork.ServletActionContext; 8 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper; 9 import com.opensymphony.xwork.ActionInvocation; 10 import com.opensymphony.xwork.ValidationAware; 11 import com.opensymphony.xwork.interceptor.Interceptor; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import java.io.File ; 16 import java.util.*; 17 18 30 public class FileUploadInterceptor implements Interceptor { 31 33 protected static final Log log = LogFactory.getLog(FileUploadInterceptor.class); 34 35 private static final String DEFAULT_DELIMITER = ","; 36 37 39 protected Long maximumSize; 40 protected String allowedTypes; 41 protected String disallowedTypes; 42 protected Set allowedTypesSet; 43 protected Set disallowedTypesSet; 44 45 47 public void setAllowedTypes(String allowedTypes) { 48 this.allowedTypes = allowedTypes; 49 50 allowedTypesSet = getDelimitedValues(allowedTypes); 52 } 53 54 public void setDisallowedTypes(String disallowedTypes) { 55 this.disallowedTypes = disallowedTypes; 56 57 disallowedTypesSet = getDelimitedValues(disallowedTypes); 59 } 60 61 public void setMaximumSize(Long maximumSize) { 62 this.maximumSize = maximumSize; 63 } 64 65 public void destroy() { 66 } 67 68 public void init() { 69 } 70 71 public String intercept(ActionInvocation invocation) throws Exception { 72 if (!(ServletActionContext.getRequest() instanceof MultiPartRequestWrapper)) { 73 if (log.isDebugEnabled()) { 74 log.debug("bypass " + invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName()); 75 } 76 77 return invocation.invoke(); 78 } 79 80 final Object action = invocation.getAction(); 81 ValidationAware validation = null; 82 83 if (action instanceof ValidationAware) { 84 validation = (ValidationAware) action; 85 } 86 87 MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest(); 88 89 if (multiWrapper.hasErrors()) { 90 for (Iterator errorIter = multiWrapper.getErrors().iterator(); errorIter.hasNext();) { 91 String error = (String ) errorIter.next(); 92 93 if (validation != null) { 94 validation.addActionError(error); 95 } 96 97 log.error(error); 98 } 99 } 100 101 Map parameters = invocation.getInvocationContext().getParameters(); 102 103 Enumeration fileParameterNames = multiWrapper.getFileParameterNames(); 105 while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { 106 String inputName = (String ) fileParameterNames.nextElement(); 108 109 String [] contentType = multiWrapper.getContentTypes(inputName); 111 112 if (isNonEmpty(contentType)) { 113 String [] fileName = multiWrapper.getFileNames(inputName); 115 116 if (isNonEmpty(fileName)) { 117 File [] files = multiWrapper.getFiles(inputName); 119 if (files != null) { 120 for (int index = 0; index < files.length; index++) { 121 log.info("file " + inputName + " " + contentType[index] + " " + fileName[index] + " " + files[index]); 122 123 if (acceptFile(files[0], contentType[0], inputName, validation)) { 124 parameters.put(inputName, files); 125 parameters.put(inputName + "ContentType", contentType); 126 parameters.put(inputName + "FileName", fileName); 127 } 128 } 129 } 130 } else { 131 log.error("Could not find a Filename for " + inputName + ". Verify that a valid file was submitted."); 132 } 133 } else { 134 log.error("Could not find a Content-Type for " + inputName + ". Verify that a valid file was submitted."); 135 } 136 } 137 138 String result = invocation.invoke(); 140 141 fileParameterNames = multiWrapper.getFileParameterNames(); 143 while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { 144 String inputValue = (String ) fileParameterNames.nextElement(); 145 File [] file = multiWrapper.getFiles(inputValue); 146 for (int index = 0; index < file.length; index++) { 147 File currentFile = file[index]; 148 log.info("removing file " + inputValue + " " + currentFile); 149 150 if ((currentFile != null) && currentFile.isFile()) { 151 currentFile.delete(); 152 } 153 } 154 } 155 156 return result; 157 } 158 159 168 protected boolean acceptFile(File file, String contentType, String inputName, ValidationAware validation) { 169 boolean fileIsAcceptable = false; 170 171 if (file == null) { 173 if (validation != null) { 174 validation.addFieldError(inputName, "Could not upload file."); 175 } 176 177 log.error("Error uploading: " + inputName); 178 179 } else if (maximumSize != null && maximumSize.longValue() < file.length()) { 180 String errMsg = "File too large: " + inputName + " \"" + file.getName() + "\" " + file.length(); 181 if (validation != null) { 182 validation.addFieldError(inputName, errMsg); 183 } 184 185 log.error(errMsg); 186 187 } else if (containsItem(disallowedTypesSet, contentType)) { 188 String errMsg = "Content-Type disallowed: " + inputName + " \"" + file.getName() + "\" " + contentType; 189 if (validation != null) { 190 validation.addFieldError(inputName, errMsg); 191 } 192 193 log.error(errMsg); 194 195 } else if (!containsItem(allowedTypesSet, contentType)) { 196 String errMsg = "Content-Type not allowed: " + inputName + " \"" + file.getName() + "\" " + contentType; 197 if (validation != null) { 198 validation.addFieldError(inputName, errMsg); 199 } 200 201 log.error(errMsg); 202 203 } else { 204 fileIsAcceptable = true; 205 } 206 207 return fileIsAcceptable; 208 } 209 210 215 private static boolean containsItem(Collection itemCollection, String key) { 216 return itemCollection.contains(key.toLowerCase()); 217 } 218 219 private static Set getDelimitedValues(String delimitedString) { 220 Set delimitedValues = new HashSet(); 221 if (delimitedString != null) { 222 StringTokenizer stringTokenizer = new StringTokenizer(delimitedString, DEFAULT_DELIMITER); 223 while (stringTokenizer.hasMoreTokens()) { 224 String nextToken = stringTokenizer.nextToken().toLowerCase().trim(); 225 if (nextToken.length() > 0) { 226 delimitedValues.add(nextToken); 227 } 228 } 229 } 230 return delimitedValues; 231 } 232 233 private static boolean isNonEmpty(Object [] objArray) { 234 boolean result = false; 235 for (int index = 0; index < objArray.length && !result; index++) { 236 if (objArray[index] != null) { 237 result = true; 238 } 239 } 240 return result; 241 } 242 } 243 | Popular Tags |