/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a
 * copy of the License at the following location:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jasig.cas.web.flow;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Min;
import org.jasig.cas.web.support.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.FlowExecutionListenerAdapter;
import org.springframework.webflow.execution.FlowSession;
import org.springframework.webflow.execution.RequestContext;

/**
 * Listener to expire web session as soon as the webflow is ended. The goal is to decrease memory
 * consumption by deleting as soon as
 * possible the web sessions created mainly for login process.
 *
 * @author Jerome Leleu
 * @author Marvin S. Addison
 * @since 3.5.1
 */
public final class TerminateWebSessionListener extends FlowExecutionListenerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(TerminateWebSessionListener.class);

    @Min(0)
    private int timeToDieInSeconds = 2;

    @Override
    public void sessionEnded(final RequestContext context, final FlowSession session, final String outcome,
                             final AttributeMap output) {

        final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
        // get session but don't create it if it doesn't already exist
        final HttpSession webSession = request.getSession(false);

        if (webSession != null) {
            LOGGER.debug("Terminate web session {} in {} seconds", webSession.getId(), this.timeToDieInSeconds);
            // set the web session to die in timeToDieInSeconds
            webSession.setMaxInactiveInterval(this.timeToDieInSeconds);
        }
    }

    public int getTimeToDieInSeconds() {
        return this.timeToDieInSeconds;
    }

    public void setTimeToDieInSeconds(final int timeToDieInSeconds) {
        this.timeToDieInSeconds = timeToDieInSeconds;
    }
}

@@ -2007,24 +2007,62 @@
  output) %7B%0A%0A
+        if ( session.isRoot() ) %7B%0A    
         fina
@@ -2141,16 +2141,20 @@
         
+    
 // get s
@@ -2208,32 +2208,36 @@
 y exist%0A        
+    
 final HttpSessio
@@ -2283,24 +2283,28 @@
 );%0A%0A        
+  
+  
 if (webSessi
@@ -2313,24 +2313,28 @@
  != null) %7B%0A
+    
             
@@ -2446,16 +2446,20 @@
         
+   
+ 
 // set t
@@ -2502,16 +2502,20 @@
 Seconds%0A
+    
         
@@ -2570,32 +2570,46 @@
 oDieInSeconds);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix for CAS-1412