1 16 17 package org.apache.velocity.tools.struts; 18 19 20 import java.util.Locale ; 21 import java.util.Iterator ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpSession ; 26 import javax.sql.DataSource ; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionErrors; 31 import org.apache.struts.action.ActionMessage; 32 import org.apache.struts.action.ActionMessages; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.config.ForwardConfig; 35 import org.apache.struts.config.ActionConfig; 36 import org.apache.struts.config.FormBeanConfig; 37 import org.apache.struts.upload.MultipartRequestWrapper; 38 import org.apache.struts.util.MessageResources; 39 import org.apache.struts.util.RequestUtils; 40 41 42 import org.apache.struts.action.ActionFormBeans; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionForwards; 45 import org.apache.struts.action.ActionMapping; 46 import org.apache.struts.action.ActionMappings; 47 48 69 public class StrutsUtils 70 { 71 72 73 74 81 public static DataSource getDataSource(ServletContext app) 82 { 83 return (DataSource )app.getAttribute(Globals.DATA_SOURCE_KEY); 84 } 85 86 87 94 public static ActionFormBeans getActionFormBeans(ServletContext app) 95 { 96 return (ActionFormBeans)app.getAttribute(Globals.FORM_BEANS_KEY); 97 } 98 99 100 108 public static FormBeanConfig getFormBean(String name, ServletContext app) 109 { 110 ActionFormBeans formBeans = getActionFormBeans(app); 111 if (formBeans == null) 112 { 113 return null; 114 } 115 return formBeans.findFormBean(name); 116 117 } 118 119 120 127 public static ActionForwards getActionForwards(ServletContext app) 128 { 129 return (ActionForwards)app.getAttribute(Globals.FORWARDS_KEY); 130 } 131 132 133 141 public static ActionForward getActionForward(String name, 142 ServletContext app) 143 { 144 ActionForwards forwards = getActionForwards(app); 145 if (forwards == null) 146 { 147 return null; 148 } 149 return forwards.findForward(name); 150 } 151 152 153 160 public static ActionMappings getActionMappings(ServletContext app) 161 { 162 return (ActionMappings)app.getAttribute(Globals.MAPPINGS_KEY); 163 } 164 165 166 174 public static ActionMapping getActionMapping(String path, 175 ServletContext app) 176 { 177 ActionMappings mappings = getActionMappings(app); 178 if (mappings == null) 179 { 180 return null; 181 } 182 return mappings.findMapping(path); 183 } 184 185 186 193 public static MessageResources getMessageResources(ServletContext app) 194 { 195 return (MessageResources)app.getAttribute(Globals.MESSAGES_KEY); 196 } 197 198 199 206 public static MessageResources getMessageResources(HttpServletRequest request, 207 ServletContext app) 208 { 209 210 ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, app); 211 return (MessageResources)app.getAttribute(Globals.MESSAGES_KEY + 212 moduleConfig.getPrefix()); 213 } 214 215 216 225 public static MessageResources getMessageResources(HttpServletRequest request, 226 ServletContext app, 227 String bundle) 228 { 229 MessageResources resources = null; 230 231 232 ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, app); 233 234 235 if (bundle == null) { 236 bundle = Globals.MESSAGES_KEY; 237 } 238 239 resources = (MessageResources) request.getAttribute(bundle + moduleConfig.getPrefix()); 241 242 if (resources == null) { 243 resources = (MessageResources) app.getAttribute(bundle + moduleConfig.getPrefix()); 244 } 245 246 return resources; 247 } 248 249 250 259 public static ModuleConfig selectModule(String urlPath, 260 ServletContext app) 261 { 262 263 String prefix = RequestUtils.getModuleName(urlPath, app); 264 265 266 ModuleConfig config = (ModuleConfig) 267 app.getAttribute(Globals.MODULE_KEY + prefix); 268 269 return config; 270 } 271 272 273 282 public static String getServletMapping(ServletContext app) 283 { 284 return (String )app.getAttribute(Globals.SERVLET_KEY); 285 } 286 287 288 289 290 298 public static Locale getLocale(HttpServletRequest request, 299 HttpSession session) 300 { 301 Locale locale = null; 302 303 if (session != null) 304 { 305 locale = (Locale )session.getAttribute(Globals.LOCALE_KEY); 306 } 307 if (locale == null) 308 { 309 locale = request.getLocale(); 310 } 311 return locale; 312 } 313 314 315 321 public static String getToken(HttpSession session) 322 { 323 if (session == null) 324 { 325 return null; 326 } 327 return (String )session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 328 } 329 330 331 332 333 340 public static ActionMessages getErrors(HttpServletRequest request) 341 { 342 return (ActionMessages)request.getAttribute(Globals.ERROR_KEY); 343 } 344 345 352 public static ActionMessages getMessages(HttpServletRequest request) 353 { 354 return (ActionMessages)request.getAttribute(Globals.MESSAGE_KEY); 355 } 356 357 358 361 public static ActionErrors getActionErrors(HttpServletRequest request) 362 { 363 return (ActionErrors)getErrors(request); 364 } 365 366 369 public static ActionMessages getActionMessages(HttpServletRequest request) 370 { 371 return getMessages(request); 372 } 373 374 375 383 public static Throwable getException(HttpServletRequest request) 384 { 385 return (Throwable )request.getAttribute(Globals.EXCEPTION_KEY); 386 } 387 388 389 396 public static MultipartRequestWrapper getMultipartRequestWrapper(HttpServletRequest request) 397 { 398 return (MultipartRequestWrapper)request.getAttribute(Globals.MULTIPART_KEY); 399 } 400 401 402 409 public static ActionForm getActionForm(HttpServletRequest request, 410 HttpSession session) 411 { 412 413 ActionConfig mapping = 414 (ActionConfig)request.getAttribute(Globals.MAPPING_KEY); 415 if (mapping == null) 416 { 417 return null; 418 } 419 420 421 String attribute = mapping.getAttribute(); 422 if (attribute == null) 423 { 424 return null; 425 } 426 427 428 if ("request".equals(mapping.getScope())) 429 { 430 return (ActionForm)request.getAttribute(attribute); 431 } 432 if (session != null) 433 { 434 return (ActionForm)session.getAttribute(attribute); 435 } 436 return null; 437 } 438 439 440 441 442 448 public static String getCancelName() 449 { 450 return org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY; 451 } 452 453 454 460 public static String getGlobalErrorName() 461 { 462 return org.apache.struts.action.ActionErrors.GLOBAL_ERROR; 463 } 464 465 466 472 public static String getTokenName() 473 { 474 return org.apache.struts.taglib.html.Constants.TOKEN_KEY; 475 } 476 477 478 479 480 494 public static String getActionMappingName(String action) 495 { 496 return RequestUtils.getActionMappingName(action); 497 } 498 499 500 508 public static String getActionMappingURL(ServletContext application, 509 HttpServletRequest request, 510 String action) 511 { 512 StringBuffer value = new StringBuffer (request.getContextPath()); 513 ModuleConfig config = 514 (ModuleConfig)request.getAttribute(Globals.MODULE_KEY); 515 if (config != null) 516 { 517 value.append(config.getPrefix()); 518 } 519 520 521 String servletMapping = 522 (String )application.getAttribute(Globals.SERVLET_KEY); 523 524 if (servletMapping != null) 525 { 526 String queryString = null; 527 int question = action.indexOf("?"); 528 529 if (question >= 0) 530 { 531 queryString = action.substring(question); 532 } 533 534 String actionMapping = RequestUtils.getActionMappingName(action); 535 536 if (servletMapping.startsWith("*.")) 537 { 538 value.append(actionMapping); 539 value.append(servletMapping.substring(1)); 540 } 541 else if (servletMapping.endsWith("/*")) 542 { 543 value.append(servletMapping.substring 544 (0, servletMapping.length() - 2)); 545 value.append(actionMapping); 546 } 547 548 if (queryString != null) 549 { 550 value.append(queryString); 551 } 552 } 553 else 554 { 555 557 if (!action.startsWith("/")) 558 { 559 value.append("/"); 560 } 561 value.append(action); 562 } 563 564 565 return value.toString(); 566 } 567 568 569 577 public static String getForwardURL(HttpServletRequest request, 578 ServletContext app, 579 String forward) 580 { 581 ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, app); 582 ForwardConfig fc = moduleConfig.findForwardConfig(forward); 584 if (fc == null) 585 { 586 return null; 587 } 588 589 StringBuffer url = new StringBuffer (); 590 if (fc.getPath().startsWith("/")) 591 { 592 url.append(request.getContextPath()); 593 url.append(RequestUtils.forwardURL(request, fc)); 594 } 595 else 596 { 597 url.append(fc.getPath()); 598 } 599 return url.toString(); 600 } 601 602 603 618 public static String errorMarkup(String property, 619 HttpServletRequest request, 620 HttpSession session, 621 ServletContext application) 622 { 623 return errorMarkup(property, null, request, session, application); 624 } 625 626 627 643 public static String errorMarkup(String property, 644 String bundle, 645 HttpServletRequest request, 646 HttpSession session, 647 ServletContext application) 648 { 649 ActionErrors errors = getActionErrors(request); 650 if (errors == null) 651 { 652 return ""; 653 } 654 655 656 Iterator reports = null; 657 if (property == null) 658 { 659 reports = errors.get(); 660 } 661 else 662 { 663 reports = errors.get(property); 664 } 665 666 if (!reports.hasNext()) 667 { 668 return ""; 669 } 670 671 672 StringBuffer results = new StringBuffer (); 673 String header = null; 674 String footer = null; 675 Locale locale = getLocale(request, session); 676 677 MessageResources resources = 678 getMessageResources(request, application, bundle); 679 if (resources != null) 680 { 681 header = resources.getMessage(locale, "errors.header"); 682 footer = resources.getMessage(locale, "errors.footer"); 683 } 684 if (header == null) 685 { 686 header = "errors.header"; 687 } 688 if (footer == null) 689 { 690 footer = "errors.footer"; 691 } 692 693 results.append(header); 694 results.append("\r\n"); 695 696 String message; 697 while (reports.hasNext()) 698 { 699 message = null; 700 ActionMessage report = (ActionMessage)reports.next(); 701 if (resources != null) 702 { 703 message = resources.getMessage(locale, 704 report.getKey(), 705 report.getValues()); 706 } 707 if (message != null) 708 { 709 results.append(message); 710 results.append("\r\n"); 711 } 712 else 713 { 714 results.append(report.getKey()); 715 results.append("\r\n"); 716 } 717 } 718 719 results.append(footer); 720 results.append("\r\n"); 721 722 723 return results.toString(); 724 } 725 726 } 727 | Popular Tags |