| 1 18 19 package org.apache.struts.util; 20 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.PageContext ; 36 37 import org.apache.commons.beanutils.BeanUtils; 38 import org.apache.commons.beanutils.DynaBean; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.apache.struts.Globals; 42 import org.apache.struts.action.ActionErrors; 43 import org.apache.struts.action.ActionForm; 44 import org.apache.struts.action.ActionMapping; 45 import org.apache.struts.action.ActionMessages; 46 import org.apache.struts.action.ActionServlet; 47 import org.apache.struts.action.ActionServletWrapper; 48 import org.apache.struts.action.DynaActionForm; 49 import org.apache.struts.action.DynaActionFormClass; 50 import org.apache.struts.config.ActionConfig; 51 import org.apache.struts.config.FormBeanConfig; 52 import org.apache.struts.config.ForwardConfig; 53 import org.apache.struts.config.ModuleConfig; 54 import org.apache.struts.taglib.TagUtils; 55 import org.apache.struts.upload.MultipartRequestHandler; 56 import org.apache.struts.upload.MultipartRequestWrapper; 57 58 64 public class RequestUtils { 65 66 67 69 70 73 protected static Log log = LogFactory.getLog(RequestUtils.class); 74 75 76 78 79 91 public static URL absoluteURL(HttpServletRequest request, String path) 92 throws MalformedURLException { 93 94 return (new URL (serverURL(request), request.getContextPath() + path)); 95 96 } 97 98 99 108 public static Class applicationClass(String className) throws ClassNotFoundException { 109 110 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 112 if (classLoader == null) { 113 classLoader = RequestUtils.class.getClassLoader(); 114 } 115 116 return (classLoader.loadClass(className)); 118 119 } 120 121 122 140 public static Object applicationInstance(String className) 141 throws ClassNotFoundException , IllegalAccessException , InstantiationException { 142 143 return (applicationClass(className).newInstance()); 144 145 } 146 147 159 public static ActionForm createActionForm( 160 HttpServletRequest request, 161 ActionMapping mapping, 162 ModuleConfig moduleConfig, 163 ActionServlet servlet) { 164 165 String attribute = mapping.getAttribute(); 167 if (attribute == null) { 168 return (null); 169 } 170 171 String name = mapping.getName(); 173 FormBeanConfig config = moduleConfig.findFormBeanConfig(name); 174 if (config == null) { 175 log.warn("No FormBeanConfig found under '" + name + "'"); 176 return (null); 177 } 178 179 ActionForm instance = lookupActionForm(request, attribute, mapping.getScope()); 180 181 try { 183 if (instance != null && canReuseActionForm(instance, config)) { 184 return (instance); 185 } 186 } catch(ClassNotFoundException e) { 187 log.error(servlet.getInternal().getMessage("formBean", config.getType()), e); 188 return (null); 189 } 190 191 return createActionForm(config, servlet); 192 } 193 194 195 196 private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope) 197 { 198 if (log.isDebugEnabled()) { 200 log.debug( 201 " Looking for ActionForm bean instance in scope '" 202 + scope 203 + "' under attribute key '" 204 + attribute 205 + "'"); 206 } 207 ActionForm instance = null; 208 HttpSession session = null; 209 if ("request".equals(scope)) { 210 instance = (ActionForm) request.getAttribute(attribute); 211 } else { 212 session = request.getSession(); 213 instance = (ActionForm) session.getAttribute(attribute); 214 } 215 216 return (instance); 217 } 218 219 232 private static boolean canReuseActionForm(ActionForm instance, FormBeanConfig config) 233 throws ClassNotFoundException  234 { 235 if (instance == null) { 236 return (false); 237 } 238 239 boolean canReuse = false; 240 String formType = null; 241 String className = null; 242 243 if (config.getDynamic()) { 244 className = ((DynaBean) instance).getDynaClass().getName(); 245 canReuse = className.equals(config.getName()); 246 formType = "DynaActionForm"; 247 } else { 248 Class configClass = applicationClass(config.getType()); 249 className = instance.getClass().getName(); 250 canReuse = configClass.isAssignableFrom(instance.getClass()); 251 formType = "ActionForm"; 252 } 253 254 if (log.isDebugEnabled()) { 255 log.debug( 256 " Can recycle existing " 257 + formType 258 + " instance " 259 + "of type '" 260 + className 261 + "'?: " 262 + canReuse); 263 log.trace(" --> " + instance); 264 } 265 return (canReuse); 266 } 267 268 280 public static ActionForm createActionForm(FormBeanConfig config, ActionServlet servlet) 281 { 282 if (config == null) 283 { 284 return (null); 285 } 286 287 ActionForm instance = null; 288 289 try { 291 292 instance = config.createActionForm(servlet); 293 if (log.isDebugEnabled()) { 294 log.debug( 295 " Creating new " 296 + (config.getDynamic() ? "DynaActionForm" : "ActionForm") 297 + " instance of type '" 298 + config.getType() 299 + "'"); 300 log.trace(" --> " + instance); 301 } 302 303 } catch(Throwable t) { 304 log.error(servlet.getInternal().getMessage("formBean", config.getType()), t); 305 } 306 307 return (instance); 308 309 } 310 311 312 321 public static Locale getUserLocale(HttpServletRequest request, String locale) { 322 323 Locale userLocale = null; 324 HttpSession session = request.getSession(false); 325 326 if (locale == null) { 327 locale = Globals.LOCALE_KEY; 328 } 329 330 if (session != null) { 332 userLocale = (Locale ) session.getAttribute(locale); 333 } 334 335 if (userLocale == null) { 336 userLocale = request.getLocale(); 338 } 339 340 return userLocale; 341 342 } 343 344 345 359 public static void populate(Object bean, HttpServletRequest request) throws ServletException { 360 361 populate(bean, null, null, request); 362 363 } 364 365 366 391 public static void populate( 392 Object bean, 393 String prefix, 394 String suffix, 395 HttpServletRequest request) 396 throws ServletException { 397 398 HashMap properties = new HashMap (); 400 Enumeration names = null; 402 Map multipartParameters = null; 404 405 String contentType = request.getContentType(); 406 String method = request.getMethod(); 407 boolean isMultipart = false; 408 409 if ((contentType != null) 410 && (contentType.startsWith("multipart/form-data")) 411 && (method.equalsIgnoreCase("POST"))) { 412 413 ActionServletWrapper servlet; 415 if (bean instanceof ActionForm) { 416 servlet = ((ActionForm) bean).getServletWrapper(); 417 } else { 418 throw new ServletException ( 419 "bean that's supposed to be " 420 + "populated from a multipart request is not of type " 421 + "\"org.apache.struts.action.ActionForm\", but type " 422 + "\"" 423 + bean.getClass().getName() 424 + "\""); 425 } 426 427 MultipartRequestHandler multipartHandler = getMultipartHandler(request); 429 430 ((ActionForm) bean).setMultipartRequestHandler(multipartHandler); 435 436 if (multipartHandler != null) { 437 isMultipart = true; 438 servlet.setServletFor(multipartHandler); 440 multipartHandler.setMapping( 441 (ActionMapping) request.getAttribute(Globals.MAPPING_KEY)); 442 multipartHandler.handleRequest(request); 444 Boolean maxLengthExceeded = 446 (Boolean ) request.getAttribute( 447 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED); 448 if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) { 449 return; 450 } 451 multipartParameters = getAllParametersForMultipartRequest( 453 request, multipartHandler); 454 names = Collections.enumeration(multipartParameters.keySet()); 455 } 456 } 457 458 if (!isMultipart) { 459 names = request.getParameterNames(); 460 } 461 462 while (names.hasMoreElements()) { 463 String name = (String ) names.nextElement(); 464 String stripped = name; 465 if (prefix != null) { 466 if (!stripped.startsWith(prefix)) { 467 continue; 468 } 469 stripped = stripped.substring(prefix.length()); 470 } 471 if (suffix != null) { 472 if (!stripped.endsWith(suffix)) { 473 continue; 474 } 475 stripped = stripped.substring(0, stripped.length() - suffix.length()); 476 } 477 Object parameterValue = null; 478 if (isMultipart) { 479 parameterValue = multipartParameters.get(name); 480 } else { 481 parameterValue = request.getParameterValues(name); 482 } 483 484 if (!(stripped.startsWith("org.apache.struts."))) { 487 properties.put(stripped, parameterValue); 488 } 489 } 490 491 try { 493 BeanUtils.populate(bean, properties); 494 } catch(Exception e) { 495 throw new ServletException ("BeanUtils.populate", e); 496 } 497 498 } 499 500 501 514 private static MultipartRequestHandler getMultipartHandler(HttpServletRequest request) 515 throws ServletException { 516 517 MultipartRequestHandler multipartHandler = null; 518 String multipartClass = (String ) request.getAttribute(Globals.MULTIPART_KEY); 519 request.removeAttribute(Globals.MULTIPART_KEY); 520 521 if (multipartClass != null) { 523 try { 524 multipartHandler = (MultipartRequestHandler) applicationInstance(multipartClass); 525 } catch(ClassNotFoundException cnfe) { 526 log.error( 527 "MultipartRequestHandler class \"" 528 + multipartClass 529 + "\" in mapping class not found, " 530 + "defaulting to global multipart class"); 531 } catch(InstantiationException ie) { 532 log.error( 533 "InstantiationException when instantiating " 534 + "MultipartRequestHandler \"" 535 + multipartClass 536 + "\", " 537 + "defaulting to global multipart class, exception: " 538 + ie.getMessage()); 539 } catch(IllegalAccessException iae) { 540 log.error( 541 "IllegalAccessException when instantiating " 542 + "MultipartRequestHandler \"" 543 + multipartClass 544 + "\", " 545 + "defaulting to global multipart class, exception: " 546 + iae.getMessage()); 547 } 548 549 if (multipartHandler != null) { 550 return multipartHandler; 551 } 552 } 553 554 ModuleConfig moduleConfig = 555 ModuleUtils.getInstance().getModuleConfig(request); 556 557 multipartClass = moduleConfig.getControllerConfig().getMultipartClass(); 558 559 if (multipartClass != null) { 561 try { 562 multipartHandler = (MultipartRequestHandler) applicationInstance(multipartClass); 563 564 } catch(ClassNotFoundException cnfe) { 565 throw new ServletException ( 566 "Cannot find multipart class \"" 567 + multipartClass 568 + "\"" 569 + ", exception: " 570 + cnfe.getMessage()); 571 572 } catch(InstantiationException ie) { 573 throw new ServletException ( 574 "InstantiationException when instantiating " 575 + "multipart class \"" 576 + multipartClass 577 + "\", exception: " 578 + ie.getMessage()); 579 580 } catch(IllegalAccessException iae) { 581 throw new ServletException ( 582 "IllegalAccessException when instantiating " 583 + "multipart class \"" 584 + multipartClass 585 + "\", exception: " 586 + iae.getMessage()); 587 } 588 589 if (multipartHandler != null) { 590 return multipartHandler; 591 } 592 } 593 594 return multipartHandler; 595 } 596 597 598 609 private static Map getAllParametersForMultipartRequest( 610 HttpServletRequest request, 611 MultipartRequestHandler multipartHandler) { 612 613 Map parameters = new HashMap (); 614 Hashtable elements = multipartHandler.getAllElements(); 615 Enumeration e = elements.keys(); 616 while (e.hasMoreElements()) { 617 String key = (String ) e.nextElement(); 618 parameters.put(key, elements.get(key)); 619 } 620 621 if (request instanceof MultipartRequestWrapper) { 622 request = ((MultipartRequestWrapper) request).getRequest(); 623 e = request.getParameterNames(); 624 while (e.hasMoreElements()) { 625 String key = (String ) e.nextElement(); 626 parameters.put(key, request.getParameterValues(key)); 627 } 628 } else { 629 log.debug("Gathering multipart parameters for unwrapped request"); 630 } 631 632 return parameters; 633 } 634 635 636 645 public static String printableURL(URL url) { 646 647 if (url.getHost() != null) { 648 return (url.toString()); 649 } 650 651 String file = url.getFile(); 652 String ref = url.getRef(); 653 if (ref == null) { 654 return (file); 655 } else { 656 StringBuffer sb = new StringBuffer (file); 657 sb.append('#'); 658 sb.append(ref); 659 return (sb.toString()); 660 } 661 662 } 663 664 665 678 public static String actionURL( 679 HttpServletRequest request, 680 ActionConfig action, 681 String pattern) { 682 683 StringBuffer sb = new StringBuffer (); 684 if (pattern.endsWith("/*")) { 685 sb.append(pattern.substring(0, pattern.length() - 2)); 686 sb.append(action.getPath()); 687 688 } else if (pattern.startsWith("*.")) { 689 ModuleConfig appConfig = 690 ModuleUtils.getInstance().getModuleConfig(request); 691 sb.append(appConfig.getPrefix()); 692 sb.append(action.getPath()); 693 sb.append(pattern.substring(1)); 694 695 } else { 696 throw new IllegalArgumentException (pattern); 697 } 698 699 return sb.toString(); 700 701 } 702 744 public static String forwardURL(HttpServletRequest request, ForwardConfig forward) { 745 return forwardURL(request,forward,null); 746 } 747 748 791 public static String forwardURL(HttpServletRequest request, ForwardConfig forward, ModuleConfig moduleConfig) { 792 if(moduleConfig == null) { 794 moduleConfig = ModuleUtils.getInstan
|