001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.container;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.jetspeed.om.page.Page;
022: import org.apache.jetspeed.pipeline.PipelineException;
023: import org.apache.jetspeed.pipeline.valve.AbstractValve;
024: import org.apache.jetspeed.pipeline.valve.ValveContext;
025: import org.apache.jetspeed.request.RequestContext;
026:
027: /**
028: * <p>
029: * Valve basically mantains the page navigation history by maintaining a previous page id in the session.
030: * Required by JS2-806
031: * </p>
032: *
033: * @author <a href="mailto:kmoh.raj@gmail.com">Mohan Kannapareddy</a>
034: * @version $Id$
035: */
036: public class PageHistoryValve extends AbstractValve {
037: protected final Log log = LogFactory.getLog(getClass());
038:
039: // SessionFullExtendedNavigationalState object needs this.
040: public static final String REQUEST_CLEAR_PORTLETS_MODE_AND_WINDOWSTATE_KEY = "clearPortletsModeAndWindowState";
041:
042: private final String SESSION_PREVIOUS_PAGEID_KEY = "PreviousPageId";
043: private boolean valveDisabled = false;
044:
045: /* (non-Javadoc)
046: * @see org.apache.jetspeed.pipeline.valve.AbstractValve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)
047: */
048: public void invoke(RequestContext request, ValveContext context)
049: throws PipelineException {
050: if (valveDisabled) {
051: if (log.isDebugEnabled()) {
052: log.debug(toString() + " is DISABLED");
053: }
054: } else { //OK, the valve is enabled check and see if are a inter-page nav.
055: try {
056: // create a session if not already created, necessary for Tomcat 5
057: request.getRequest().getSession(true);
058:
059: Page page = request.getPage();
060: String curPageId = page.getId();
061:
062: String prevPageId = (String) request
063: .getSessionAttribute(SESSION_PREVIOUS_PAGEID_KEY);
064: if (prevPageId == null) {
065: //First time, lets set it
066: request.setSessionAttribute(
067: SESSION_PREVIOUS_PAGEID_KEY, curPageId);
068: if (log.isDebugEnabled()) {
069: log
070: .debug("No previous page Id found in session, setting it for the first time");
071: }
072: } else {
073:
074: if (prevPageId.equalsIgnoreCase(curPageId)) {
075: if (log.isDebugEnabled()) {
076: log
077: .debug("Previous page id is same as current page id, not clearing page state");
078: }
079: } else {
080: if (log.isDebugEnabled()) {
081: log
082: .debug("Page Change encountered Current Page:"
083: + curPageId
084: + " Prev Page:"
085: + prevPageId);
086: }
087: // Make sure we set the prevPageId in session
088: request.setSessionAttribute(
089: SESSION_PREVIOUS_PAGEID_KEY, curPageId);
090: // inform NavigationalState object we want to clear all Modes
091: request
092: .setAttribute(
093: REQUEST_CLEAR_PORTLETS_MODE_AND_WINDOWSTATE_KEY,
094: Boolean.TRUE);
095: }
096: }
097: } catch (Exception e) {
098: throw new PipelineException(e);
099: }
100: }
101: // Pass control to the next Valve in the Pipeline
102: context.invokeNext(request);
103:
104: }
105:
106: public String toString() {
107: return "PageHistoryValve";
108: }
109:
110: public void setValveDisabled(boolean valveDisabled) {
111: this .valveDisabled = valveDisabled;
112: }
113:
114: public boolean isValveDisabled() {
115: return valveDisabled;
116: }
117:
118: }
|