1 16 17 package org.springframework.web.servlet.support; 18 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.springframework.context.MessageSourceResolvable; 29 import org.springframework.context.NoSuchMessageException; 30 import org.springframework.ui.context.Theme; 31 import org.springframework.ui.context.ThemeSource; 32 import org.springframework.ui.context.support.ResourceBundleThemeSource; 33 import org.springframework.validation.BindException; 34 import org.springframework.validation.BindingResult; 35 import org.springframework.validation.Errors; 36 import org.springframework.web.bind.EscapedErrors; 37 import org.springframework.web.context.WebApplicationContext; 38 import org.springframework.web.servlet.LocaleResolver; 39 import org.springframework.web.util.HtmlUtils; 40 import org.springframework.web.util.UrlPathHelper; 41 import org.springframework.web.util.WebUtils; 42 43 66 public class RequestContext { 67 68 75 public final static String DEFAULT_THEME_NAME = "theme"; 76 77 87 public final static String JSTL_LOCALE_ATTRIBUTE = "javax.servlet.jsp.jstl.fmt.locale"; 88 89 90 91 protected static final String REQUEST_SCOPE_SUFFIX = ".request"; 92 93 94 protected static final String SESSION_SCOPE_SUFFIX = ".session"; 95 96 97 protected static final String APPLICATION_SCOPE_SUFFIX = ".application"; 98 99 100 private HttpServletRequest request; 101 102 private Map model; 103 104 private WebApplicationContext webApplicationContext; 105 106 private Locale locale; 107 108 private Theme theme; 109 110 private boolean defaultHtmlEscape; 111 112 private UrlPathHelper urlPathHelper; 113 114 private Map errorsMap; 115 116 117 129 public RequestContext(HttpServletRequest request) { 130 initContext(request, null, null); 131 } 132 133 147 public RequestContext(HttpServletRequest request, ServletContext servletContext) { 148 initContext(request, servletContext, null); 149 } 150 151 164 public RequestContext(HttpServletRequest request, Map model) { 165 initContext(request, null, model); 166 } 167 168 183 public RequestContext(HttpServletRequest request, ServletContext servletContext, Map model) { 184 initContext(request, servletContext, model); 185 } 186 187 190 protected RequestContext() { 191 } 192 193 194 210 protected void initContext(HttpServletRequest request, ServletContext servletContext, Map model) { 211 this.request = request; 212 this.model = model; 213 214 this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext); 217 218 LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); 220 if (localeResolver != null) { 221 this.locale = localeResolver.resolveLocale(request); 223 } 224 else { 225 this.locale = getFallbackLocale(); 227 } 228 229 this.theme = RequestContextUtils.getTheme(request); 231 if (this.theme == null) { 232 this.theme = getFallbackTheme(); 234 } 235 236 this.defaultHtmlEscape = WebUtils.isDefaultHtmlEscape(this.webApplicationContext.getServletContext()); 239 240 this.urlPathHelper = new UrlPathHelper(); 241 } 242 243 251 protected Locale getFallbackLocale() { 252 Locale locale = (Locale ) getRequest().getAttribute(JSTL_LOCALE_ATTRIBUTE); 253 if (locale == null) { 254 locale = (Locale ) getRequest().getAttribute(JSTL_LOCALE_ATTRIBUTE + REQUEST_SCOPE_SUFFIX); 255 if (locale == null) { 256 HttpSession session = getRequest().getSession(false); 257 if (session != null) { 258 locale = (Locale ) session.getAttribute(JSTL_LOCALE_ATTRIBUTE); 259 if (locale == null) { 260 locale = (Locale ) session.getAttribute(JSTL_LOCALE_ATTRIBUTE + SESSION_SCOPE_SUFFIX); 261 } 262 } 263 if (locale == null) { 264 locale = (Locale ) getServletContext().getAttribute(JSTL_LOCALE_ATTRIBUTE); 265 if (locale == null) { 266 locale = (Locale ) getServletContext().getAttribute(JSTL_LOCALE_ATTRIBUTE + APPLICATION_SCOPE_SUFFIX); 267 if (locale == null) { 268 locale = getRequest().getLocale(); 270 } 271 } 272 } 273 } 274 } 275 return locale; 276 } 277 278 283 protected Theme getFallbackTheme() { 284 ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest()); 285 if (themeSource == null) { 286 themeSource = new ResourceBundleThemeSource(); 287 } 288 return themeSource.getTheme(DEFAULT_THEME_NAME); 289 } 290 291 292 296 protected final HttpServletRequest getRequest() { 297 return this.request; 298 } 299 300 303 public final WebApplicationContext getWebApplicationContext() { 304 return this.webApplicationContext; 305 } 306 307 311 protected final ServletContext getServletContext() { 312 return this.webApplicationContext.getServletContext(); 313 } 314 315 318 public final Locale getLocale() { 319 return this.locale; 320 } 321 322 325 public final Theme getTheme() { 326 return this.theme; 327 } 328 329 330 336 public void setDefaultHtmlEscape(boolean defaultHtmlEscape) { 337 this.defaultHtmlEscape = defaultHtmlEscape; 338 } 339 340 343 public boolean isDefaultHtmlEscape() { 344 return this.defaultHtmlEscape; 345 } 346 347 352 public void setUrlPathHelper(UrlPathHelper urlPathHelper) { 353 this.urlPathHelper = (urlPathHelper != null ? urlPathHelper : new UrlPathHelper()); 354 } 355 356 361 public UrlPathHelper getUrlPathHelper() { 362 return this.urlPathHelper; 363 } 364 365 366 374 public String getContextPath() { 375 return this.urlPathHelper.getOriginatingContextPath(this.request); 376 } 377 378 393 public String getRequestUri() { 394 return this.urlPathHelper.getOriginatingRequestUri(this.request); 395 } 396 397 412 public String getQueryString() { 413 return this.urlPathHelper.getOriginatingQueryString(this.request); 414 } 415 416 417 423 public String getMessage(String code, String defaultMessage) { 424 return getMessage(code, null, defaultMessage, this.defaultHtmlEscape); 425 } 426 427 434 public String getMessage(String code, Object [] args, String defaultMessage) { 435 return getMessage(code, args, defaultMessage, this.defaultHtmlEscape); 436 } 437 438 445 public String getMessage(String code, List args, String defaultMessage) { 446 return getMessage(code, (args != null ? args.toArray() : null), defaultMessage, this.defaultHtmlEscape); 447 } 448 449 457 public String getMessage(String code, Object [] args, String defaultMessage, boolean htmlEscape) { 458 String msg = this.webApplicationContext.getMessage(code, args, defaultMessage, this.locale); 459 return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg); 460 } 461 462 468 public String getMessage(String code) throws NoSuchMessageException { 469 return getMessage(code, null, this.defaultHtmlEscape); 470 } 471 472 479 public String getMessage(String code, Object [] args) throws NoSuchMessageException { 480 return getMessage(code, args, this.defaultHtmlEscape); 481 } 482 483 490 public String getMessage(String code, List args) throws NoSuchMessageException { 491 return getMessage(code, (args != null ? args.toArray() : null), this.defaultHtmlEscape); 492 } 493 494 502 public String getMessage(String code, Object [] args, boolean htmlEscape) throws NoSuchMessageException { 503 String msg = this.webApplicationContext.getMessage(code, args, this.locale); 504 return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg); 505 } 506 507 514 public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException { 515 return getMessage(resolvable, this.defaultHtmlEscape); 516 } 517 518 525 public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException { 526 String msg = this.webApplicationContext.getMessage(resolvable, this.locale); 527 return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg); 528 } 529 530 531 539 public String getThemeMessage(String code, String defaultMessage) { 540 return this.theme.getMessageSource().getMessage(code, null, defaultMessage, this.locale); 541 } 542 543 552 public String getThemeMessage(String code, Object [] args, String defaultMessage) { 553 return this.theme.getMessageSource().getMessage(code, args, defaultMessage, this.locale); 554 } 555 556 565 public String getThemeMessage(String code, List args, String defaultMessage) { 566 return this.theme.getMessageSource().getMessage( 567 code, (args != null ? args.toArray() : null), defaultMessage, this.locale); 568 } 569 570 578 public String getThemeMessage(String code) throws NoSuchMessageException { 579 return this.theme.getMessageSource().getMessage(code, null, this.locale); 580 } 581 582 591 public String getThemeMessage(String code, Object [] args) throws NoSuchMessageException { 592 return this.theme.getMessageSource().getMessage(code, args, this.locale); 593 } 594 595 604 public String getThemeMessage(String code, List args) throws NoSuchMessageException { 605 return this.theme.getMessageSource().getMessage( 606 code, (args != null ? args.toArray() : null), this.locale); 607 } 608 609 617 public String getThemeMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException { 618 return this.theme.getMessageSource().getMessage(resolvable, this.locale); 619 } 620 621 622 628 public Errors getErrors(String name) { 629 return getErrors(name, this.defaultHtmlEscape); 630 } 631 632 638 public Errors getErrors(String name, boolean htmlEscape) { 639 if (this.errorsMap == null) { 640 this.errorsMap = new HashMap (); 641 } 642 Errors errors = (Errors) this.errorsMap.get(name); 643 boolean put = false; 644 if (errors == null) { 645 errors = (Errors) getModelObject(BindingResult.MODEL_KEY_PREFIX + name); 646 if (errors == null) { 648 errors = (Errors) getModelObject(BindException.ERROR_KEY_PREFIX + name); 649 } 650 if (errors == null) { 651 return null; 652 } 653 put = true; 654 } 655 if (htmlEscape && !(errors instanceof EscapedErrors)) { 656 errors = new EscapedErrors(errors); 657 put = true; 658 } 659 else if (!htmlEscape && errors instanceof EscapedErrors) { 660 errors = ((EscapedErrors) errors).getSource(); 661 put = true; 662 } 663 if (put) { 664 this.errorsMap.put(name, errors); 665 } 666 return errors; 667 } 668 669 675 protected Object getModelObject(String modelName) { 676 if (this.model != null) { 677 return this.model.get(modelName); 678 } 679 else { 680 return this.request.getAttribute(modelName); 681 } 682 } 683 684 692 public BindStatus getBindStatus(String path) throws IllegalStateException { 693 return new BindStatus(this, path, this.defaultHtmlEscape); 694 } 695 696 705 public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException { 706 return new BindStatus(this, path, htmlEscape); 707 } 708 709 } 710 | Popular Tags |