1 package org.apache.turbine.services.rundata; 2 3 56 57 import java.io.IOException ; 58 import java.io.PrintWriter ; 59 import java.util.Hashtable ; 60 import java.util.Locale ; 61 import java.util.Vector ; 62 63 import javax.servlet.ServletConfig ; 64 import javax.servlet.ServletContext ; 65 import javax.servlet.http.HttpServletRequest ; 66 import javax.servlet.http.HttpServletResponse ; 67 import javax.servlet.http.HttpSession ; 68 69 import org.apache.commons.collections.FastHashMap; 70 import org.apache.commons.logging.Log; 71 import org.apache.commons.logging.LogFactory; 72 import org.apache.fulcrum.TurbineServices; 73 import org.apache.fulcrum.localization.LocalizationService; 74 import org.apache.fulcrum.mimetype.MimeTypeService; 75 import org.apache.fulcrum.parser.CookieParser; 76 import org.apache.fulcrum.parser.ParameterParser; 77 import org.apache.fulcrum.pool.Recyclable; 78 import org.apache.fulcrum.security.entity.User; 79 import org.apache.fulcrum.security.util.AccessControlList; 80 import org.apache.turbine.Turbine; 81 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 82 83 103 public class DefaultTurbineRunData implements TurbineRunData, Recyclable { 104 private static final Log log = LogFactory.getLog(DefaultTurbineRunData.class); 105 106 109 private boolean disposed; 110 111 114 private static Locale defaultLocale; 115 116 119 private static boolean defaultLocaleChecked; 120 121 124 private static String defaultCharSet; 125 126 129 private static boolean defaultCharSetChecked; 130 131 134 private ParameterParser parameters; 135 136 139 public CookieParser cookies; 140 141 144 private HttpServletRequest req; 145 146 149 private HttpServletResponse res; 150 151 154 private HttpSession session; 155 156 159 private ServletConfig config; 160 161 165 private ServletContext servletContext; 166 167 170 private AccessControlList acl; 171 172 175 private String action; 176 177 180 private String templateEncoding; 181 182 185 private User user; 186 187 190 private String title; 191 192 195 private Locale locale; 196 197 200 private String charSet; 201 202 205 private String contentType = "text/html"; 206 207 210 private String redirectURI; 211 212 215 private int statusCode = HttpServletResponse.SC_OK; 216 217 220 private Vector errors = new Vector (); 221 222 225 private String remoteAddr; 226 227 230 private String remoteHost; 231 232 235 private String userAgent; 236 237 240 private String stackTrace; 241 242 245 private Throwable stackTraceException; 246 247 251 private Hashtable varDebug = new Hashtable (); 252 253 private String target; 254 255 private String message; 256 257 private FastHashMap temp; 258 259 261 262 private String serverName = null; 263 264 265 private int serverPort = 80; 266 267 268 private String serverScheme = null; 269 270 271 private String scriptName = null; 272 273 274 private String contextPath = null; 275 276 public void setTarget(String target) { 277 this.target = target; 278 } 279 280 public String getTarget() { 281 return target; 282 } 283 284 public boolean hasTarget() { 285 return (target != null); 286 } 287 288 public void setTemp(String key, Object value) { 289 temp.put(key, value); 290 } 291 292 public Object getTemp(String key) { 293 return temp.get(key); 294 } 295 296 299 public DefaultTurbineRunData() { 300 recycle(); 301 302 temp = new FastHashMap(); 306 } 307 308 311 public void recycle() { 312 disposed = false; 313 } 314 315 318 public void dispose() { 319 target = null; 320 parameters = null; 321 cookies = null; 322 req = null; 323 res = null; 324 session = null; 325 config = null; 326 servletContext = null; 327 acl = null; 328 action = null; 329 templateEncoding = null; 330 user = null; 331 title = null; 332 locale = null; 333 charSet = null; 334 contentType = "text/html"; 335 redirectURI = null; 336 statusCode = HttpServletResponse.SC_OK; 337 errors.clear(); 338 remoteAddr = null; 339 remoteHost = null; 340 userAgent = null; 341 stackTrace = null; 342 stackTraceException = null; 343 varDebug.clear(); 344 message = null; 345 346 temp.clear(); 349 350 356 disposed = true; 357 } 358 359 364 public boolean isDisposed() { 365 return disposed; 366 } 367 368 372 378 public ParameterParser getParameters() { 379 if ((this.parameters != null) && (this.parameters.getRequest() == null)) { 381 this.parameters.setRequest(this.req); 382 } 383 return this.parameters; 384 } 385 386 391 public CookieParser getCookies() { 392 return this.cookies; 393 } 394 395 400 public HttpServletRequest getRequest() { 401 return this.req; 402 } 403 404 409 public HttpServletResponse getResponse() { 410 return this.res; 411 } 412 413 418 public HttpSession getSession() { 419 return session; 420 } 421 422 427 public ServletConfig getServletConfig() { 428 return this.config; 429 } 430 431 436 public ServletContext getServletContext() { 437 return this.servletContext; 438 } 439 440 445 public AccessControlList getACL() { 446 return acl; 447 } 448 449 455 public void setACL(AccessControlList acl) { 456 this.acl = acl; 457 } 458 459 464 public boolean hasAction() { 465 return (this.action != null && this.action.length() > 0 && !this.action.equalsIgnoreCase("null")); 466 } 467 468 474 public String getAction() { 475 return (hasAction() ? this.action : ""); 476 } 477 478 484 public void setAction(String action) { 485 this.action = action; 486 } 487 488 493 public String getTemplateEncoding() { 494 return templateEncoding; 495 } 496 497 503 public void setTemplateEncoding(String encoding) { 504 templateEncoding = encoding; 505 } 506 507 512 public String getTitle() { 513 return (this.title == null ? "" : this.title); 514 } 515 516 522 public void setTitle(String title) { 523 this.title = title; 524 } 525 526 531 public boolean userExists() { 532 user = getUserFromSession(); 533 return (user != null); 534 } 535 536 541 public User getUser() { 542 return this.user; 543 } 544 545 551 public void setUser(User user) { 552 this.user = user; 553 } 554 555 573 public static User getUserFromSession(HttpSession session) { 574 try { 575 SessionBindingEventProxy proxy = (SessionBindingEventProxy) session.getAttribute(User.SESSION_KEY); 576 577 return (proxy == null ? null : (User) proxy.getListener()); 581 } catch (ClassCastException e) { 582 String message = "User object did not implement User interface. " 583 + "if you are sure the interface is implemented, the user " 584 + "object in the session and this class may be loaded from " 585 + "different classloaders. This has been known to happen " 586 + "when using multiple turbine apps in tomcat that interact " 587 + "through the use of RequestDispatcher.include or forward."; 588 if (log != null) { 589 log.error(message, e); 590 } else { 591 System.err.println(message); 592 e.printStackTrace(); 593 } 594 595 return null; 596 } 597 } 598 599 606 public static boolean removeUserFromSession(HttpSession session) { 607 try { 608 session.removeAttribute(User.SESSION_KEY); 609 } catch (Exception e) { 610 return false; 611 } 612 return true; 613 } 614 615 618 public static AccessControlList getACLFromSession(HttpSession session) { 619 try { 620 AccessControlList acl = (AccessControlList) session.getAttribute(AccessControlList.SESSION_KEY); 621 622 return (acl == null ? null : acl); 624 } catch (ClassCastException e) { 625 return null; 626 } 627 } 628 629 636 public static boolean removeACLFromSession(HttpSession session) { 637 try { 638 session.removeAttribute(AccessControlList.SESSION_KEY); 639 } catch (Exception e) { 640 return false; 641 } 642 return true; 643 } 644 645 651 public User getUserFromSession() { 652 return getUserFromSession(session); 653 } 654 655 660 public boolean removeUserFromSession() { 661 return removeUserFromSession(session); 662 } 663 664 670 public AccessControlList getACLFromSession() { 671 return getACLFromSession(session); 672 } 673 674 679 public boolean removeACLFromSession() { 680 return removeACLFromSession(session); 681 } 682 683 689 public PrintWriter getOut() throws IOException { 690 return res.getWriter(); 691 } 692 693 701 protected static Locale getDefaultLocale() { 702 if (!defaultLocaleChecked) { 703 704 String lang = Turbine.getConfiguration().getString("locale.default.language"); 705 String country = Turbine.getConfiguration().getString("locale.default.country"); 706 if (lang != null) { 707 defaultLocale = country != null ? new Locale (lang, country) : new Locale (lang, ""); 708 } else if (country != null) { 709 defaultLocale = new Locale ("", country); 710 } else { 711 defaultLocale = Locale.getDefault(); 712 } 713 defaultLocaleChecked = true; 714 } 715 return defaultLocale; 716 } 717 718 724 protected String getDefaultCharSet() { 725 if (!defaultCharSetChecked) { 726 729 defaultCharSet = Turbine.getConfiguration().getString("locale.default.charset"); 730 731 if (defaultCharSet == null) { 732 735 Locale locale = getLocale(); 736 737 defaultCharSet = getMimeTypeService().getCharSet(locale); 738 739 } 740 defaultCharSetChecked = true; 741 } 742 return defaultCharSet; 743 } 744 745 753 public Locale getLocale() { 754 if (locale == null) { 755 locale = getLocalizationService().getLocale(getRequest()); 756 if (locale == null) { 757 locale = getDefaultLocale(); 758 } 759 } 760 return locale; 761 } 762 763 769 public void setLocale(Locale locale) { 770 this.locale = locale; 771 } 772 773 786 public String getCharSet() { 787 if (charSet == null) { 788 charSet = getDefaultCharSet(); 789 if (charSet == null) { 790 charSet = getMimeTypeService().getCharSet(getLocale()); 791 } 792 } 793 return charSet; 794 } 795 796 802 public void setCharSet(String charset) { 803 this.charSet = charset; 804 } 805 806 816 public String getContentType() { 817 String ct = this.contentType; 818 if (ct != null) { 819 if (ct.startsWith("text/")) { 820 String charset = getCharSet(); 821 if (charset != null) { 822 ct += "; charset=" + charset; 823 } 824 } 825 } else { 826 ct = ""; 827 } 828 return ct; 829 } 830 831 837 public void setContentType(String ct) { 838 this.contentType = ct; 839 } 840 841 847 public String getRedirectURI() { 848 return (this.redirectURI == null ? "" : redirectURI); 849 } 850 851 858 public void setRedirectURI(String ruri) { 859 this.redirectURI = ruri; 860 } 861 862 867 public int getStatusCode() { 868 return statusCode; 869 } 870 871 877 public void setStatusCode(int sc) { 878 this.statusCode = sc; 879 } 880 881 886 public String getServerScheme() { 887 return serverScheme; 888 } 889 890 895 public String getServerName() { 896 return serverName; 897 } 898 899 904 public int getServerPort() { 905 return serverPort; 906 } 907 908 913 public String getContextPath() { 914 return contextPath; 915 } 916 917 922 public String getScriptName() { 923 return scriptName; 924 } 925 926 931 public String getRemoteAddr() { 932 if (this.remoteAddr == null) { 933 this.remoteAddr = this.getRequest().getRemoteAddr(); 934 } 935 936 return this.remoteAddr; 937 } 938 939 944 public String getRemoteHost() { 945 if (this.remoteHost == null) { 946 this.remoteHost = this.getRequest().getRemoteHost(); 947 } 948 949 return this.remoteHost; 950 } 951 952 957 public String getUserAgent() { 958 if (this.userAgent == null) { 959 this.userAgent = this.getRequest().getHeader("User-Agent"); 960 } 961 962 return this.userAgent; 963 } 964 965 969 public void populate() { 970 user = getUserFromSession(); 971 972 if (user != null) { 973 user.setLastAccessDate(); 974 user.incrementAccessCounter(); 975 user.incrementAccessCounterForSession(); 976 } 977 } 978 979 999 public void save() { 1000 session.setAttribute(User.SESSION_KEY, new SessionBindingEventProxy(user)); 1001 session.setAttribute(AccessControlList.SESSION_KEY, (Object ) acl); 1002 } 1003 1004 1009 public String getStackTrace() { 1010 return stackTrace; 1011 } 1012 1013 1018 public Throwable getStackTraceException() { 1019 return stackTraceException; 1020 } 1021 1022 1030 public void setStackTrace(String trace, Throwable exp) { 1031 stackTrace = trace; 1032 stackTraceException = exp; 1033 } 1034 1035 1040 public Hashtable getVarDebug() { 1041 return varDebug; 1042 } 1043 1044 1048 1053 public ParameterParser getParameterParser() { 1054 return parameters; 1055 } 1056 1057 1063 public void setParameterParser(ParameterParser parser) { 1064 parameters = parser; 1065 } 1066 1067 1072 public CookieParser getCookieParser() { 1073 return cookies; 1074 } 1075 1076 1082 public void setCookieParser(CookieParser parser) { 1083 cookies = parser; 1084 } 1085 1086 1092 public void setRequest(HttpServletRequest req) { 1093 this.req = req; 1094 } 1095 1096 1102 public void setResponse(HttpServletResponse res) { 1103 this.res = res; 1104 } 1105 1106 1112 public void setSession(HttpSession sess) { 1113 this.session = sess; 1114 } 1115 1116 1122 public void setServletConfig(ServletConfig config) { 1123 this.config = config; 1124 if (config == null) { 1125 this.servletContext = null; 1126 } else { 1127 this.servletContext = config.getServletContext(); 1128 } 1129 } 1130 1131 1135 1141 public void setServerScheme(String serverScheme) { 1142 this.serverScheme = serverScheme; 1143 } 1144 1145 1151 public void setServerName(String serverName) { 1152 this.serverName = serverName; 1153 } 1154 1155 1161 public void setServerPort(int serverPort) { 1162 this.serverPort = serverPort; 1163 } 1164 1165 1171 public void setContextPath(String contextPath) { 1172 this.contextPath = contextPath; 1173 } 1174 1175 1181 public void setScriptName(String scriptName) { 1182 this.scriptName = scriptName; 1183 } 1184 1185 public void setMessage(String message) { 1186 this.message = message; 1187 } 1188 1189 public String getMessage() { 1190 return message; 1191 } 1192 1193 1198 protected static MimeTypeService getMimeTypeService() { 1199 try { 1200 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 1201 YaafiComponentService.SERVICE_NAME); 1202 return (MimeTypeService) yaafi.lookup(MimeTypeService.class.getName()); 1203 } catch (Exception e) { 1204 throw new RuntimeException ("Problem looking up localization service", e); 1205 } 1206 } 1207 1208 1213 protected static LocalizationService getLocalizationService() { 1214 try { 1215 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 1216 YaafiComponentService.SERVICE_NAME); 1217 return (LocalizationService) yaafi.lookup(LocalizationService.class.getName()); 1218 } catch (Exception e) { 1219 throw new RuntimeException ("Problem looking up localization service", e); 1220 } 1221 } 1222} | Popular Tags |