1 18 package org.apache.beehive.netui.pageflow; 19 20 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 import java.util.Map ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletContext ; 29 30 import org.apache.beehive.netui.util.logging.Logger; 32 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 33 import org.apache.beehive.netui.pageflow.scoping.ScopedRequest; 34 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 35 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 36 37 import org.apache.struts.Globals; 39 import org.apache.struts.action.ActionForm; 40 import org.apache.struts.action.ActionMapping; 41 import org.apache.struts.action.ActionServletWrapper; 42 import org.apache.struts.config.ModuleConfig; 43 import org.apache.struts.upload.MultipartRequestHandler; 44 import org.apache.struts.upload.MultipartRequestWrapper; 45 import org.apache.struts.util.RequestUtils; 46 47 54 class MultipartRequestUtils 55 { 56 private static final Logger _log = Logger.getInstance(MultipartRequestUtils.class); 57 58 private static final String PREHANDLED_MULTIPART_REQUEST_ATTR = InternalConstants.ATTR_PREFIX + "handledMultipart"; 59 60 90 static final Map handleMultipartRequest(HttpServletRequest request, ActionForm bean) 91 throws ServletException 92 { 93 String contentType = request.getContentType(); 94 String method = request.getMethod(); 95 boolean isMultipart = false; 96 97 Map multipartParameters = null; 98 99 if(contentType != null && 100 contentType.startsWith("multipart/form-data") && 101 method.equalsIgnoreCase("POST")) 102 { 103 if ( ! InternalUtils.isMultipartHandlingEnabled( request ) ) 104 { 105 throw new ServletException ( "Received a multipart request, but multipart handling is not enabled." ); 106 } 107 108 ActionServletWrapper servlet; 109 110 if (bean != null) 111 { 112 servlet = bean.getServletWrapper(); 113 } 114 else 115 { 116 ServletContext servletContext = InternalUtils.getServletContext(request); 117 servlet = new ActionServletWrapper(InternalUtils.getActionServlet(servletContext)); 118 } 119 120 128 MultipartRequestHandler multipartHandler = getCachedMultipartHandler(request); 129 boolean preHandled = false; 130 131 if (multipartHandler == null) 132 { 133 multipartHandler = getMultipartHandler(request); 134 } 135 else 136 { 137 preHandled = true; 138 } 139 140 if (bean != null) 141 { 142 bean.setMultipartRequestHandler(multipartHandler); 143 } 144 145 if(multipartHandler != null) 146 { 147 isMultipart = true; 148 149 servlet.setServletFor(multipartHandler); 150 multipartHandler.setMapping((ActionMapping)request.getAttribute(Globals.MAPPING_KEY)); 151 152 if (! preHandled) 153 { 154 multipartHandler.handleRequest(request); 155 } 156 157 Boolean maxLengthExceeded = (Boolean )request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED); 158 if(maxLengthExceeded != null && maxLengthExceeded.booleanValue()) 159 { 160 return null; 163 } 164 multipartParameters = MultipartRequestUtils.getAllParametersForMultipartRequest(request, multipartHandler); 165 166 } 169 } 170 171 if(!isMultipart) 172 { 173 return null; 178 } 179 else 180 { 181 ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest( PageFlowUtils.unwrapMultipart( request ) ); 182 183 if ( scopedRequest != null ) 184 { 185 multipartParameters = scopedRequest.filterParameterMap( multipartParameters ); 186 } 187 return multipartParameters; 188 } 189 } 190 191 194 static void preHandleMultipartRequest(HttpServletRequest request) 195 throws ServletException 196 { 197 MultipartRequestHandler multipartHandler = getCachedMultipartHandler(request); 198 199 if (multipartHandler == null) 200 { 201 multipartHandler = getMultipartHandler(request); 202 203 if(multipartHandler != null) 204 { 205 multipartHandler.handleRequest(request); 209 HttpServletRequest outerRequest = 210 ScopedServletUtils.getOuterRequest(PageFlowUtils.unwrapMultipart(request)); 211 outerRequest.setAttribute(PREHANDLED_MULTIPART_REQUEST_ATTR, multipartHandler ); 212 } 213 } 214 } 215 216 225 private static final MultipartRequestHandler getMultipartHandler(HttpServletRequest request) 227 throws ServletException 228 { 229 MultipartRequestHandler multipartHandler = null; 230 String multipartClass = (String ) request.getAttribute(Globals.MULTIPART_KEY); 231 request.removeAttribute(Globals.MULTIPART_KEY); 232 233 if (multipartClass != null) { 235 try { 236 multipartHandler = (MultipartRequestHandler)RequestUtils.applicationInstance(multipartClass); 237 } catch (ClassNotFoundException cnfe) { 238 _log.error( 239 "MultipartRequestHandler class \"" 240 + multipartClass 241 + "\" in mapping class not found, " 242 + "defaulting to global multipart class"); 243 } catch (InstantiationException ie) { 244 _log.error( 245 "InstantiaionException when instantiating " 246 + "MultipartRequestHandler \"" 247 + multipartClass 248 + "\", " 249 + "defaulting to global multipart class, exception: " 250 + ie.getMessage()); 251 } catch (IllegalAccessException iae) { 252 _log.error( 253 "IllegalAccessException when instantiating " 254 + "MultipartRequestHandler \"" 255 + multipartClass 256 + "\", " 257 + "defaulting to global multipart class, exception: " 258 + iae.getMessage()); 259 } 260 261 if (multipartHandler != null) 262 return multipartHandler; 263 } 264 265 ModuleConfig moduleConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY); 266 multipartClass = moduleConfig.getControllerConfig().getMultipartClass(); 267 268 if (multipartClass != null) { 270 try { 271 multipartHandler = (MultipartRequestHandler) RequestUtils.applicationInstance(multipartClass); 272 } catch (ClassNotFoundException cnfe) { 273 throw new ServletException ( 274 "Cannot find multipart class \"" 275 + multipartClass 276 + "\"" 277 + ", exception: " 278 + cnfe.getMessage()); 279 } catch (InstantiationException ie) { 280 throw new ServletException ( 281 "InstantiaionException when instantiating " 282 + "multipart class \"" 283 + multipartClass 284 + "\", exception: " 285 + ie.getMessage()); 286 } catch (IllegalAccessException iae) { 287 throw new ServletException ( 288 "IllegalAccessException when instantiating " 289 + "multipart class \"" 290 + multipartClass 291 + "\", exception: " 292 + iae.getMessage()); 293 } 294 295 if (multipartHandler != null) 296 return multipartHandler; 297 } 298 299 return multipartHandler; 300 } 301 302 314 private static final Map getAllParametersForMultipartRequest(HttpServletRequest request, MultipartRequestHandler multipartHandler) 316 { 317 Map parameters = new HashMap (); 318 Enumeration e; 319 320 Hashtable elements = multipartHandler.getAllElements(); 321 e = elements.keys(); 322 while (e.hasMoreElements()) { 323 String key = (String ) e.nextElement(); 324 parameters.put(key, elements.get(key)); 325 } 326 327 if (request instanceof MultipartRequestWrapper) { 328 request = ((MultipartRequestWrapper)request).getRequest(); 329 e = request.getParameterNames(); 330 while (e.hasMoreElements()) { 331 String key = (String ) e.nextElement(); 332 parameters.put(key, request.getParameterValues(key)); 333 } 334 } else { 335 _log.debug("Gathering multipart parameters for unwrapped request"); 336 } 337 338 return parameters; 339 } 340 341 static MultipartRequestHandler getCachedMultipartHandler( HttpServletRequest request ) 342 { 343 HttpServletRequest req = ScopedServletUtils.getOuterRequest( PageFlowUtils.unwrapMultipart( request ) ); 344 return ( MultipartRequestHandler ) req.getAttribute( MultipartRequestUtils.PREHANDLED_MULTIPART_REQUEST_ATTR ); 345 } 346 } 347 | Popular Tags |