001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.ajax;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.tapestry.BaseComponent;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.web.WebRequest;
023    import org.apache.tapestry.web.WebSession;
024    
025    /**
026     * @author mb
027     * @since 4.0
028     */
029    public abstract class Timeout extends BaseComponent
030    {
031    
032        public abstract int getWarningTime();
033    
034        public abstract int getAutoProlongTime();
035    
036        public abstract String getWarningMessage();
037    
038        public abstract String getExpirationMessage();
039    
040        public abstract boolean getDisableWarning();
041    
042        public abstract boolean getDisableAutoProlong();
043    
044        public abstract String getExpirationFunction();
045    
046        public abstract WebRequest getRequest();
047        
048        protected WebSession getSession()
049        {
050            return getRequest().getSession(true);
051        }
052        
053        protected int getSessionTime()
054        {
055            return getSession().getMaxInactiveInterval();
056        }
057        
058        public boolean isInSession()
059        {
060            return getRequest().getSession(false) != null;
061        }
062    
063        public Map getScriptSymbols()
064        {
065            int nSessionTime = getSessionTime();
066            int nTimeToMessage = nSessionTime - getWarningTime();
067            if (nTimeToMessage < 0) nTimeToMessage = 0;
068            int nRemainingTime = nSessionTime - nTimeToMessage;
069            int nAutoProlongTime = nSessionTime - getAutoProlongTime();
070            
071            Map mapSymbols = new HashMap();
072            mapSymbols.put("confirmTimeout", new Integer(nTimeToMessage * 1000));
073            mapSymbols.put("expirationTimeout", new Integer(nRemainingTime * 1000));
074            mapSymbols.put("prolongSessionPeriod", new Integer(
075                    nAutoProlongTime * 1000));
076            mapSymbols.put("confirmMessage", getWarningMessage());
077            mapSymbols.put("expirationMessage", getExpirationMessage());
078            mapSymbols.put("disableWarning", new Boolean(getDisableWarning()));
079            mapSymbols.put("disableAutoProlong", new Boolean(
080                    getDisableAutoProlong()));
081            mapSymbols.put("expirationFunction", getExpirationFunction());
082            return mapSymbols;
083        }
084    
085        public void renewSession(IRequestCycle cycle)
086        {
087            // calling this method via the XTile service will automatically renew
088            // the session
089            // System.out.println("Prolonging session...");
090        }
091    }