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.getInstance().getModuleConfig(request); 795 } 796 797 String path = forward.getPath(); 798 String prefix = moduleConfig.getPrefix(); 800 801 if(forward.getModule() != null) { 803 prefix = forward.getModule(); 804 if ("/".equals(prefix)) { 805 prefix = ""; 806 } 807 } 808 809 StringBuffer sb = new StringBuffer (); 811 if (forward.getContextRelative()) { 812 if (!path.startsWith("/")) { 813 sb.append("/"); 814 } 815 sb.append(path); 816 return (sb.toString()); 817 } 818 819 String forwardPattern = moduleConfig.getControllerConfig().getForwardPattern(); 821 if (forwardPattern == null) { 822 sb.append(prefix); 824 if (!path.startsWith("/")) { 826 sb.append("/"); 827 } 828 sb.append(path); 829 830 } else { 831 boolean dollar = false; 832 for (int i = 0; i < forwardPattern.length(); i++) { 833 char ch = forwardPattern.charAt(i); 834 if (dollar) { 835 switch (ch) { 836 case 'M': 837 sb.append(prefix); 838 break; 839 case 'P': 840 if (!path.startsWith("/")) { 842 sb.append("/"); 843 } 844 sb.append(path); 845 break; 846 case '$': 847 sb.append('$'); 848 break; 849 default : 850 ; } 852 dollar = false; 853 continue; 854 } else if (ch == '$') { 855 dollar = true; 856 } else { 857 sb.append(ch); 858 } 859 } 860 } 861 862 return (sb.toString()); 863 864 } 865 866 867 876 public static URL requestURL(HttpServletRequest request) throws MalformedURLException { 877 878 StringBuffer url = requestToServerUriStringBuffer(request); 879 return (new URL (url.toString())); 880 881 } 882 883 884 895 public static URL serverURL(HttpServletRequest request) throws MalformedURLException { 896 897 StringBuffer url = requestToServerStringBuffer(request); 898 return (new URL (url.toString())); 899 900 } 901 902 903 914 public static StringBuffer requestToServerUriStringBuffer(HttpServletRequest request) { 915 916 StringBuffer serverUri = createServerUriStringBuffer(request.getScheme(),request.getServerName(), 917 request.getServerPort(),request.getRequestURI()); 918 return serverUri; 919 920 } 921 922 933 public static StringBuffer requestToServerStringBuffer(HttpServletRequest request) { 934 935 return createServerStringBuffer(request.getScheme(),request.getServerName(),request.getServerPort()); 936 937 } 938 939 940 951 public static StringBuffer createServerStringBuffer(String scheme,String server,int port) { 952 953 StringBuffer url = new StringBuffer (); 954 if (port < 0) { 955 port = 80; } 957 url.append(scheme); 958 url.append("://"); 959 url.append(server); 960 if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) { 961 url.append(':'); 962 url.append(port); 963 } 964 return url; 965 966 } 967 968 969 981 public static StringBuffer createServerUriStringBuffer(String scheme,String server,int port,String uri) { 982 983 StringBuffer serverUri = createServerStringBuffer(scheme,server,port); 984 serverUri.append(uri); 985 return serverUri; 986 987 } 988 989 990 992 993 994 1006 public static void selectModule( 1007 String prefix, 1008 HttpServletRequest request, 1009 ServletContext context) { 1010 1012 ModuleUtils.getInstance().selectModule(prefix, request, context); 1013 1014 } 1015 1016 1017 1027 public static void selectModule(HttpServletRequest request, ServletContext context) { 1028 1030 ModuleUtils.getInstance().selectModule(request, context); 1031 1032 } 1033 1034 1035 1043 public static String getModuleName(HttpServletRequest request, ServletContext context) { 1044 1046 return ModuleUtils.getInstance().getModuleName(request, context); 1047 1048 } 1049 1050 1051 1061 public static String getModuleName(String matchPath, ServletContext context) { 1062 1064 return ModuleUtils.getInstance().getModuleName(matchPath, context); 1065 1066 } 1067 1068 1069 1086 public static ModuleConfig getRequestModuleConfig(HttpServletRequest request) { 1087 1089 return ModuleUtils.getInstance().getModuleConfig(request); 1090 1091 } 1092 1093 1094 1105 public static ModuleConfig getModuleConfig( 1106 HttpServletRequest request, 1107 ServletContext context) { 1108 1110 return ModuleUtils.getInstance().getModuleConfig(request, context); 1111 1112 } 1113 1114 1115 1127 public static String [] getModulePrefixes(ServletContext context) { 1128 1130 return ModuleUtils.getInstance().getModulePrefixes(context); 1131 1132 } 1133 1134 1135 1137 1138 1170 public static Map computeParameters( 1171 PageContext pageContext, 1172 String paramId, 1173 String paramName, 1174 String paramProperty, 1175 String paramScope, 1176 String name, 1177 String property, 1178 String scope, 1179 boolean transaction) 1180 throws JspException { 1181 1183 return TagUtils.getInstance().computeParameters(pageContext, paramId, paramName, paramProperty, paramScope, 1184 name, property, scope, transaction); 1185 1186 } 1187 1188 1189 1210 public static String computeURL( 1211 PageContext pageContext, 1212 String forward, 1213 String href, 1214 String page, 1215 Map params, 1216 String anchor, 1217 boolean redirect) 1218 throws MalformedURLException { 1219 1221 return (TagUtils.getInstance().computeURLWithCharEncoding( 1222 pageContext, forward, href, page, null, null, params, anchor, redirect, false)); 1223 } 1224 1225 1226 1251 public static String computeURL( 1252 PageContext pageContext, 1253 String forward, 1254 String href, 1255 String page, 1256 String action, 1257 Map params, 1258 String anchor, 1259 boolean redirect) 1260 throws MalformedURLException { 1261 1263 return TagUtils.getInstance().computeURL( 1264 pageContext, 1265 forward, 1266 href, 1267 page, 1268 action, 1269 null, 1270 params, 1271 anchor, 1272 redirect); 1273 } 1274 1275 1276 1305 public static String computeURL( 1306 PageContext pageContext, 1307 String forward, 1308 String href, 1309 String page, 1310 String action, 1311 Map params, 1312 String anchor, 1313 boolean redirect, 1314 boolean encodeSeparator) 1315 throws MalformedURLException { 1316 1318 return (TagUtils.getInstance().computeURL( 1319 pageContext, 1320 forward, 1321 href, 1322 page, 1323 action, 1324 null, 1325 params, 1326 anchor, 1327 redirect, 1328 encodeSeparator)); 1329 } 1330 1331 1332 1345 public static String getActionMappingName(String action) { 1346 1348 return TagUtils.getInstance().getActionMappingName(action); 1349 1350 } 1351 1352 1353 1358 public static String getActionMappingURL( 1359 String action, 1360 PageContext pageContext) { 1361 1363 return TagUtils.getInstance().getActionMappingURL(action, pageContext); 1364 1365 } 1366 1367 1368 1385 public static Object lookup(PageContext pageContext, String name, String scopeName) 1386 throws JspException { 1387 1389 return TagUtils.getInstance().lookup(pageContext, name, scopeName); 1390 1391 } 1392 1393 1394 1407 public static int getScope(String scopeName) throws JspException { 1408 1410 return TagUtils.getInstance().getScope(scopeName); 1411 1412 } 1413 1414 1415 1439 public static Object lookup( 1440 PageContext pageContext, 1441 String name, 1442 String property, 1443 String scope) 1444 throws JspException { 1445 1447 return TagUtils.getInstance().lookup(pageContext, name, property, scope); 1448 1449 } 1450 1451 1452 1463 public static Locale retrieveUserLocale(PageContext pageContext, String locale) { 1464 1466 return TagUtils.getInstance().getUserLocale(pageContext, locale); 1467 1468 } 1469 1470 1471 1486 public static String message( 1487 PageContext pageContext, 1488 String bundle, 1489 String locale, 1490 String key) 1491 throws JspException { 1492 1494 return TagUtils.getInstance().message(pageContext, bundle, locale, key); 1495 1496 } 1497 1498 1499 1514 public static String message( 1515 PageContext pageContext, 1516 String bundle, 1517 String locale, 1518 String key, 1519 Object args[]) 1520 throws JspException { 1521 1523 return TagUtils.getInstance().message( 1524 pageContext, 1525 bundle, 1526 locale, 1527 key, 1528 args); 1529 } 1530 1531 1532 1548 public static boolean present( 1549 PageContext pageContext, 1550 String bundle, 1551 String locale, 1552 String key) 1553 throws JspException { 1554 1556 return TagUtils.getInstance().present(pageContext, bundle, locale, key); 1557 1558 } 1559 1560 1561 1577 public static String pageURL(HttpServletRequest request, String page) { 1578 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request); 1580 1581 return TagUtils.getInstance().pageURL(request, page, moduleConfig); 1582 1584 } 1585 1586 1587 1596 public static void saveException(PageContext pageContext, Throwable exception) { 1597 1598 TagUtils.getInstance().saveException(pageContext, exception); 1599 1601 } 1602 1603 1604 1614 public static ModuleConfig getModuleConfig(PageContext pageContext) { 1615 1617 return TagUtils.getInstance().getModuleConfig(pageContext); 1618 1619 } 1620 1621 1622 1634 public static ActionMessages getActionMessages(PageContext pageContext, String paramName) 1635 throws JspException { 1636 1638 return TagUtils.getInstance().getActionMessages(pageContext, paramName); 1639 } 1640 1641 1642 1655 public static ActionErrors getActionErrors(PageContext pageContext, String paramName) 1656 throws JspException { 1657 1659 return TagUtils.getInstance().getActionErrors(pageContext, paramName); 1660 } 1661 1662 1663 1672 public static String encodeURL(String url) { 1673 1675 return TagUtils.getInstance().encodeURL(url); 1676 1677 } 1678 1679 1680 1687 public static boolean isXhtml(PageContext pageContext) { 1688 1690 String xhtml = 1691 (String ) pageContext.getAttribute( 1692 Globals.XHTML_KEY, 1693 PageContext.PAGE_SCOPE); 1694 1695 return "true".equalsIgnoreCase(xhtml); 1696 1697 } 1698 1699} 1700 | Popular Tags |