001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.listener;
017
018import javax.servlet.http.HttpSessionEvent;
019import javax.servlet.http.HttpSessionListener;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.kew.api.exception.WorkflowException;
023import org.kuali.rice.kim.api.identity.Person;
024import org.kuali.rice.krad.UserSession;
025import org.kuali.rice.krad.document.Document;
026import org.kuali.rice.krad.document.authorization.PessimisticLock;
027import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
028import org.kuali.rice.krad.util.GlobalVariables;
029import org.kuali.rice.krad.util.KRADConstants;
030import org.kuali.rice.krad.util.ObjectUtils;
031
032import java.util.List;
033
034/**
035 * This class is used to handle session timeouts where {@link PessimisticLock} objects should
036 * be removed from a document 
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041public class KualiHttpSessionListener implements HttpSessionListener {
042
043    /**
044     *  HttpSession hook for additional setup method when sessions are created
045     *
046     * @param se - the HttpSessionEvent containing the session
047     *
048     *  @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
049     */
050    public void sessionCreated(HttpSessionEvent se) {
051        // no operation required at this time
052    }
053
054    /**
055     * HttpSession hook for additional cleanup when sessions are destroyed
056     *
057     * @param se - the HttpSessionEvent containing the session
058     * 
059     * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
060     */
061    public void sessionDestroyed(HttpSessionEvent se) {
062        releaseLocks();
063    }
064
065    /**
066     * Remove any locks that the user has for this session
067     */
068    private void releaseLocks() {
069        String sessionId = GlobalVariables.getUserSession().getKualiSessionId();
070        List<PessimisticLock> locks = KRADServiceLocatorWeb.getPessimisticLockService().getPessimisticLocksForSession(sessionId);
071        Person user = GlobalVariables.getUserSession().getPerson();
072
073        KRADServiceLocatorWeb.getPessimisticLockService().releaseAllLocksForUser(locks, user);
074    }
075
076}
077