1 13 82 package org.jahia.params; 83 84 import java.io.File ; 85 import java.io.IOException ; 86 import java.net.URL ; 87 import java.util.ArrayList ; 88 import java.util.Date ; 89 import java.util.Enumeration ; 90 import java.util.HashMap ; 91 import java.util.HashSet ; 92 import java.util.Iterator ; 93 import java.util.LinkedList ; 94 import java.util.ListIterator ; 95 import java.util.Locale ; 96 import java.util.Map ; 97 import java.util.NoSuchElementException ; 98 import java.util.Properties ; 99 import java.util.Set ; 100 import java.util.StringTokenizer ; 101 import java.util.Vector ; 102 103 import javax.servlet.ServletContext ; 104 import javax.servlet.http.HttpServletRequest ; 105 import javax.servlet.http.HttpServletResponse ; 106 import javax.servlet.http.HttpSession ; 107 import javax.servlet.jsp.jstl.core.Config; 108 109 import org.apache.commons.httpclient.Header; 110 import org.apache.commons.httpclient.HttpClient; 111 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 112 import org.apache.commons.httpclient.methods.GetMethod; 113 import org.apache.commons.lang.math.NumberUtils; 114 import org.apache.log4j.Logger; 115 import org.apache.struts.Globals; 116 import org.jahia.bin.Jahia; 117 import org.jahia.content.ContentPageKey; 118 import org.jahia.engines.login.Login_Engine; 119 import org.jahia.exceptions.JahiaException; 120 import org.jahia.exceptions.JahiaForbiddenAccessException; 121 import org.jahia.exceptions.JahiaInitializationException; 122 import org.jahia.exceptions.JahiaPageNotFoundException; 123 import org.jahia.exceptions.JahiaSessionExpirationException; 124 import org.jahia.exceptions.JahiaSiteNotFoundException; 125 import org.jahia.exceptions.JahiaSiteAndPageIDMismatchException; 126 import org.jahia.pipelines.Pipeline; 127 import org.jahia.pipelines.PipelineException; 128 import org.jahia.registries.ServicesRegistry; 129 import org.jahia.services.applications.JahiaApplicationsDispatchingService; 130 import org.jahia.services.applications.ServletIncludeRequestWrapper; 131 import org.jahia.services.applications.ServletIncludeResponseWrapper; 132 import org.jahia.services.cache.CacheFactory; 133 import org.jahia.services.cache.HtmlCache; 134 import org.jahia.services.fields.ContentField; 135 import org.jahia.services.lock.LockKey; 136 import org.jahia.services.lock.LockService; 137 import org.jahia.services.pages.ContentPage; 138 import org.jahia.services.pages.JahiaPage; 139 import org.jahia.services.pages.PageProperty; 140 import org.jahia.services.sites.JahiaSite; 141 import org.jahia.services.sites.SiteLanguageMapping; 142 import org.jahia.services.sites.SiteLanguageSettings; 143 import org.jahia.services.usermanager.JahiaUser; 144 import org.jahia.services.usermanager.JahiaUserManagerService; 145 import org.jahia.services.version.EntryLoadRequest; 146 import org.jahia.services.version.StateModificationContext; 147 import org.jahia.settings.SettingsBean; 148 import org.jahia.utils.JahiaTools; 149 import org.jahia.utils.LanguageCodeConverters; 150 151 168 public class ParamBean { 169 170 private static final Logger logger = Logger.getLogger(ParamBean.class); 171 172 public static final String ENGINE_NAME_PARAMETER = "engineName"; 173 174 public static final String SITE_KEY_PARAMETER = "site"; 175 public static final String PAGE_ID_PARAMETER = "pid"; public static final String CONTAINERLIST_ID_PARAMETER = "clid"; 177 public static final String CONTAINER_ID_PARAMETER = "cid"; 178 public static final String FIELD_ID_PARAMETER = "fid"; 179 180 public static final String OPERATION_MODE_PARAMETER = "op"; 181 public static final String CACHE_MODE_PARAMETER = "cache"; 182 public static final String ENTRY_STATE_PARAMETER = "entrystate"; 183 public static final String SHOW_REVISION_DIFF_PARAMETER = "showrevdiff"; 184 public static final String VALIDATE_PARAMETER = "validate"; 185 public static final String CONTAINER_SCROLL_PREFIX_PARAMETER = "ctnscroll_"; 186 public static final String LANGUAGE_CODE = "lang"; 187 public static final String STEAL_LOCK = "stealLock"; 188 public static final String RELEASE_LOCK = "releaseLock"; 189 public static final String TEMPLATE_PARAMETER = "template"; 190 191 public static final String DEFAULT_SITE_PROPERTY = "defaultSite"; 192 193 194 public static final String DEFAULT_PAGE_ID = "1"; 195 196 197 public static final String CORE_ENGINE_NAME = "core"; 198 200 public static final int GET_METHOD = 1; 202 public static final int POST_METHOD = 2; 203 204 public static final String NORMAL = "normal"; public static final String EDIT = "edit"; public static final String DEBUG = "debug"; public static final String PREVIEW = "preview"; public static final String COMPARE = "compare"; 211 public static final String CACHE_ON = "on"; 213 public static final String CACHE_ONLYUPDATE = "onlyupdate"; public static final String CACHE_OFFONCE = "offonce"; public static final String CACHE_BYPASS = "bypass"; public static final String CACHE_OFF = "off"; 217 218 public static final String SESSION_USER = "org.jahia.usermanager.jahiauser"; 220 public static final String SESSION_SITE = "org.jahia.services.sites.jahiasite"; 221 public static final String SESSION_DEFAULT_SITE = "org.jahia.services.sites.jahiadefaultsite"; 222 public static final String SESSION_LAST_REQUESTED_PAGE_ID = "org.jahia.params.lastrequestedpageid"; 223 public static final String SESSION_LAST_ENGINE_NAME = "org.jahia.engines.lastenginename"; 224 public static final String SESSION_JAHIA_RUNNING_MODE = "org.jahia.bin.jahiarunningmode"; 225 public static final String SESSION_JAHIA_ENGINEMAP = "jahia_session_engineMap"; 226 public static final String SESSION_LOCALE = "org.jahia.services.multilang.currentlocale"; 227 228 private static HttpClient httpClient; 229 private long startTime = 0; 230 private int httpMethod = 0; 231 private String engineName = ""; 232 private int fieldID = 0; 233 private int containerID = 0; 234 private int containerListID = 0; 235 private String opMode = ""; 236 private HttpServletRequest mRealRequest; 237 private ServletIncludeRequestWrapper mRequest; 238 private HttpServletResponse mRealResponse; 239 private ServletIncludeResponseWrapper mResponse = null; 241 private ServletContext context; 242 private JahiaPage thePage; 243 private ContentPage contentPage; 244 private JahiaUser theUser = null; 245 private String userAgent = ""; 246 protected Locale currentLocale = null; 247 protected ArrayList localeList = null; 249 private String anchor = null; 255 private int siteID = -1; 256 private String siteKey = null; 257 private JahiaSite site; 258 private boolean siteResolvedByKeyOrPageId; 259 private boolean contentPageLoadedWhileTryingToFindSiteByPageID; 260 261 264 private Map customParameters = new HashMap (); 265 266 private int lastRequestedPageID = 0; 267 private boolean newPageRequest = false; private String lastEngineName = null; 269 private boolean engineHasChanged = false; private String cacheStatus = CACHE_ON; private String originalCacheStatus = cacheStatus; private Date cacheExpirationDate = null; 274 protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT; 275 276 private EntryLoadRequest substituteEntryLoadRequest = null; 277 278 private static Properties defaultParameterValues; 280 private boolean useQueryStringParameterUrl = false; 281 282 private int diffVersionID = 0; 283 284 private ArrayList pageURLKeys = new ArrayList (); 285 private long delayFromNow = -1; 286 private String responseMimeType = "text/html"; 287 288 static { 289 293 defaultParameterValues = new Properties (); 295 defaultParameterValues.setProperty(ParamBean.ENGINE_NAME_PARAMETER, ParamBean.CORE_ENGINE_NAME); 296 defaultParameterValues.setProperty(ParamBean.OPERATION_MODE_PARAMETER, ParamBean.NORMAL); 297 defaultParameterValues.setProperty(ParamBean.SITE_KEY_PARAMETER, ""); 298 defaultParameterValues.setProperty(ParamBean.CACHE_MODE_PARAMETER, ParamBean.CACHE_ON); 299 303 MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 304 httpClient = new HttpClient(connectionManager); 305 httpClient.setConnectionTimeout(Jahia.getSettings().getSiteServerNameTestConnectTimeout()); 306 } 307 308 312 ParamBean() { 313 } 315 316 344 ParamBean(HttpServletRequest request, HttpServletResponse response, ServletContext aContext, SettingsBean jSettings, 345 long aStartTime, int aHttpMethod, JahiaSite aSite, JahiaUser user, ContentPage aContentPage) 346 throws JahiaPageNotFoundException, JahiaSessionExpirationException, JahiaSiteNotFoundException, 347 JahiaException { 348 this.engineName = CORE_ENGINE_NAME; 350 this.opMode = NORMAL; 351 352 mRealRequest = request; 354 mRealResponse = response; 355 356 358 this.context = aContext; 359 this.startTime = aStartTime; 360 this.httpMethod = aHttpMethod; 361 362 this.site = aSite; 363 this.siteID = aSite.getID(); 364 this.siteKey = aSite.getSiteKey(); 365 366 entryLoadRequest = EntryLoadRequest.STAGED; 367 368 if (getRequest() != null) { 369 HttpSession session = getRequest().getSession(); 370 371 Integer lrpID = (Integer ) session.getAttribute(SESSION_LAST_REQUESTED_PAGE_ID); 373 if ((lrpID == null)) { 374 lrpID = new Integer (-1); 375 } 376 this.newPageRequest = (lrpID.intValue() != this.getPageID()); 377 this.lastRequestedPageID = lrpID.intValue(); 378 379 theUser = (JahiaUser) session.getAttribute(SESSION_USER); 382 if (theUser == null) { 383 setUserGuest(this.getSiteID()); 384 } 385 386 Enumeration userAgentValues = getRequest().getHeaders("user-agent"); 387 if (userAgentValues.hasMoreElements()) { 388 userAgent = (String ) userAgentValues.nextElement(); 390 } 391 392 Locale lastLocale = (Locale ) session.getAttribute(ParamBean.SESSION_LOCALE); 394 if (lastLocale != null) { 395 this.changeLanguage(lastLocale); 396 } 397 } else { 398 theUser = user; 399 if (theUser == null) { 400 setUserGuest(this.getSiteID()); 401 } 402 } 403 if (aContentPage != null) { 404 thePage = aContentPage.getPage(this.getEntryLoadRequest(), this.getOperationMode(), this.getUser()); 405 } 406 } 407 408 413 public ParamBean(HttpServletRequest request, HttpServletResponse response, ServletContext aContext, 414 SettingsBean jSettings, long aStartTime, int aHttpMethod) throws JahiaPageNotFoundException, 415 JahiaSessionExpirationException, JahiaSiteNotFoundException, JahiaException { 416 Jahia.setThreadParamBean(this); 417 this.engineName = CORE_ENGINE_NAME; 418 this.opMode = NORMAL; 419 mRealRequest = request; 420 mRealResponse = response; 421 this.context = aContext; 423 this.startTime = aStartTime; 424 this.httpMethod = aHttpMethod; 425 HttpSession session = getRequest().getSession(); 426 429 buildCustomParameterMapFromPathInfo(getRequest()); 430 431 setEngineNameIfAvailable(); 432 433 if (findSiteFromWhatWeHave() == false) { 434 throw new JahiaSiteNotFoundException("400 Bad Request : No site specified or site not found", 435 JahiaException.CRITICAL_SEVERITY); 436 } 437 438 if (request.getServerPort() == 80 || request.getServerPort() == 443) { 440 if (siteResolvedByKeyOrPageId) { 444 JahiaSite siteByHostName = getSiteByHostName(); 445 if (siteByHostName != null && !site.getSiteKey().equals(siteByHostName.getSiteKey())) { 446 throw new JahiaSiteAndPageIDMismatchException(site.getSiteKey(), siteByHostName.getSiteKey(), request 447 .getServerName()); 448 } 449 } 450 } 451 452 setSiteInfoFromSiteFound(); 453 454 if (contentPageLoadedWhileTryingToFindSiteByPageID == false) { 455 if (isPageRequestedByID()) { 456 setContentPageToPageWithID(); 457 } else if (isPageRequestedByKey()) { 458 setContentPageToPageWithURLKey(); 459 } else { 460 contentPage = site.getHomeContentPage(); 461 } 462 } 463 464 resolveUser(request, session); 465 466 setUserAgent(); 467 468 setFieldIDIfAvailable(); 469 470 setContainerIDIfAvailable(); 471 472 setContainerListIDIfAvailable(); 473 474 resolveOpMode(session); 475 476 resolveLocales(session); 477 478 processLockAction(response, contentPage.getID()); 479 480 String verInfo = resolveEntryState(); 481 482 resolveDiffVersionID(verInfo); 483 484 checkLocales(); 485 486 checkPageAccess(session, contentPage.getID()); 487 488 switchUserToGuestUserIfSiteChangedAndCurrentUserDoesntHaveReadAccess(); 489 490 loadJahiaPageFromContentPage(); 491 492 this.lastEngineName = (String ) session.getAttribute(SESSION_LAST_ENGINE_NAME); 493 494 this.engineHasChanged = (this.lastEngineName == null || !this.lastEngineName.equals(getEngine())); 495 496 resolveCacheStatus(); 497 498 processActivationAction(); 499 } 501 505 private void switchUserToGuestUserIfSiteChangedAndCurrentUserDoesntHaveReadAccess() throws JahiaException { 506 if (siteChanged() == false) 507 return; 508 509 if (doesUserHaveReadAccessToContentPage() == false) { 510 setUserGuest(getSiteID()); 512 } 513 } 514 515 518 private boolean doesUserHaveReadAccessToContentPage() { 519 return contentPage.checkReadAccess(getUser()); 520 } 521 522 private void setContainerListIDIfAvailable() { 523 String containerListIDStr = getParameter(CONTAINERLIST_ID_PARAMETER); 524 if (containerListIDStr == null) 525 return; 526 try { 527 containerListID = Integer.parseInt(containerListIDStr); 528 } catch (NumberFormatException nfe) { 529 logger.warn("Invalid container list ID [" + containerListIDStr + "] specified in request, ignoring..."); 530 containerListID = 0; 531 } 532 } 533 534 private void setFieldIDIfAvailable() { 535 String fieldIDStr = getParameter(FIELD_ID_PARAMETER); 536 if (fieldIDStr == null) 537 return; 538 try { 539 fieldID = Integer.parseInt(fieldIDStr); 540 } catch (NumberFormatException nfe) { 541 logger.warn("Invalid field ID [" + fieldIDStr + "] specified in request, ignoring..."); 542 fieldID = 0; 543 } 544 } 545 546 550 private void setContainerIDIfAvailable() { 551 String containerIDStr = getParameter(CONTAINER_ID_PARAMETER); 552 if (containerIDStr == null) 553 return; 554 try { 555 containerID = Integer.parseInt(containerIDStr); 556 } catch (NumberFormatException nfe) { 557 logger.warn("Invalid container ID [" + containerIDStr + "] specified in request, ignoring..."); 558 containerID = 0; 559 } 560 } 561 562 566 private void setUserAgent() { 567 Enumeration userAgentValues = getRequest().getHeaders("user-agent"); 568 if (userAgentValues.hasMoreElements()) { 569 userAgent = (String ) userAgentValues.nextElement(); 571 } 572 } 573 574 580 private void setContentPageToPageWithID() throws JahiaException { 581 int pageID = NumberUtils.stringToInt(getParameter(PAGE_ID_PARAMETER), 0); 582 contentPage = ServicesRegistry.getInstance().getJahiaPageService().lookupContentPage(pageID, true); 583 } 584 585 594 private void setContentPageToPageWithURLKey() throws JahiaException { 595 String pageURLKey = (String ) pageURLKeys.get(pageURLKeys.size() - 1); 596 int pageID = ServicesRegistry.getInstance().getJahiaPageService().getPageIDFromPageKeyAndSiteID(pageURLKey, 597 siteID); 598 contentPage = ContentPage.getPage(pageID); 599 } 600 601 605 private boolean siteChanged() { 606 JahiaSite oldSite = (JahiaSite) mRequest.getSession().getAttribute(SESSION_SITE); 607 608 if (oldSite == null) { 609 return true; 610 } else if (oldSite.getID() != site.getID()) { 611 return true; 612 } else { 613 return false; 614 } 615 } 616 617 620 private void setSiteInfoFromSiteFound() { 621 this.siteID = site.getID(); 622 this.siteKey = site.getSiteKey(); 623 mRequest.getSession().setAttribute(SESSION_SITE, site); 624 625 if (settings().isSiteIDInURL() == false) { 626 JahiaSite defaultSite = getDefaultSite(); 627 if ((defaultSite != null) && (defaultSite.getID() == site.getID())) { 628 ParamBean.defaultParameterValues.setProperty(ParamBean.SITE_KEY_PARAMETER, site.getSiteKey()); 631 } 632 } 633 } 634 635 641 private boolean findSiteFromWhatWeHave() throws JahiaException { 642 if (findSiteByItsKey()) { 643 return true; 644 } else if (isPageRequestedByID() && findSiteByPageID()) { 645 return true; 646 } else if (findSiteByHostName()) { 647 return true; 648 } else if (findSiteByRequestParam()) { 649 return true; 650 } else if (findSiteFromSession()) { 651 return true; 652 } else if (findDefaultSite()) { 653 return true; 654 } 655 656 return false; 657 } 658 659 private boolean findSiteFromSession() { 660 site = (JahiaSite) mRequest.getSession().getAttribute(SESSION_SITE); 661 if (site == null) 662 return false; 663 return true; 664 } 665 666 669 private boolean findDefaultSite() { 670 logger.debug("No site found in URL, serverName or via page ID, trying default site..."); 671 site = getDefaultSite(); 672 if (site == null) 673 return false; 674 return true; 675 } 676 677 683 private boolean isPageRequestedByID() { 684 String pageIDStr = getParameter(PAGE_ID_PARAMETER); 685 if (pageIDStr == null) 686 return false; 687 return NumberUtils.stringToInt(pageIDStr, 0) > 0; 688 } 689 690 693 private boolean isPageRequestedByKey() { 694 return isPageRequestedByID() == false && (pageURLKeys.size() > 0); 695 } 696 697 703 private boolean findSiteByPageID() throws JahiaException { 704 int pageID = NumberUtils.stringToInt(getParameter(PAGE_ID_PARAMETER), 0); 705 contentPage = ServicesRegistry.getInstance().getJahiaPageService().lookupContentPage(pageID, true); 706 if (contentPage == null) 707 return false; 708 709 contentPageLoadedWhileTryingToFindSiteByPageID = true; 710 this.site = ServicesRegistry.getInstance().getJahiaSitesService().getSite(contentPage.getJahiaID()); 711 712 if (site == null) 713 return false; 714 715 siteResolvedByKeyOrPageId = true; 716 717 return true; 718 } 719 720 727 private JahiaSite getSiteByHostName() throws JahiaException { 728 JahiaSite resolvedSite = null; 729 String serverName = getRequest().getServerName().toLowerCase(); 730 731 if (isValidServerName(serverName)) { 732 resolvedSite = ServicesRegistry.getInstance().getJahiaSitesService().getSite(getRequest().getServerName()); 733 } 734 735 return resolvedSite; 736 } 737 738 744 private boolean findSiteByHostName() throws JahiaException { 745 site = getSiteByHostName(); 746 747 return (site != null); 748 } 749 750 754 private boolean isValidServerName(String serverName) { 755 if (serverName == null) 756 return false; 757 if (serverName.equals("localhost")) 758 return false; 759 if (serverName.equals("127.0.0.1")) 760 return false; 761 return true; 762 } 763 764 770 private boolean findSiteByItsKey() throws JahiaException { 771 if (!isSiteKeyPresent()) 772 return false; 773 site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(getParameter(SITE_KEY_PARAMETER)); 774 if (site == null) 775 return false; 776 logger.debug("Found site info in parameters..."); 777 778 siteResolvedByKeyOrPageId = true; 779 780 return true; 781 } 782 783 790 private boolean findSiteByRequestParam() throws JahiaException { 791 String paramSiteKey = getRequest().getParameter("siteKey"); 792 793 if (paramSiteKey == null || paramSiteKey.length() == 0) 794 return false; 795 796 this.site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(paramSiteKey); 797 798 if (site == null) 799 return false; 800 801 logger.debug("Found site info in parameters..."); 802 803 siteResolvedByKeyOrPageId = true; 804 805 return true; 806 } 807 808 private boolean isSiteKeyPresent() { 809 return getParameter(SITE_KEY_PARAMETER) != null; 810 } 811 812 815 private void setEngineNameIfAvailable() { 816 if (getParameter(ENGINE_NAME_PARAMETER) == null) 817 return; 818 this.engineName = getParameter(ENGINE_NAME_PARAMETER); 819 } 820 821 827 public void setUserGuest(int siteId) throws JahiaSessionExpirationException, JahiaException { 828 HttpSession session = getRequest().getSession(false); 829 if (session == null) { 830 throw new JahiaSessionExpirationException(); 831 } 832 833 JahiaUserManagerService userMgr = ServicesRegistry.getInstance().getJahiaUserManagerService(); 835 theUser = userMgr.lookupUser(siteId, JahiaUserManagerService.GUEST_USERNAME); 836 session.setAttribute(SESSION_USER, theUser); 837 setOperationMode(NORMAL); 838 } 839 840 844 public void setUser(JahiaUser user) throws JahiaSessionExpirationException, JahiaException { 845 HttpSession session = getRequest().getSession(false); 846 if (session == null) { 847 throw new JahiaSessionExpirationException(); 848 } 849 850 theUser = user; 851 session.setAttribute(SESSION_USER, theUser); 852 flushLocaleListCache(); 853 this.entryLoadRequest.setLocales(getLocales()); 854 setOperationMode(NORMAL); 855 } 856 857 860 public SettingsBean settings() { 861 return Jahia.getSettings(); 862 } 863 864 public HttpServletRequest getRequest() { 865 return getRequestWrapper(); 866 } 867 868 public ServletIncludeRequestWrapper getRequestWrapper() { 869 if (mRequest == null) { 870 HttpServletRequest request = getRealRequest(); 871 if (request != null) 872 mRequest = new ServletIncludeRequestWrapper(request, this); 873 } 874 return mRequest; 875 } 876 877 public HttpServletRequest getRealRequest() { 878 return mRealRequest; 879 } 880 881 public ServletIncludeResponseWrapper getResponseWrapper() { 882 if (mResponse == null) { 883 String forceEncoding = null; 884 if (settings().isUtf8Encoding()) { 885 forceEncoding = "UTF-8"; 886 } 887 mResponse = new ServletIncludeResponseWrapper(getRealResponse(), true, forceEncoding); 888 } 889 return mResponse; 890 } 891 892 public HttpServletResponse getResponse() { 893 return getResponseWrapper(); 894 } 895 896 public HttpServletResponse getRealResponse() { 897 return mRealResponse; 898 } 899 900 public ServletContext getContext() { 901 return context; 902 } 903 904 public int getHttpMethod() { 905 return httpMethod; 906 } 907 908 public long getStartTime() { 909 return startTime; 910 } 911 912 public String getEngine() { 913 return engineName; 914 } 915 916 public String getOperationMode() { 917 return opMode; 918 } 919 920 public ContentPage getContentPage() { 921 return contentPage; 922 } 923 924 public JahiaPage getPage() { 925 return thePage; 926 } 927 928 public int getPageID() { 929 if (contentPage == null) 930 return -1; 931 return contentPage.getID(); 932 } 933 934 public int getLastRequestedPageID() { 935 return lastRequestedPageID; 936 } 937 938 public boolean newPageRequest() { 939 return newPageRequest; 940 } 941 942 public String getLastEngineName() { 943 return lastEngineName; 944 } 945 946 public boolean engineHasChanged() { 947 return engineHasChanged; 948 } 949 950 public JahiaUser getUser() { 951 return theUser; 952 } 953 954 public String getUserAgent() { 955 return userAgent; 956 } 957 958 public int getFieldID() { 959 return fieldID; 960 } 961 962 public int getContainerID() { 963 return containerID; 964 } 965 966 public int getContainerListID() { 967 return containerListID; 968 } 969 970 public int getSiteID() { 971 return siteID; 972 } 973 974 public String getSiteKey() { 975 return siteKey; 976 } 977 978 public int getJahiaID() { 979 return getSiteID(); 980 } 982 public JahiaSite getSite() { 983 return site; 984 } 985 986 public EntryLoadRequest getEntryLoadRequest() { 987 if (substituteEntryLoadRequest != null) { 988 return substituteEntryLoadRequest; 989 } else { 990 return entryLoadRequest; 991 } 992 } 993 994 public void setSubstituteEntryLoadRequest(EntryLoadRequest anEntryLoadRequest) { 995 this.substituteEntryLoadRequest = anEntryLoadRequest; 996 } 997 998 public void resetSubstituteEntryLoadRequest() { 999 this.substituteEntryLoadRequest = null; 1000 } 1001 1002 public int getDiffVersionID() { 1003 return this.diffVersionID; 1004 } 1005 1006 public boolean showRevisionDiff() { 1007 return (this.diffVersionID != 0); 1008 } 1009 1010 1036 public String getCacheStatus() { 1037 return cacheStatus; 1038 } 1039 1040 1066 public String getOriginalCacheStatus() { 1067 return originalCacheStatus; 1068 } 1069 1070 1074 public void setOperationMode(String newOperationMode) throws JahiaException { 1075 opMode = newOperationMode; 1076 EntryLoadRequest newLoadRequest = new EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, getLocales()); 1077 if (this.getSiteID() != -1) { 1079 if ((opMode == EDIT || opMode == PREVIEW || opMode == COMPARE) 1080 && (ServicesRegistry.getInstance().getJahiaVersionService().isStagingEnabled(this.getSiteID()))) { 1081 newLoadRequest = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, getLocales()); 1082 } 1085 } 1086 if (opMode == COMPARE) { 1087 newLoadRequest.setWithMarkedForDeletion(true); 1088 } 1089 if (this.entryLoadRequest.getWorkflowState() > 0) { 1090 this.entryLoadRequest = newLoadRequest; 1091 } 1092 } 1093 1094 1106 public boolean setCacheStatus(String newCacheStatus) { 1107 if (ParamBean.CACHE_ON.equals(newCacheStatus) || ParamBean.CACHE_ONLYUPDATE.equals(newCacheStatus) 1108 || ParamBean.CACHE_OFFONCE.equals(newCacheStatus) || ParamBean.CACHE_BYPASS.equals(newCacheStatus) 1109 || ParamBean.CACHE_OFF.equals(newCacheStatus)) { 1110 this.cacheStatus = newCacheStatus; 1111 return true; 1112 } else { 1113 return false; 1114 } 1115 } 1116 1117 1124 public boolean userIsAdmin() { 1125 1126 return getUser().isAdminMember(getSiteID()); 1127 1128 } 1129 1130 1146 1147 public int getJahiaRunningMode() throws JahiaSessionExpirationException { 1148 HttpSession session = getRequest().getSession(false); 1149 if (session == null) { 1150 throw new JahiaSessionExpirationException(); 1151 } 1152 Integer I = (Integer ) session.getAttribute(SESSION_JAHIA_RUNNING_MODE); 1153 if (I == null) 1154 return -1; 1155 return I.intValue(); 1156 } 1157 1158 1171 1172 public final boolean isInAdminMode() throws JahiaSessionExpirationException { 1173 HttpSession session = getRequest().getSession(false); 1174 if (session == null) { 1175 throw new JahiaSessionExpirationException(); 1176 } 1177 Integer I = (Integer ) session.getAttribute(SESSION_JAHIA_RUNNING_MODE); 1178 if (I == null) 1179 return false; 1180 return (I.intValue() == Jahia.ADMIN_MODE); 1181 } 1182 1183 1192 public String getSessionID() throws JahiaSessionExpirationException { 1193 HttpSession session = getRequest().getSession(false); 1194 if (session == null) { 1195 throw new JahiaSessionExpirationException(); 1196 } 1197 return session.getId(); 1198 } 1199 1200 1209 1210 public HttpSession getSession() throws JahiaSessionExpirationException { 1211 HttpSession session = getRequest().getSession(false); 1212 if (session == null) { 1213 throw new JahiaSessionExpirationException(); 1214 } 1215 return session; 1216 } 1217 1218 1227 1228 public HttpSession getSession(boolean create) throws JahiaSessionExpirationException { 1229 HttpSession session = getRequest().getSession(create); 1230 if (session == null) 1231 throw new JahiaSessionExpirationException(); 1232 return session; 1233 } 1234 1235 1240 public void invalidateSession() throws JahiaSessionExpirationException { 1241 HttpSession session = getRequest().getSession(false); 1242 if (session == null) { 1243 throw new JahiaSessionExpirationException(); 1244 } 1245 1246 session.invalidate(); 1247 session = getRequest().getSession(true); 1248 } 1249 1250 1257 public void purgeSession() throws JahiaSessionExpirationException { 1258 HttpSession session = getRequest().getSession(false); 1259 if (session == null) { 1260 throw new JahiaSessionExpirationException(); 1261 } 1262 1263 logger.debug("Purging session of all objects..."); 1264 1265 Enumeration attributeNamesEnum = session.getAttributeNames(); 1268 Vector attributeNames = new Vector (); 1269 while (attributeNamesEnum.hasMoreElements()) { 1270 String curAttributeName = (String ) attributeNamesEnum.nextElement(); 1271 attributeNames.add(curAttributeName); 1272 } 1273 1274 attributeNamesEnum = attributeNames.elements(); 1275 while (attributeNamesEnum.hasMoreElements()) { 1276 String curAttributeName = (String ) attributeNamesEnum.nextElement(); 1277 1281 session.removeAttribute(curAttributeName); 1282 } 1283 1284 session.setAttribute(ParamBean.SESSION_LOCALE, this.currentLocale); 1286 session.setAttribute(Globals.LOCALE_KEY, this.currentLocale); 1288 Config.set(session, Config.FMT_LOCALE, this.currentLocale); 1289 1290 } 1291 1292 1299 private void testLocaleList(ArrayList locales, boolean isMixLanguageActive) throws JahiaException { 1300 if (locales != null) { 1301 if (locales.size() == 1 && locales.get(0).toString().equals(ContentField.SHARED_LANGUAGE) 1302 && this.getSite().getLanguageSettings().size() == 0) { 1303 SettingsBean settings = Jahia.getSettings(); 1306 if (settings != null) { 1307 logger.debug("Using jahia.properties default language code : " + settings.getDefaultLanguageCode()); 1308 locales.add(LanguageCodeConverters.languageCodeToLocale(settings.getDefaultLanguageCode())); 1309 } else { 1310 logger 1311 .warn("Warning : Couldn't find default language settings in jahia.properties, using english as default locale"); 1312 locales.add(Locale.ENGLISH); 1313 } 1314 } 1315 if (locales.size() == 0 || !locales.get(0).toString().equals(ContentField.SHARED_LANGUAGE)) { 1316 locales.add(0, new Locale (ContentField.SHARED_LANGUAGE, "")); 1317 } 1318 if (!isMixLanguageActive) { 1319 if (locales.size() > 2) { 1323 ArrayList newLocaleList = new ArrayList (); 1324 newLocaleList.add(locales.get(0)); 1325 newLocaleList.add(locales.get(1)); 1326 locales.clear(); 1327 locales.addAll(newLocaleList); 1328 } 1329 } 1330 locales = insertLocaleMappings(locales); 1333 } 1334 } 1335 1336 1348 private ArrayList insertLocaleMappings(ArrayList source) { 1349 ArrayList result = new ArrayList (); 1350 if (site == null) { 1351 logger.debug("Site unknown, can't add mappings..."); 1352 return source; 1353 } 1354 Vector languageMappings; 1355 try { 1356 languageMappings = site.getLanguageMappings(); 1357 } catch (JahiaException je) { 1358 logger.debug("Error while retrieving site mappings, returning unmodified locale list.", je); 1359 return source; 1360 } 1361 1362 Map languageMappingsHashtable = new HashMap (); 1365 Enumeration mappingsEnum = languageMappings.elements(); 1366 while (mappingsEnum.hasMoreElements()) { 1367 SiteLanguageMapping curMapping = (SiteLanguageMapping) mappingsEnum.nextElement(); 1368 languageMappingsHashtable.put(curMapping.getFromLanguageCode(), curMapping); 1369 } 1370 1371 Set previousLocales = new HashSet (); 1376 ListIterator sourceIter = source.listIterator(); 1377 while (sourceIter.hasNext()) { 1378 Locale curSourceLocale = (Locale ) sourceIter.next(); 1379 result.add(curSourceLocale); 1381 previousLocales.add(curSourceLocale.toString()); 1382 if (curSourceLocale.getCountry().equals("")) { 1383 if (languageMappingsHashtable.containsKey(curSourceLocale.getLanguage())) { 1385 SiteLanguageMapping curMapping = (SiteLanguageMapping) languageMappingsHashtable 1386 .get(curSourceLocale.getLanguage()); 1387 Locale targetLocale = LanguageCodeConverters.languageCodeToLocale(curMapping.getToLanguageCode()); 1388 if (sourceIter.hasNext()) { 1392 Locale nextSourceLocale = (Locale ) sourceIter.next(); 1393 if (!nextSourceLocale.equals(targetLocale) 1394 && !previousLocales.contains(targetLocale.toString())) { 1395 result.add(targetLocale); 1396 previousLocales.add(targetLocale.toString()); 1397 } else { 1398 logger.debug("Not inserting locale " + targetLocale.toString() 1399 + " since it already exists in locale list..."); 1400 } 1401 sourceIter.previous(); 1403 } else { 1404 if (!previousLocales.contains(targetLocale.toString())) { 1407 result.add(targetLocale); 1408 previousLocales.add(targetLocale.toString()); 1409 } else { 1410 logger.debug("Not inserting locale " + targetLocale.toString() 1411 + " since it already exists in locale list..."); 1412 } 1413 } 1414 } 1415 } 1416 } 1417 1418 return result; 1419 } 1420 1421 1427 public void changeLanguage(Locale locale) throws JahiaSessionExpirationException, JahiaException { 1428 if (locale == null) { 1429 return; 1430 } 1431 1432 this.localeList = null; 1434 this.getSession().setAttribute(SESSION_LOCALE, locale); 1435 this.entryLoadRequest = new EntryLoadRequest(this.entryLoadRequest.getWorkflowState(), this.entryLoadRequest 1436 .getVersionID(), this.getLocales()); 1437 this.resetSubstituteEntryLoadRequest(); 1438 this.setParameter(ParamBean.LANGUAGE_CODE, this.currentLocale.toString()); 1439 1440 this.getSession().setAttribute(Globals.LOCALE_KEY, locale); 1442 Config.set(this.getSession(), Config.FMT_LOCALE, locale); 1443 1444 } 1445 1446 1465 public ArrayList getLocales() throws JahiaException { 1466 return getLocales(getSite().isMixLanguagesActive()); 1467 } 1468 1469 1488 public ArrayList getLocales(boolean allowMixLanguages) throws JahiaException { 1489 1490 if (localeList != null) { 1493 return localeList; 1494 } 1495 1496 localeList = new ArrayList (); 1497 1498 ArrayList siteLanguages = new ArrayList (); 1499 try { 1500 siteLanguages = this.getSite().getLanguageSettingsAsLocales(true); 1501 } catch (Throwable t) { 1502 logger.debug("getLanguageSettingsAsLocales failed", t); 1503 } 1504 1505 if (getRequest() != null) { 1506 try { 1508 currentLocale = (Locale ) this.getSession().getAttribute(SESSION_LOCALE); 1509 if (currentLocale != null && siteLanguages.contains(currentLocale)) { 1510 localeList.add(currentLocale); 1511 } 1512 } catch (JahiaSessionExpirationException jsee) { 1513 logger.debug("Session expiration, cannot add language setting from session", jsee); 1514 } 1515 1516 1535 1536 Enumeration browserLocales = getRequest().getLocales(); 1538 while (browserLocales.hasMoreElements()) { 1539 Locale curLocale = (Locale ) browserLocales.nextElement(); 1540 if (siteLanguages.contains(curLocale)) { 1541 localeList.add(curLocale); 1542 } else if (curLocale.getCountry().length() != 0) { 1543 Locale langOnlyLocale = new Locale (curLocale.getLanguage()); 1544 if (siteLanguages.contains(langOnlyLocale)) { 1545 localeList.add(langOnlyLocale); 1546 } 1547 } 1548 } 1549 } 1550 try { 1552 Vector siteLanguageSettings = this.getSite().getLanguageSettings(); 1553 if (siteLanguageSettings != null) { 1554 boolean firstSiteActiveLanguage = true; 1555 for (int i = 0; i < siteLanguageSettings.size(); i++) { 1556 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLanguageSettings.elementAt(i); 1557 if (curSetting.isActivated()) { 1558 Locale tempLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode()); 1559 localeList.add(tempLocale); 1560 if (firstSiteActiveLanguage) { 1561 ParamBean.defaultParameterValues.setProperty(ParamBean.LANGUAGE_CODE, curSetting.getCode()); 1562 firstSiteActiveLanguage = false; 1563 } 1564 } else { 1565 logger.debug("Not adding language " + curSetting.getCode() 1566 + " because it is currently deactivated."); 1567 } 1568 } 1569 } 1570 } catch (JahiaException je) { 1571 logger.debug("Cannot retrieve site language settings", je); 1572 } 1573 1574 testLocaleList(localeList, allowMixLanguages); 1575 return localeList; 1576 } 1577 1578 1593 public Locale getLocale() { 1594 1595 if (this.currentLocale != null) { 1598 return currentLocale; 1600 } 1601 1602 try { 1605 ArrayList locales = getLocales(); 1606 if (locales.size() >= 2) { 1607 currentLocale = (Locale ) locales.get(1); 1608 } 1609 } catch (Throwable t) { 1610 t.printStackTrace(); 1611 } 1612 if (currentLocale == null) { 1613 currentLocale = Locale.getDefault(); 1614 } 1615 return currentLocale; 1616 } 1617 1618 1624 public void changePage(ContentPage page) throws JahiaException { 1625 if (page == null) 1626 return; 1627 if (contentPage == null) { 1628 lastRequestedPageID = -1; 1629 newPageRequest = true; 1630 } else if (contentPage.getID() != page.getID()) { 1631 lastRequestedPageID = contentPage.getID(); 1632 newPageRequest = true; 1633 } 1634 contentPage = page; 1635 thePage = contentPage.getPage(getEntryLoadRequest(), getOperationMode(), getUser()); 1636 } 1637 1638 public void flushLocaleListCache() { 1639 localeList = null; 1640 } 1641 1642 1648 public String getParameter(String sParameter) { 1649 1650 if (customParameters.get(sParameter) != null) { 1651 return (String ) customParameters.get(sParameter); 1652 } else { 1653 return getRequest().getParameter(sParameter); 1654 } 1655 } 1656 1657 1669 public void setParameter(String parameterName, String parameterValue) { 1670 customParameters.put(parameterName, parameterValue); 1671 } 1672 1673 1689 private void buildCustomParameterMapFromPathInfo(HttpServletRequest request) { 1690 String pathInfo = request.getPathInfo(); 1692 1693 if (pathInfo != null) { 1694 1695 if (pathInfo.endsWith(".html")) { 1696 int lastSlash = pathInfo.lastIndexOf("/"); 1698 if (lastSlash != -1) { 1699 String fakeStaticName = pathInfo.substring(lastSlash + 1); 1700 pathInfo = pathInfo.substring(0, lastSlash); 1701 logger.debug("Removed fake static ending. pathInfo=[" + pathInfo + "] fakeEnding=[" 1702 + fakeStaticName + "]"); 1703 } 1704 } 1705 1706 try { 1707 StringTokenizer st = new StringTokenizer (pathInfo, "/"); 1708 boolean inPageResolving = true; 1709 while (st.hasMoreTokens()) { 1710 String token = st.nextToken(); 1711 if (ParamBean.isReservedKeyword(token)) { 1712 inPageResolving = false; 1713 customParameters.put(token, st.nextToken()); 1714 } else if (inPageResolving) { 1715 pageURLKeys.add(token); 1716 } 1717 } 1718 } catch (NoSuchElementException nee) { 1719 } 1721 } 1722 1723 if (isMultipartRequest(request)) { 1726 Map queryStringParams = new HashMap (); 1727 ServletIncludeRequestWrapper.parseStringParameters(queryStringParams, request.getQueryString(), request 1728 .getCharacterEncoding(), true); 1729 customParameters.putAll(queryStringParams); 1730 } 1731 } 1732 1733 1740 public void setUseQueryStringParameterUrl(boolean val) { 1741 this.useQueryStringParameterUrl = val; 1742 } 1743 1744 1752 public boolean useQueryStringParameterUrl() { 1753 return this.useQueryStringParameterUrl; 1754 } 1755 1756 1763 public static JahiaSite getDefaultSite() { 1764 1765 1767 JahiaSite site = null; 1768 String siteKey = null; 1769 1770 SettingsBean settings = Jahia.getSettings(); 1772 if (settings != null) { 1773 siteKey = settings.getDefaultSite(); 1774 if (siteKey == null || siteKey.trim().equals("")) { 1775 return null; 1776 } 1777 } 1778 1779 if (siteKey != null) { 1780 try { 1781 site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(siteKey); 1782 } catch (JahiaException je) { 1783 return null; 1784 } 1785 } 1786 1787 return site; 1788 } 1789 1790 1802 private String condAppendURL(String parameterName, String parameterValue) { 1803 String defaultValue = defaultParameterValues.getProperty(parameterName); 1804 String result = appendParam(parameterName, parameterValue); 1805 1806 if (defaultValue == null) { 1807 return result.toString(); 1808 } else { 1809 if (defaultValue.equals(parameterValue)) { 1810 return ""; 1811 } else { 1812 return result.toString(); 1813 } 1814 } 1815 } 1816 1817 private String appendParam(String parameterName, String parameterValue) { 1818 StringBuffer result = new StringBuffer (); 1819 if (!this.useQueryStringParameterUrl()) { 1820 result.append("/"); 1821 } 1822 1823 result.append(parameterName); 1824 if (!this.useQueryStringParameterUrl()) { 1825 result.append("/"); 1826 } else { 1827 result.append("="); 1828 } 1829 result.append(parameterValue); 1830 return result.toString(); 1831 } 1832 1833 1839 1848 public String composeUrl(String params) throws JahiaException { 1849 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1850 1851 if (contentPage != null) { 1852 theUrl.append(getPageURLKeyPart(contentPage.getID())); 1853 } 1854 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 1855 theUrl.append(getSiteURLPart(site.getSiteKey())); 1856 theUrl.append(getOpModeURLPart(opMode)); 1857 theUrl.append(getCacheModeURLPart(cacheStatus)); 1858 if (contentPage != null) 1860 theUrl.append(getPageURLPart(contentPage.getID())); 1861 1862 appendParams(theUrl, params); 1863 1864 appendAnchor(theUrl); 1865 1866 return encodeURL(theUrl.toString()); 1867 } 1869 1880 public String composeUrl(String pageUrlKey, String params) throws JahiaException { 1881 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1882 1883 theUrl.append("/" + pageUrlKey); 1884 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 1885 theUrl.append(getSiteURLPart(site.getSiteKey())); 1886 theUrl.append(getOpModeURLPart(opMode)); 1887 theUrl.append(getCacheModeURLPart(cacheStatus)); 1888 1890 appendParams(theUrl, params); 1891 1892 appendAnchor(theUrl); 1893 1894 return encodeURL(theUrl.toString()); 1895 } 1897 1909 public String composePageUrl(int pageID, String languageCode) throws JahiaException { 1910 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1911 theUrl.append(getPageURLKeyPart(pageID)); 1912 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 1913 theUrl.append(getSiteURLPart(site.getSiteKey())); 1914 theUrl.append(getOpModeURLPart(opMode)); 1915 theUrl.append(getCacheModeURLPart(cacheStatus)); 1916 if (languageCode != null) { 1917 theUrl.append(condAppendURL(LANGUAGE_CODE, languageCode)); 1918 } 1919 theUrl.append(getPageURLPart(pageID)); 1920 try { 1921 return encodeURL(theUrl.toString()); 1922 } catch (NullPointerException npe) { 1923 return theUrl.toString(); 1924 } 1925 } 1926 1927 1930 1931 public String composePageUrl(String pageUrlKey, String languageCode) throws JahiaException { 1932 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1933 theUrl.append("/" + pageUrlKey); 1934 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 1935 theUrl.append(getSiteURLPart(site.getSiteKey())); 1936 theUrl.append(getOpModeURLPart(opMode)); 1937 theUrl.append(getCacheModeURLPart(cacheStatus)); 1938 if (languageCode != null) { 1939 theUrl.append(condAppendURL(LANGUAGE_CODE, languageCode)); 1940 } 1941 try { 1942 return encodeURL(theUrl.toString()); 1943 } catch (NullPointerException npe) { 1944 return theUrl.toString(); 1945 } 1946 } 1947 1948 1954 public String composePageUrl(int pageID) throws JahiaException { 1955 return composePageUrl(pageID, this.getLocale().toString()); 1956 } 1957 1958 1962 1965 public String composeEngineUrl(String theEngineName) throws JahiaException { 1966 1967 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1968 1969 if (contentPage != null) { 1970 theUrl.append(getPageURLKeyPart(contentPage.getID())); 1971 } 1972 if (theEngineName != null) { 1973 theUrl.append(getEngineURLPart(theEngineName)); 1974 } 1975 1976 theUrl.append(getSiteURLPart()); 1977 theUrl.append(getOpModeURLPart(opMode)); 1978 if (contentPage != null) 1979 theUrl.append(getPageURLPart(contentPage.getID())); 1980 1981 appendAnchor(theUrl); 1982 1983 return encodeURL(theUrl.toString()); 1984 } 1986 1991 1994 public String composeEngineUrl(String theEngineName, String params) throws JahiaException { 1995 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 1998 if (contentPage != null) { 2000 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2001 } 2002 if (theEngineName != null) { 2003 theUrl.append(getEngineURLPart(theEngineName)); 2004 } 2005 2007 theUrl.append(getSiteURLPart()); 2008 theUrl.append(getOpModeURLPart(opMode)); 2009 if (contentPage != null) 2010 theUrl.append(getPageURLPart(contentPage.getID())); 2011 2012 appendParams(theUrl, params); 2013 2014 appendAnchor(theUrl); 2015 2016 return encodeURL(theUrl.toString()); 2017 } 2019 2032 public String composeStrutsUrl(String strutsAction, String params) throws JahiaException { 2033 return composeStrutsUrl(strutsAction, new Properties (), params); 2034 } 2035 2036 2050 public String composeStrutsUrl(String strutsAction, Properties extraJahiaParams, String params) 2051 throws JahiaException { 2052 if (extraJahiaParams == null) { 2053 extraJahiaParams = new Properties (); 2054 } 2055 2056 this.setUseQueryStringParameterUrl(true); 2057 2058 StringBuffer theUrl = new StringBuffer (); 2059 2060 theUrl.append(this.getRequest().getContextPath()); 2067 if (!strutsAction.startsWith("/")) { 2069 theUrl.append("/"); 2070 } 2071 theUrl.append(strutsAction); 2072 2073 if (!strutsAction.endsWith(".do")) 2074 theUrl.append(".do"); 2075 2076 String paramValue = getSiteURLPart(); 2077 String paramSep = "?"; 2078 if (paramValue != null && !paramValue.trim().equals("")) { 2079 if (!paramValue.startsWith("/")) { 2080 theUrl.append(paramSep); 2081 theUrl.append(paramValue); 2082 paramSep = "&"; 2083 } 2084 } 2085 paramValue = getOpModeURLPart(opMode); 2086 if (paramValue != null && !paramValue.trim().equals("")) { 2087 theUrl.append(paramSep); 2088 theUrl.append(paramValue); 2089 paramSep = "&"; 2090 } 2091 if (contentPage != null) { 2092 paramValue = getPageURLPart(contentPage.getID()); 2093 if (paramValue != null && !paramValue.trim().equals("")) { 2094 theUrl.append(paramSep); 2095 theUrl.append(paramValue); 2096 paramSep = "&"; 2097 } 2098 } 2099 Enumeration propertyNames = extraJahiaParams.propertyNames(); 2100 while (propertyNames.hasMoreElements()) { 2101 String propertyName = (String ) propertyNames.nextElement(); 2102 String propertyValue = extraJahiaParams.getProperty(propertyName); 2103 paramValue = condAppendURL(propertyName, propertyValue); 2104 if (paramValue != null && !paramValue.trim().equals("")) { 2105 theUrl.append(paramSep); 2106 theUrl.append(paramValue); 2107 paramSep = "&"; 2108 } 2109 } 2110 2111 appendParams(theUrl, params); 2112 2113 appendAnchor(theUrl); 2114 2115 this.setUseQueryStringParameterUrl(false); 2116 2117 String val = encodeURL(theUrl.toString()); 2118 return val; 2119 } 2120 2121 2135 public String composeEngineUrl(String engine, Properties extraJahiaParams, String params) throws JahiaException { 2136 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2139 if (contentPage != null) { 2141 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2142 } 2143 if (engine != null) { 2144 theUrl.append(getEngineURLPart(engine)); 2145 } 2146 2148 theUrl.append(getSiteURLPart()); 2149 theUrl.append(getOpModeURLPart(opMode)); 2150 if ((contentPage != null) && (!extraJahiaParams.containsKey(PAGE_ID_PARAMETER))) { 2151 theUrl.append(getPageURLPart(contentPage.getID())); 2152 } 2153 2154 Enumeration propertyNames = extraJahiaParams.propertyNames(); 2155 while (propertyNames.hasMoreElements()) { 2156 String propertyName = (String ) propertyNames.nextElement(); 2157 String propertyValue = extraJahiaParams.getProperty(propertyName); 2158 theUrl.append(condAppendURL(propertyName, propertyValue)); 2159 } 2160 2161 appendParams(theUrl, params); 2162 2163 appendAnchor(theUrl); 2164 2165 return encodeURL(theUrl.toString()); 2166 } 2168 2171 public String composeEngineUrl(String theEngineName, String params, int fieldId) throws JahiaException { 2172 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2175 if (contentPage != null) { 2176 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2177 } 2178 if (theEngineName != null) { 2180 theUrl.append(getEngineURLPart(theEngineName)); 2181 } 2182 2184 theUrl.append(getSiteURLPart()); 2185 theUrl.append(getOpModeURLPart(opMode)); 2186 if (contentPage != null) 2187 theUrl.append(getPageURLPart(contentPage.getID())); 2188 theUrl.append(getFieldURLPart(fieldId)); 2189 2190 appendParams(theUrl, params); 2191 2192 appendAnchor(theUrl); 2193 2194 return encodeURL(theUrl.toString()); 2195 } 2197 2202 2205 public String composeOperationUrl(String operationMode, String params) throws JahiaException { 2206 StringBuffer theUrl = new StringBuffer (getJahiaCoreHttpPath()); 2207 2208 if (!NORMAL.equals(operationMode) && !canEditCurrentPage()) { 2210 operationMode = NORMAL; 2211 } 2212 if (!EDIT.equals(operationMode) && contentPage != null) { 2213 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2214 } 2215 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 2216 theUrl.append(getSiteURLPart()); 2217 if (operationMode != null) { 2218 theUrl.append(getOpModeURLPart(operationMode)); 2219 } 2220 if (contentPage != null) { 2222 theUrl.append(getPageURLPartWithOperationMode(operationMode, contentPage.getID())); 2223 } 2224 2225 appendParams(theUrl, params); 2226 2227 return encodeURL(theUrl.toString()); 2228 } 2230 2239 public String composeRevDifferenceUrl(int revisionDiffID, String operationMode, String params) 2240 throws JahiaException { 2241 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2242 2243 if (contentPage != null) { 2244 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2245 } 2246 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 2247 theUrl.append(getSiteURLPart()); 2248 if (operationMode != null) { 2249 theUrl.append(getOpModeURLPart(ParamBean.COMPARE)); 2250 } 2251 theUrl.append(getCacheModeURLPart(ParamBean.CACHE_OFFONCE)); 2252 if (contentPage != null) { 2253 theUrl.append(getPageURLPart(contentPage.getID())); 2254 if (revisionDiffID == 0) { 2255 theUrl.append(this.getEntryStateURLPart("a")); 2256 } else { 2257 theUrl.append(this.getEntryStateURLPart("s")); 2258 } 2259 theUrl.append(this.getShowRevisionDiffURLPart(revisionDiffID)); 2260 } 2261 appendParams(theUrl, params); 2262 return encodeURL(theUrl.toString()); 2263 } 2265 2269 public String composeLanguageURL(String code) throws JahiaException { 2270 2271 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2272 if (contentPage != null) { 2273 theUrl.append(getPageURLKeyPart(contentPage.getID())); 2274 } 2275 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 2276 theUrl.append(getSiteURLPart()); 2277 theUrl.append(getOpModeURLPart(this.opMode)); 2278 theUrl.append(appendParam(LANGUAGE_CODE, code)); 2279 if (contentPage != null) 2280 theUrl.append(getPageURLPart(contentPage.getID())); 2281 2282 if ( this.getRequest() != null ){ 2283 String queryString = this.getRequest().getQueryString(); 2284 if ( queryString != null && !"".equals(queryString) ){ 2285 theUrl.append("?").append(queryString); 2286 } 2287 } 2288 return encodeURL(theUrl.toString()); 2289 } 2290 2291 public String composeStealLockURL(LockKey lockKey) throws JahiaException { 2293 StringBuffer theURL = new StringBuffer (); 2294 theURL.append(getRequest().getRequestURI()); 2295 theURL.append("/" + ParamBean.CACHE_MODE_PARAMETER + "/" + ParamBean.CACHE_OFF); 2296 theURL.append("/" + ParamBean.STEAL_LOCK + "/" + lockKey.toString()); 2297 theURL.append("?"); 2298 theURL.append(getRequest().getQueryString()); 2299 return encodeURL(theURL.toString()); 2300 } 2301 2302 public String composeReleaseLockURL(LockKey lockKey) throws JahiaException { 2303 StringBuffer theURL = new StringBuffer (); 2304 theURL.append(getRequest().getRequestURI()); 2305 theURL.append("/" + ParamBean.CACHE_MODE_PARAMETER + "/" + ParamBean.CACHE_OFF); 2306 theURL.append("/" + ParamBean.RELEASE_LOCK + "/" + lockKey.toString()); 2307 theURL.append("?"); 2308 theURL.append(getRequest().getQueryString()); 2309 return encodeURL(theURL.toString()); 2310 } 2311 2312 2314 2320 public String composeSiteUrl(JahiaSite jahiaSite) { 2321 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2322 2323 theUrl.append(getPageURLKeyPart(jahiaSite.getHomePageID())); 2324 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 2325 theUrl.append(getSiteURLPart(jahiaSite.getSiteKey())); 2326 theUrl.append(getOpModeURLPart(ParamBean.NORMAL)); 2327 theUrl.append(getPageURLPart(jahiaSite.getHomePageID())); 2328 2329 return encodeURL(theUrl.toString()); 2330 } 2331 2332 2338 public String composeSiteUrl() { 2339 StringBuffer theUrl = new StringBuffer (this.getJahiaCoreHttpPath()); 2340 2341 theUrl.append(getPageURLKeyPart(this.site.getHomePageID())); 2342 theUrl.append(getEngineURLPart(CORE_ENGINE_NAME)); 2343 theUrl.append(getSiteURLPart(this.site.getSiteKey())); 2344 theUrl.append(getOpModeURLPart(ParamBean.NORMAL)); 2345 theUrl.append(getPageURLPart(this.site.getHomePageID())); 2346 2347 return encodeURL(theUrl.toString()); 2348 } 2349 2350 2360 protected void appendParams(StringBuffer theUrl, String params) { 2361 2362 if (params != null && (params.length() > 0)) { 2363 if (theUrl.toString().indexOf("?") == -1) { 2364 if (params.startsWith("&")) { 2365 params = "?" + params.substring(1, params.length()); 2366 } else if (!params.startsWith("?")) { 2367 if (!params.startsWith("/")) { 2368 params = "?" + params; 2369 } 2370 } 2371 } else { 2372 if (!params.startsWith("&")) { 2373 if (!params.startsWith("/")) { 2374 params = "&" + params; 2375 } 2376 } 2377 } 2378 theUrl.append(params); 2379 } else { 2380 if (theUrl.toString().indexOf("?") == -1) { 2381 theUrl.append("?matrix="); 2382 } else { 2383 theUrl.append("&matrix="); 2384 } 2385 theUrl.append(String.valueOf(System.currentTimeMillis())); 2386 } 2387 2388 } 2389 2390 2398 protected void appendAnchor(StringBuffer theUrl) { 2399 2400 if (this.anchor != null) { 2401 theUrl.append("#"); 2402 theUrl.append(anchor); 2403 2404 this.anchor = null; 2406 } 2407 } 2408 2409 2416 public void setAnchor(String val) { 2417 this.anchor = val; 2418 } 2419 2420 2423 public String getAnchor() { 2424 return this.anchor; 2425 } 2426 2427 2434 public String getJahiaFilesTemplatesDiskPath() { 2435 2436 StringBuffer buff = new StringBuffer (settings().getJahiaFilesTemplatesDiskPath()); 2437 buff.append(File.separator); 2438 buff.append("site_"); 2439 buff.append(getSite().getID()); 2440 File f = new File (buff.toString()); 2442 if (!f.isDirectory()) { 2443 f.mkdirs(); 2444 } 2445 return f.getAbsolutePath(); 2446 2447 } 2449 2458 public String getGeneratedOutput() throws IOException { 2459 return getResponseWrapper().getStringBuffer(); 2460 } 2461 2462 2468 public String getRedirectLocation() { 2469 return getResponseWrapper().getRedirectLocation(); 2470 } 2471 2472 2479 public String getContentType() { 2480 return getResponseWrapper().getContentType(); 2481 } 2482 2483 2493 public static boolean isMultipartRequest(HttpServletRequest req) { 2494 String contentType = req.getHeader("Content-Type"); 2495 2496 if (contentType != null && contentType.indexOf("multipart/form-data") >= 0) { 2497 return true; 2498 } 2499 2500 return false; 2501 } 2502 2503 2512 public ArrayList getPageURLKeys() { 2513 return pageURLKeys; 2514 } 2515 2516 protected final String getEngineURLPart(String theEngineName) { 2520 return condAppendURL(ENGINE_NAME_PARAMETER, theEngineName); 2521 } 2522 2523 protected final String getSiteURLPart(String val) { 2526 return condAppendURL(SITE_KEY_PARAMETER, val); 2527 2528 } 2529 2530 protected final String getSiteURLPart() { 2533 return condAppendURL(SITE_KEY_PARAMETER, getSite().getSiteKey()); 2534 } 2535 2536 protected final String getPageURLPart(int id) { 2540 boolean mustRender = true; 2541 2542 if (!EDIT.equals(opMode)) { 2543 try { 2544 PageProperty pageProperty = ContentPage.getPage(id).getPageLocalProperty( 2545 PageProperty.PAGE_URL_KEY_PROPNAME); 2546 if (pageProperty != null) { 2547 mustRender = false; 2548 } 2549 } catch (JahiaException je) { 2550 logger.error("Error while testing existing of page URL key for page " + id, je); 2551 } 2552 } 2553 2554 if (mustRender) { 2555 return condAppendURL(PAGE_ID_PARAMETER, Integer.toString(id)); 2556 } else { 2557 return ""; 2558 } 2559 } 2560 2561 protected final String getPageURLPartWithOperationMode(String operationMode, int id) { 2562 boolean mustRender = true; 2563 2564 if (operationMode.equals(EDIT) == false) { 2565 try { 2566 PageProperty pageProperty = ContentPage.getPage(id).getPageLocalProperty( 2567 PageProperty.PAGE_URL_KEY_PROPNAME); 2568 if (pageProperty != null) { 2569 mustRender = false; 2570 } 2571 } catch (JahiaException je) { 2572 logger.error("Error while testing existing of page URL key for page " + id, je); 2573 } 2574 } 2575 2576 if (mustRender) { 2577 return condAppendURL(PAGE_ID_PARAMETER, Integer.toString(id)); 2578 } else { 2579 return ""; 2580 } 2581 } 2582 2583 protected final String getPageURLKeyPart(int id) { 2584 StringBuffer pageURLKeysPartBuf = new StringBuffer (); 2585 2586 LinkedList urlKeys = new LinkedList (); 2588 try { 2589 ContentPage curContentPage = ContentPage.getPage(id); 2590 Enumeration pagePathEnum = curContentPage.getContentPagePath(this); 2591 while (pagePathEnum.hasMoreElements()) { 2592 ContentPage curParentPage = (ContentPage) pagePathEnum.nextElement(); 2593 PageProperty urlKeyProp = curParentPage.getPageLocalProperty(PageProperty.PAGE_URL_KEY_PROPNAME); 2594 if (urlKeyProp != null) { 2595 urlKeys.add(urlKeyProp.getValue()); 2596 } 2597 } 2598 if (urlKeys.size() > 0) { 2599 Iterator urlKeyIter = urlKeys.iterator(); 2600 while (urlKeyIter.hasNext()) { 2601 String curURLKey = (String ) urlKeyIter.next(); 2602 pageURLKeysPartBuf.append("/"); 2603 pageURLKeysPartBuf.append(curURLKey); 2604 } 2605 } 2606 } catch (JahiaException je) { 2607 logger.error("Error while retrieving page path for page " + id, je); 2608 } 2609 2610 return pageURLKeysPartBuf.toString(); 2611 } 2612 2613 protected final String getContainerListURLPart(int id) { 2616 return condAppendURL(CONTAINERLIST_ID_PARAMETER, Integer.toString(id)); 2617 } 2618 2619 protected final String getContainerURLPart(int id) { 2620 return condAppendURL(CONTAINER_ID_PARAMETER, Integer.toString(id)); 2621 } 2622 2623 protected final String getFieldURLPart(int id) { 2624 return condAppendURL(FIELD_ID_PARAMETER, Integer.toString(id)); 2625 } 2626 2627 protected final String getOpModeURLPart(String mode) { 2631 return condAppendURL(OPERATION_MODE_PARAMETER, mode); 2632 } 2633 2634 protected final String getCacheModeURLPart(String mode) { 2637 return condAppendURL(CACHE_MODE_PARAMETER, mode); 2638 } 2639 2640 protected final String getEntryStateURLPart(int versionID) { 2642 return condAppendURL(ParamBean.ENTRY_STATE_PARAMETER, String.valueOf(versionID)); 2643 } 2644 2645 2650 protected final String getEntryStateURLPart(String status) { 2651 return condAppendURL(ParamBean.ENTRY_STATE_PARAMETER, status); 2652 } 2653 2654 2659 protected final String getShowRevisionDiffURLPart() { 2660 return getShowRevisionDiffURLPart(2); 2661 } 2662 2663 2670 protected final String getShowRevisionDiffURLPart(int versionID) { 2671 String value = ""; 2672 if (versionID == 2) { 2673 value = condAppendURL(ParamBean.SHOW_REVISION_DIFF_PARAMETER, "s"); 2674 } else if (versionID == 1) { 2675 value = condAppendURL(ParamBean.SHOW_REVISION_DIFF_PARAMETER, "a"); 2676 } else { 2677 value = condAppendURL(ParamBean.SHOW_REVISION_DIFF_PARAMETER, String.valueOf(versionID)); 2678 } 2679 return value; 2680 } 2681 2682 2692 protected final String getJahiaCoreHttpPath() { 2693 StringBuffer buffer = new StringBuffer (); 2698 2699 if (Jahia.getContextPath() != null) { 2700 buffer.append(Jahia.getContextPath()); 2701 } else { 2702 buffer.append(getRequest().getContextPath()); 2703 } 2704 2705 if (Jahia.getServletPath() != null) { 2706 buffer.append(Jahia.getServletPath()); 2707 } else { 2708 buffer.append(getRequest().getServletPath()); 2711 } 2712 2713 return buffer.toString(); 2714 } 2715 2716 2725 private String encodeURL(String inputURL) { 2726 if (inputURL.indexOf(";jsessionid=") != -1) { 2727 this.cacheStatus = ParamBean.CACHE_OFFONCE; 2728 logger.debug("jsession ALREADY in URL, returning URL unmodified..."); 2729 return inputURL; 2730 } 2731 if (getRealResponse() == null) { 2732 return inputURL; 2733 } 2734 String encodedURL = getResponse().encodeURL(inputURL); 2735 int sessionIDPos = encodedURL.indexOf(";jsessionid="); 2736 if (sessionIDPos != -1) { 2737 if (!ParamBean.CACHE_OFFONCE.equals(this.cacheStatus)) { 2738 logger.debug("jsessionid in URL, setting cache to off_once..."); 2739 this.cacheStatus = ParamBean.CACHE_OFFONCE; 2740 this.originalCacheStatus = ParamBean.CACHE_OFF; 2741 } 2742 } 2743 return encodedURL; 2744 } 2745 2746 public String getSiteURL() { 2747 if (getPage() != null) { 2748 return getSiteURL(getPageID(), true, true); 2749 } 2750 return getSiteURL(-1, true, true); 2751 } 2752 2753 2772 public String getSiteURL(int pageID, boolean withSessionID, boolean withOperationMode) { 2773 JahiaSite theSite = getSite(); 2776 if (theSite == null) { 2777 return ""; 2778 } 2779 2780 String siteServerName = theSite.getServerName(); 2781 boolean serverNameValid = false; 2782 String sessionIDStr = null; 2783 2784 GetMethod method = null; 2788 try { 2789 2790 Map contextVars = new HashMap (); 2791 contextVars.put("request", getRequest()); 2792 contextVars.put("siteServerName", siteServerName); 2793 String testURL = JahiaTools.evaluateExpressions(settings().getSiteServerNameTestURLExpr(), contextVars); 2794 URL targetURL = new URL (testURL); 2795 2796 method = new GetMethod(targetURL.toString()); 2798 2799 httpClient.executeMethod(method); 2801 2802 Header javaVersionHeader = method.getResponseHeader("jahia-version"); 2804 if (javaVersionHeader != null) { 2805 serverNameValid = true; 2806 } 2807 2808 } catch (Throwable t) { 2809 logger.error("Unable to check server name validity: " + siteServerName); 2810 serverNameValid = false; 2811 } finally { 2812 if (method != null) 2813 method.releaseConnection(); 2814 } 2815 2816 StringBuffer newSiteURL = new StringBuffer (getRequest().getScheme() + "://"); 2817 if (serverNameValid) { 2818 newSiteURL.append(siteServerName); 2821 if (!siteServerName.equals(getRequest().getServerName())) { 2822 try { 2825 HttpSession session = getSession(); 2826 sessionIDStr = ";jsessionid=" + session.getId(); 2827 } catch (JahiaSessionExpirationException jsee) { 2828 } 2829 } 2830 if (getRequest().getServerPort() != 80 && siteServerName.indexOf(":") == -1) { 2831 newSiteURL.append(":"); 2832 newSiteURL.append(getRequest().getServerPort()); 2833 } 2834 newSiteURL.append(getRequest().getContextPath()); 2835 newSiteURL.append(Jahia.getServletPath()); 2836 } else { 2837 newSiteURL.append(getRequest().getServerName()); 2840 if (getRequest().getServerPort() != 80) { 2841 newSiteURL.append(":"); 2842 newSiteURL.append(getRequest().getServerPort()); 2843 } 2844 newSiteURL.append(getRequest().getContextPath()); 2845 newSiteURL.append(Jahia.getServletPath()); 2846 newSiteURL.append("/site/"); 2847 newSiteURL.append(theSite.getSiteKey()); 2848 } 2849 if (pageID != -1) { 2850 newSiteURL.append("/pid/"); 2851 newSiteURL.append(pageID); 2852 } 2853 2854 if (withOperationMode) { 2855 try { 2856 HttpSession session = this.getSession(); 2857 if (session.getAttribute(OPERATION_MODE_PARAMETER) != null) { 2858 if (session.getAttribute(OPERATION_MODE_PARAMETER) instanceof String ) { 2859 String oldOpMode = (String ) session.getAttribute(OPERATION_MODE_PARAMETER); 2860 newSiteURL.append(getOpModeURLPart(oldOpMode)); 2861 } 2862 } else { 2863 if (this.getOperationMode() != null) { 2864 newSiteURL.append(getOpModeURLPart(this.getOperationMode())); 2865 } 2866 } 2867 } catch (JahiaSessionExpirationException jsee) { 2868 } 2871 } 2872 2873 if (withSessionID) { 2874 String serverURL = encodeURL(newSiteURL.toString()); 2875 if (sessionIDStr != null) { 2876 if (serverURL.indexOf("jsessionid") == -1) { 2877 serverURL += sessionIDStr; 2878 } 2879 } 2880 return serverURL; 2881 } else { 2882 return newSiteURL.toString(); 2883 } 2884 } 2885 2886 2891 public java.util.Date getCacheExpirationDate() { 2892 if (cacheExpirationDate != null) 2893 return cacheExpirationDate; 2894 else if (delayFromNow > -1) { 2895 return new Date (System.currentTimeMillis() + delayFromNow); 2896 } 2897 return null; 2898 } 2899 2900 public String getResponseMimeType() { 2901 return responseMimeType; 2902 } 2903 2904 2913 final public void setCacheExpirationDate(java.util.Date aCacheExpirationDate) { 2914 this.cacheExpirationDate = aCacheExpirationDate; 2915 } 2916 2917 2925 final public void setCacheExpirationDelay(long aDelayFromNow) { 2926 this.delayFromNow = aDelayFromNow; 2927 } 2928 2929 2936 final public long getCacheExpirationDelay() { 2937 return delayFromNow; 2938 } 2939 2940 private void processActivationAction() throws JahiaException { 2941 String validate = getParameter(VALIDATE_PARAMETER); 2943 if (validate != null) { 2944 if (validate.equals("on")) { 2945 if ((contentPage != null) && (theUser != null)) { 2946 logger.debug("Validating page"); 2947 Map languageStates = contentPage.getLanguagesStates(true); 2948 Set languageCodes = languageStates.keySet(); 2949 ServicesRegistry.getInstance().getJahiaVersionService() 2950 .activateStagedPage( 2951 contentPage.getID(), 2952 theUser, 2953 this, 2954 new StateModificationContext(new ContentPageKey(contentPage.getID()), 2955 languageCodes, false)); 2956 this.cacheStatus = ParamBean.CACHE_OFFONCE; 2957 } else { 2958 logger.debug("Can't validate page, user or page not found"); 2959 } 2960 } 2961 } 2962 } 2963 2964 private void resolveCacheStatus() { 2965 String newCacheStatus = getParameter(CACHE_MODE_PARAMETER); 2967 if (newCacheStatus != null) { 2968 if (CACHE_ON.equals(newCacheStatus)) { 2969 this.cacheStatus = ParamBean.CACHE_ON; 2970 } else if (CACHE_ONLYUPDATE.equals(newCacheStatus)) { 2971 this.cacheStatus = ParamBean.CACHE_ONLYUPDATE; 2972 } else if (CACHE_OFFONCE.equals(newCacheStatus)) { 2973 this.cacheStatus = ParamBean.CACHE_OFFONCE; 2974 } else if (CACHE_BYPASS.equals(newCacheStatus)) { 2975 this.cacheStatus = ParamBean.CACHE_BYPASS; 2976 } else if (CACHE_OFF.equals(newCacheStatus)) { 2977 this.cacheStatus = ParamBean.CACHE_OFF; 2978 } else { 2979 this.cacheStatus = ParamBean.CACHE_ON; 2980 } 2981 } else { 2982 this.cacheStatus = ParamBean.CACHE_ON; 2983 } 2984 this.originalCacheStatus = this.cacheStatus; 2985 } 2986 2987 private void loadJahiaPageFromContentPage() throws JahiaException { 2988 if (contentPage != null) { 2989 thePage = contentPage.getPage(this.getEntryLoadRequest(), this.getOperationMode(), this.getUser()); 2990 } 2991 } 2992 2993 private void checkPageAccess(HttpSession session, int pageID) throws JahiaException { 2994 2995 if (contentPage != null) { 2996 int deleteVersionID = contentPage.getDeleteVersionID(); 2997 if ((deleteVersionID != -1)) { 2998 2999 3002 if (NORMAL.equals(opMode)) { 3003 throw new JahiaPageNotFoundException(pageID); 3006 } 3007 3008 if (EDIT.equals(opMode) && (!contentPage.hasStagingEntries() || contentPage.isMarkedForDelete())) { 3009 throw new JahiaPageNotFoundException(pageID); 3012 } 3013 3014 if ((COMPARE.equals(opMode) || (PREVIEW.equals(opMode))) 3015 && (!contentPage.checkWriteAccess(theUser))) { 3016 throw new JahiaPageNotFoundException(pageID); 3019 } 3020 } 3021 3022 if ((NORMAL.equals(opMode) || COMPARE.equals(opMode)) && !contentPage.hasActiveEntries() 3024 && (deleteVersionID == -1)) { 3025 3026 opMode = EDIT; 3027 entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, getLocales()); 3028 resetSubstituteEntryLoadRequest(); 3030 if (!getEngine().equals(Login_Engine.ENGINE_NAME)) { 3031 throw new JahiaForbiddenAccessException(); 3032 } 3033 } 3034 } 3035 3036 if ((contentPage == null) 3037 || ((NORMAL.equals(opMode) || COMPARE.equals(opMode)) 3038 && getEntryLoadRequest().isCurrent() && !contentPage.hasActiveEntries()) 3039 || ((NORMAL.equals(opMode) || COMPARE.equals(opMode)) 3040 && getEntryLoadRequest().isCurrent() && (contentPage.getTitle(getEntryLoadRequest()) == null))) { 3041 throw new JahiaPageNotFoundException(pageID); 3042 } 3043 if (contentPage.getJahiaID() != getSiteID()) { 3045 contentPage = site.getHomeContentPage(); 3046 } 3047 3048 Integer lrpID = (Integer ) session.getAttribute(SESSION_LAST_REQUESTED_PAGE_ID); 3050 if (lrpID == null) { 3051 lrpID = new Integer (-1); 3052 } 3053 newPageRequest = (lrpID.intValue() != getPageID()); 3054 lastRequestedPageID = lrpID.intValue(); 3055 } 3056 3057 private void checkLocales() throws JahiaException { 3058 if ((NORMAL.equals(opMode) || COMPARE.equals(opMode)) 3059 && !contentPage.hasEntries(ContentPage.ACTIVE_PAGE_INFOS, getLocale().toString())) { 3060 ArrayList siteLanguages = site.getLanguageSettingsAsLocales(true); 3061 boolean skip = false; 3062 for (int i = 0; i < siteLanguages.size(); i++) { 3063 Locale locale = (Locale ) siteLanguages.get(i); 3064 if (!skip && contentPage.hasEntries(ContentPage.ACTIVE_PAGE_INFOS, locale.toString())) { 3065 changeLanguage(locale); 3066 skip = true; 3067 } 3068 } 3069 } 3070 } 3071 3072 private void resolveDiffVersionID(String verInfo) { 3073 verInfo = getParameter(ParamBean.SHOW_REVISION_DIFF_PARAMETER); 3075 if (verInfo != null) { 3076 if (verInfo.equals("s")) { 3077 this.diffVersionID = 2; } else if (verInfo.equals("a")) { 3079 this.diffVersionID = 1; } else { 3081 try { 3082 int ver = Integer.parseInt(verInfo); 3083 this.diffVersionID = ver; 3084 } catch (NumberFormatException nfe) { 3085 logger.debug("Diff VersionID format exception", nfe); 3086 } 3087 } 3088 } 3089 } 3090 3091 private String resolveEntryState() throws JahiaException { 3092 String verInfo = getParameter(ENTRY_STATE_PARAMETER); 3094 if (verInfo != null) { 3095 if (verInfo.equals("s")) { 3096 this.entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, getLocales()); 3097 this.cacheStatus = ParamBean.CACHE_OFFONCE; 3098 this.originalCacheStatus = this.cacheStatus; 3099 } else if (verInfo.equals("a")) { 3100 this.entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, getLocales()); 3101 this.cacheStatus = ParamBean.CACHE_OFFONCE; 3102 this.originalCacheStatus = this.cacheStatus; 3103 } else { 3104 try { 3105 int ver = Integer.parseInt(verInfo); 3106 this.entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.VERSIONED_WORKFLOW_STATE, ver, 3107 getLocales()); 3108 logger.debug("Using entry load request specified : " + this.entryLoadRequest); 3109 this.cacheStatus = ParamBean.CACHE_OFFONCE; 3110 this.originalCacheStatus = this.cacheStatus; 3111 } catch (NumberFormatException nfe) { 3112 logger.debug("VersionID format exception", nfe); 3113 } 3114 } 3115 } 3116 return verInfo; 3117 } 3118 3119 private void processLockAction(HttpServletResponse response, int pageID) throws JahiaException { 3120 try { 3123 LockService lockRegistry = ServicesRegistry.getInstance().getLockService(); 3124 String lockKeyStr = getParameter(ParamBean.STEAL_LOCK); 3125 LockKey lockKey; 3126 if (lockKeyStr != null) { 3127 lockKey = LockKey.composeLockKey(lockKeyStr); 3128 lockRegistry.steal(lockKey, getUser(), getSessionID()); 3129 response.sendRedirect(composePageUrl(pageID)); 3130 } else { 3131 lockKeyStr = getParameter(ParamBean.RELEASE_LOCK); 3132 if (lockKeyStr != null) { 3133 lockKey = LockKey.composeLockKey(lockKeyStr); 3134 lockRegistry.release(lockKey, getUser(), getSessionID()); 3135 response.sendRedirect(composePageUrl(pageID)); 3136 } 3137 } 3138 } catch (java.io.IOException ioe) { 3139 logger.error("Problem with sendRedirect response", ioe); 3140 } 3141 } 3143 3144 private void resolveLocales(HttpSession session) throws JahiaException { 3145 String languageCode = getParameter(LANGUAGE_CODE); 3147 3148 if (languageCode != null) { 3149 Locale previousLocale = (Locale ) session.getAttribute(SESSION_LOCALE); 3150 Locale newLocale = LanguageCodeConverters.languageCodeToLocale(languageCode); 3151 if (previousLocale == null || !previousLocale.equals(newLocale)) { 3152 localeList = null; 3155 } 3156 session.setAttribute(SESSION_LOCALE, newLocale); 3157 } 3158 if (session.getAttribute(SESSION_LOCALE) != null) { 3159 currentLocale = (Locale ) session.getAttribute(SESSION_LOCALE); 3160 } else { 3161 currentLocale = null; 3164 localeList = null; 3165 getLocale(); 3166 session.setAttribute(SESSION_LOCALE, currentLocale); 3167 } 3168 3169 this.currentLocale = (Locale ) getLocales().get(1); session.setAttribute(SESSION_LOCALE, currentLocale); 3173 3174 this.entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, getLocales()); 3175 if (this.getSiteID() != -1) { 3177 if (((opMode == EDIT) || (opMode == PREVIEW) || (opMode == COMPARE)) 3178 && (ServicesRegistry.getInstance().getJahiaVersionService().isStagingEnabled(this.getSiteID()))) { 3179 this.entryLoadRequest = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, getLocales()); 3180 this.cacheStatus = ParamBean.CACHE_OFFONCE; 3181 this.originalCacheStatus = this.cacheStatus; 3182 } 3183 } 3184 session.setAttribute(Globals.LOCALE_KEY, currentLocale); 3186 Config.set(session, Config.FMT_LOCALE, currentLocale); 3188 3189 } 3190 3191 private Boolean canEdit = null; 3192 3193 public boolean canEditCurrentPage() { 3194 if (canEdit == null) { 3195 canEdit = Boolean.valueOf(contentPage.checkWriteAccess(theUser, true) 3197 || contentPage.checkAdminAccess(theUser, true)); 3198 } 3199 return canEdit.booleanValue(); 3200 } 3201 3202 private void resolveOpMode(HttpSession session) throws JahiaInitializationException, 3203 JahiaSessionExpirationException { 3204 String paramOpMode = getParameter(OPERATION_MODE_PARAMETER); 3206 if (paramOpMode != null) { 3207 if (NORMAL.equals(paramOpMode)) 3208 opMode = NORMAL; 3209 else if (COMPARE.equals(paramOpMode)) 3210 opMode = canEditCurrentPage() ? COMPARE : NORMAL; 3211 else if (EDIT.equals(paramOpMode)) 3212 opMode = canEditCurrentPage() ? EDIT : NORMAL; 3213 else if (PREVIEW.equals(paramOpMode)) 3214 opMode = canEditCurrentPage() ? PREVIEW : NORMAL; 3215 else if (DEBUG.equals(paramOpMode)) 3216 opMode = contentPage.checkAdminAccess(theUser) ? DEBUG : NORMAL; 3217 } 3218 3219 if ((NORMAL.equals(opMode) || COMPARE.equals(opMode)) && !contentPage.hasActiveEntries() 3220 && canEditCurrentPage()) { 3221 try { 3222 int deleteVersionID = contentPage.getDeleteVersionID(); 3223 if (deleteVersionID == -1) { 3224 opMode = EDIT; 3226 } 3227 } catch (JahiaException je) { 3228 logger.error("Error while trying to test if page was deleted", je); 3229 } 3230 } 3231 3232 String oldOpMode; 3233 if (session.getAttribute(OPERATION_MODE_PARAMETER) instanceof String ) { 3234 oldOpMode = (String ) session.getAttribute(OPERATION_MODE_PARAMETER); 3235 } else { 3236 oldOpMode = NORMAL; 3237 } 3238 3239 if (!isInAdminMode()) { 3242 session.setAttribute(OPERATION_MODE_PARAMETER, opMode); 3243 } 3244 if (!oldOpMode.equals(opMode)) { 3245 logger.debug("Mode switch detected."); 3246 JahiaApplicationsDispatchingService dispatcher = ServicesRegistry.getInstance() 3249 .getJahiaApplicationsDispatchingService(); 3250 if (dispatcher != null) { 3251 dispatcher.flushAllSessionsCaches(session); 3252 } 3253 3254 HtmlCache htmlCache = CacheFactory.getHtmlCache(); 3256 if (htmlCache != null) 3257 htmlCache.invalidateUserEntries(getUser().getUsername()); 3258 else 3259 logger.warn("Could not get the HTML cache instance!!"); 3260 } 3261 } 3262 3263 private void resolveUser(HttpServletRequest request, HttpSession session) throws JahiaException { 3264 3265 Pipeline authPipeline = Jahia.getAuthPipeline(); 3266 try { 3267 authPipeline.invoke(this); 3268 } catch (PipelineException pe) { 3269 logger.error("Error while authorizing user", pe); 3270 theUser = null; 3271 } 3272 3273 if (theUser != null) { 3276 if (theUser.getUsername().equals(JahiaUserManagerService.GUEST_USERNAME) 3277 && theUser.getSiteID() != this.getSiteID()) { 3278 theUser = null; 3280 } 3281 } 3282 3283 if (theUser == null) { 3284 setUserGuest(getSiteID()); 3285 } 3286 } 3287 3288 public void setSite(JahiaSite jahiaSite) { 3289 this.site = jahiaSite; 3290 this.siteID = jahiaSite.getID(); 3291 this.siteKey = jahiaSite.getSiteKey(); 3292 } 3293 3294 3301 public void setTheUser(JahiaUser aUser) { 3302 this.theUser = aUser; 3303 } 3304 3305 public void setResponseMimeType(String aResponseMimeType) { 3306 this.responseMimeType = aResponseMimeType; 3307 } 3308 3309 3315 public static boolean isReservedKeyword(String str) { 3316 if (ParamBean.ENGINE_NAME_PARAMETER.equals(str) || ParamBean.SITE_KEY_PARAMETER.equals(str) 3317 || ParamBean.PAGE_ID_PARAMETER.equals(str) || ParamBean.CONTAINERLIST_ID_PARAMETER.equals(str) 3318 || ParamBean.CONTAINER_ID_PARAMETER.equals(str) || ParamBean.FIELD_ID_PARAMETER.equals(str) 3319 || ParamBean.OPERATION_MODE_PARAMETER.equals(str) || ParamBean.ENTRY_STATE_PARAMETER.equals(str) 3320 || ParamBean.SHOW_REVISION_DIFF_PARAMETER.equals(str) || ParamBean.VALIDATE_PARAMETER.equals(str) 3321 || ParamBean.LANGUAGE_CODE.equals(str) || ParamBean.RELEASE_LOCK.equals(str) 3322 || ParamBean.STEAL_LOCK.equals(str) || ParamBean.TEMPLATE_PARAMETER.equals(str) 3323 || ParamBean.CACHE_MODE_PARAMETER.equals(str)) 3324 return true; 3325 else if (isContainerScroll(str) == true) 3326 return true; 3327 return false; 3328 } 3329 3330 public static boolean isContainerScroll(String str) { 3331 if (str.startsWith(CONTAINER_SCROLL_PREFIX_PARAMETER)) 3332 return true; 3333 return false; 3334 } 3335 3336 public String toString() { 3337 return "ParamBean{user: " + theUser + "; siteId: " + siteID + "; locale: " + currentLocale + "; session: " 3338 + mRequest.getSession(false) + "; ...}"; 3339 } 3340 3341 protected void setData(JahiaSite jSite, JahiaUser jUser) { 3342 site = jSite; 3343 siteID = jSite.getID(); 3344 siteKey = jSite.getSiteKey(); 3345 theUser = jUser; 3346 } 3347} | Popular Tags |