1 18 19 package org.apache.struts.upload; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionServlet; 34 import org.apache.struts.config.ModuleConfig; 35 import org.apache.struts.util.ModuleUtils; 36 37 44 public class DiskMultipartRequestHandler implements MultipartRequestHandler { 45 46 49 protected static Log log = 50 LogFactory.getLog(DiskMultipartRequestHandler.class); 51 52 55 protected ActionServlet servlet; 56 57 60 protected ActionMapping mapping; 61 62 65 protected Hashtable fileElements; 66 67 70 protected Hashtable textElements; 71 72 75 protected Hashtable allElements; 76 77 80 protected String tempDir; 81 82 83 88 public void handleRequest(HttpServletRequest request) throws ServletException { 89 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request); 90 this.retrieveTempDir(moduleConfig); 91 92 try { 93 MultipartIterator iterator = 94 new MultipartIterator( 95 request, 96 moduleConfig.getControllerConfig().getBufferSize(), 97 getMaxSize(moduleConfig.getControllerConfig().getMaxFileSize()), 98 tempDir); 99 100 MultipartElement element; 101 102 textElements = new Hashtable (); 103 fileElements = new Hashtable (); 104 allElements = new Hashtable (); 105 106 while ((element = iterator.getNextElement()) != null) { 107 if (!element.isFile()) { 108 createTextElement(request, element); 109 } else { 110 createDiskFile(element); 111 } 112 } 113 114 if (iterator.isMaxLengthExceeded()) { 116 request.setAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED, Boolean.TRUE); 117 } 118 119 } catch(IOException ioe) { 120 throw new ServletException (ioe); 121 } 122 } 123 124 protected void createTextElement(HttpServletRequest request, MultipartElement element) { 125 if (request instanceof MultipartRequestWrapper) { 126 ((MultipartRequestWrapper) request).setParameter(element.getName(), element.getValue()); 127 } 128 String [] textValues = (String []) textElements.get(element.getName()); 129 130 if (textValues != null) { 131 String [] textValues2 = new String [textValues.length + 1]; 132 System.arraycopy(textValues, 0, textValues2, 0, textValues.length); 133 textValues2[textValues.length] = element.getValue(); 134 textValues = textValues2; 135 } else { 136 textValues = new String [1]; 137 textValues[0] = element.getValue(); 138 } 139 textElements.put(element.getName(), textValues); 140 allElements.put(element.getName(), textValues); 141 } 142 143 protected void createDiskFile(MultipartElement element) { 144 File tempFile = element.getFile(); 145 if (tempFile.exists()) { 146 DiskFile theFile = new DiskFile(tempFile.getAbsolutePath()); 147 theFile.setContentType(element.getContentType()); 148 theFile.setFileName(element.getFileName()); 149 theFile.setFileSize((int) tempFile.length()); 150 fileElements.put(element.getName(), theFile); 151 allElements.put(element.getName(), theFile); 152 } 153 } 154 155 public Hashtable getAllElements() { 156 return allElements; 157 } 158 159 public Hashtable getTextElements() { 160 return textElements; 161 } 162 163 public Hashtable getFileElements() { 164 return fileElements; 165 } 166 167 170 public void rollback() { 171 Enumeration names = fileElements.keys(); 172 173 while (names.hasMoreElements()) { 174 String name = (String ) names.nextElement(); 175 DiskFile theFile = (DiskFile) fileElements.get(name); 176 theFile.destroy(); 177 } 178 } 179 180 184 public void finish() { 185 rollback(); 186 } 187 188 public void setServlet(ActionServlet servlet) { 189 this.servlet = servlet; 190 } 191 192 public void setMapping(ActionMapping mapping) { 193 this.mapping = mapping; 194 } 195 196 public ActionServlet getServlet() { 197 return servlet; 198 } 199 200 public ActionMapping getMapping() { 201 return mapping; 202 } 203 204 208 protected long getMaxSize(String stringSize) throws ServletException { 209 long size = -1; 210 int multiplier = 1; 211 212 if (stringSize.endsWith("K")) { 213 multiplier = 1024; 214 stringSize = stringSize.substring(0, stringSize.length() - 1); 215 } 216 if (stringSize.endsWith("M")) { 217 multiplier = 1024 * 1024; 218 stringSize = stringSize.substring(0, stringSize.length() - 1); 219 } else if (stringSize.endsWith("G")) { 220 multiplier = 1024 * 1024 * 1024; 221 stringSize = stringSize.substring(0, stringSize.length() - 1); 222 } 223 224 try { 225 size = Long.parseLong(stringSize); 226 } catch(NumberFormatException nfe) { 227 throw new ServletException ("Invalid format for maximum file size"); 228 } 229 230 return (size * multiplier); 231 } 232 233 237 protected void retrieveTempDir(ModuleConfig moduleConfig) { 238 239 ActionServlet servlet = getServlet(); 241 if (servlet != null) { 242 ServletContext context = servlet.getServletContext(); 244 245 try { 246 tempDir = 247 (String ) context.getAttribute("javax.servlet.context.tempdir"); 248 } catch(ClassCastException cce) { 249 tempDir = ((File ) context.getAttribute("javax.servlet.context.tempdir")).getAbsolutePath(); 250 } 251 } 252 253 if (tempDir == null) { 254 tempDir = moduleConfig.getControllerConfig().getTempDir(); 256 257 if (tempDir == null) { 258 tempDir = System.getProperty("java.io.tmpdir"); 260 261 log.debug( 262 "DiskMultipartRequestHandler.handleRequest(): " 263 + "defaulting to java.io.tmpdir directory \"" 264 + tempDir); 265 } 266 } 267 } 268 } 269 | Popular Tags |