001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.automation.seam.operations;
019
020import javax.faces.context.FacesContext;
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.jboss.seam.contexts.Contexts;
026import org.jboss.seam.contexts.ServletLifecycle;
027import org.jboss.seam.core.ConversationPropagation;
028import org.jboss.seam.core.Manager;
029import org.jboss.seam.web.ServletContexts;
030import org.nuxeo.ecm.automation.OperationContext;
031import org.nuxeo.ecm.automation.jsf.OperationHelper;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.NuxeoPrincipal;
035import org.nuxeo.ecm.platform.actions.ActionContext;
036import org.nuxeo.ecm.platform.actions.seam.SeamActionContext;
037import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
038import org.nuxeo.ecm.platform.ui.web.util.SeamContextHelper;
039
040/**
041 * Utility class used to manage Seam init and cleanup
042 *
043 * @author Tiry (tdelprat@nuxeo.com)
044 */
045public class SeamOperationFilter {
046
047    protected static final Log log = LogFactory.getLog(SeamOperationFilter.class);
048
049    /**
050     * Initialize a workable Seam context as well as a conversion if needed
051     *
052     * @param context
053     * @param conversationId
054     */
055    public static void handleBeforeRun(OperationContext context, String conversationId) {
056
057        CoreSession session = context.getCoreSession();
058
059        // Initialize Seam context if needed
060        if (!OperationHelper.isSeamContextAvailable()) {
061            initializeSeamContext(context, conversationId, session);
062        } else {
063            // Only set Seam Action context
064            setSeamActionContext(context, session);
065        }
066    }
067
068    /**
069     * Manages Seam context and lifecycle cleanup
070     *
071     * @param context
072     * @param conversationId
073     */
074    public static void handleAfterRun(OperationContext context, String conversationId) {
075
076        // Cannot destroy Seam context if it is not initialized
077        if (!OperationHelper.isSeamContextAvailable()) {
078            log.error("Cannot destroy Seam context: it is not initialized");
079            return;
080        }
081
082        HttpServletRequest request = getRequest(context);
083        if (request == null) {
084            log.error("Can not destroy Seam context: no HttpServletRequest was found");
085            return;
086        }
087
088        if (conversationId == null) {
089            conversationId = (String) context.get("conversationId");
090        }
091
092        if (conversationId != null) {
093            // CoreSession seamDocumentManager = (CoreSession)
094            // Contexts.getConversationContext().get("seamDocumentManager");
095            Contexts.getEventContext().remove("documentManager");
096            // Manager.instance().endConversation(true);
097        }
098        ServletLifecycle.endRequest(request);
099    }
100
101    protected static void initializeSeamContext(OperationContext context, String conversationId, CoreSession session)
102            {
103
104        HttpServletRequest request = getRequest(context);
105        if (request == null) {
106            throw new NuxeoException("Can not init Seam context: no HttpServletRequest was found");
107        }
108        ServletLifecycle.beginRequest(request);
109        ServletContexts.instance().setRequest(request);
110
111        if (conversationId == null) {
112            conversationId = (String) context.get("conversationId");
113        }
114
115        if (conversationId != null) {
116            ConversationPropagation.instance().setConversationId(conversationId);
117            Manager.instance().restoreConversation();
118            ServletLifecycle.resumeConversation(request);
119            Contexts.getEventContext().set("documentManager", session);
120            setSeamActionContext(context, session);
121        }
122    }
123
124    /**
125     * Gets the request from the Automation context, fallback on the FacesContext.
126     */
127    protected static HttpServletRequest getRequest(OperationContext context) {
128        HttpServletRequest request = (HttpServletRequest) context.get("request");
129        if (request == null) {
130            FacesContext faces = FacesContext.getCurrentInstance();
131            if (faces != null) {
132                request = (HttpServletRequest) faces.getExternalContext().getRequest();
133            }
134        }
135        return request;
136    }
137
138    protected static void setSeamActionContext(OperationContext context, CoreSession session) {
139        ActionContext seamActionContext = new SeamActionContext();
140        NavigationContext navigationContext = (NavigationContext) Contexts.getConversationContext().get(
141                "navigationContext");
142        if (navigationContext != null) {
143            seamActionContext.setCurrentDocument(navigationContext.getCurrentDocument());
144        }
145        seamActionContext.setDocumentManager(session);
146        seamActionContext.putLocalVariable("SeamContext", new SeamContextHelper());
147        seamActionContext.setCurrentPrincipal((NuxeoPrincipal) session.getPrincipal());
148
149        context.put("seamActionContext", seamActionContext);
150    }
151
152}