001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.auth.plugins;
023
024import javax.faces.context.ExternalContext;
025import javax.faces.context.FacesContext;
026import javax.servlet.ServletRequest;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpSession;
029
030import org.jboss.seam.Seam;
031import org.jboss.seam.contexts.ServletLifecycle;
032import org.jboss.seam.core.Manager;
033import org.nuxeo.common.utils.ExceptionUtils;
034import org.nuxeo.ecm.platform.ui.web.rest.FancyURLRequestWrapper;
035
036public class SeamJsfSessionManager extends DefaultSessionManager {
037
038    @Override
039    public boolean canBypassRequest(ServletRequest request) {
040        return request instanceof FancyURLRequestWrapper;
041    }
042
043    @Override
044    public void onBeforeSessionInvalidate(ServletRequest request) {
045        try {
046            Seam.invalidateSession();
047        } catch (RuntimeException e) {
048            // TODO what is caught here?
049            super.onBeforeSessionInvalidate(request);
050        }
051    }
052
053    @Override
054    public void onBeforeSessionReinit(ServletRequest request) {
055        // destroy session
056        // because of Seam Phase Listener we can't use Seam.invalidateSession()
057        // because the session would be invalidated at the end of the request !
058        HttpServletRequest httpRequest = (HttpServletRequest) request;
059        HttpSession session = httpRequest.getSession(false);
060        if (session != null) {
061            FacesContext facesContext = FacesContext.getCurrentInstance();
062            if (facesContext != null) {
063                ExternalContext externalContext = facesContext.getExternalContext();
064                // Make long-running conversation temporary
065                Manager.instance().endConversation(true);
066                Manager.instance().endRequest(externalContext.getSessionMap());
067                ServletLifecycle.endRequest(httpRequest);
068            }
069        }
070    }
071
072    @Override
073    public void onAfterSessionReinit(ServletRequest request) {
074        HttpServletRequest httpRequest = (HttpServletRequest) request;
075        // reinit Seam so the afterResponseComplete does not crash
076        ServletLifecycle.beginRequest(httpRequest);
077    }
078
079}