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 javax.servlet.http.HttpSession;
021    
022    import org.apache.tapestry.BaseComponent;
023    import org.apache.tapestry.IRequestCycle;
024    
025    /**
026     * @author mb
027     * @since 4.0
028     */
029    public abstract class Timeout extends BaseComponent {
030            public abstract int getWarningTime();
031            public abstract int getAutoProlongTime();
032            
033            public abstract String getWarningMessage();
034            public abstract String getExpirationMessage();
035    
036            public abstract boolean getDisableWarning();
037            public abstract boolean getDisableAutoProlong();
038            
039            public abstract String getExpirationFunction();
040            
041            protected HttpSession getSession()
042            {
043                    return getPage().getRequestCycle().getRequestContext().getSession();
044            }
045            
046            protected int getSessionTime()
047            {
048                    return getSession().getMaxInactiveInterval();
049            }
050            
051            public boolean isInSession()
052            {
053                    HttpSession session = getSession();
054                    return session != null;
055            }
056            
057            public Map getScriptSymbols()
058            {
059                    int nSessionTime = getSessionTime();
060                    int nTimeToMessage = nSessionTime - getWarningTime();
061                    if (nTimeToMessage < 0)
062                            nTimeToMessage = 0;
063                    int nRemainingTime = nSessionTime - nTimeToMessage;
064                    int nAutoProlongTime = nSessionTime - getAutoProlongTime();
065                    
066                    Map mapSymbols = new HashMap();
067                    mapSymbols.put("confirmTimeout", new Integer(nTimeToMessage*1000));
068                    mapSymbols.put("expirationTimeout", new Integer(nRemainingTime*1000));
069                    mapSymbols.put("prolongSessionPeriod", new Integer(nAutoProlongTime*1000));
070                    mapSymbols.put("confirmMessage", getWarningMessage());
071                    mapSymbols.put("expirationMessage", getExpirationMessage());
072                    mapSymbols.put("disableWarning", new Boolean(getDisableWarning()));
073                    mapSymbols.put("disableAutoProlong", new Boolean(getDisableAutoProlong()));
074                    mapSymbols.put("expirationFunction", getExpirationFunction());
075                    return mapSymbols;
076            }
077            
078            public void renewSession(IRequestCycle cycle)
079            {
080                    // calling this method via the XTile service will automatically renew the session
081                    // System.out.println("Prolonging session...");
082            }
083    }