001/*
002 * (C) Copyright 2013 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 */
020
021package org.nuxeo.ecm.platform.ui.web.seam;
022
023import static org.jboss.seam.annotations.Install.FRAMEWORK;
024
025import javax.faces.context.FacesContext;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.jboss.seam.ScopeType;
030import org.jboss.seam.annotations.Install;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.jboss.seam.annotations.intercept.BypassInterceptors;
034import org.jboss.seam.core.ConversationEntry;
035import org.jboss.seam.international.StatusMessage.Severity;
036import org.jboss.seam.jsf.concurrency.AbstractResolver;
037import org.jboss.seam.jsf.concurrency.ConcurrentRequestResolver;
038import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService;
039import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
040
041/**
042 * Default implementation to handle concurrent requests against the same Seam Conversation. This component can be
043 * overridden using the standard Seam override system.
044 *
045 * @since 5.7.3
046 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
047 */
048@Scope(ScopeType.STATELESS)
049@Name(ConcurrentRequestResolver.NAME)
050@Install(precedence = FRAMEWORK)
051@BypassInterceptors
052public class NuxeoConcurrentRequestResolver extends AbstractResolver implements ConcurrentRequestResolver {
053
054    @Override
055    public boolean handleConcurrentRequest(ConversationEntry ce, HttpServletRequest request,
056            HttpServletResponse response) {
057
058        if (request.getMethod().equalsIgnoreCase("get")) {
059            // flag request to skip apply method bindings
060            request.setAttribute(URLPolicyService.DISABLE_ACTION_BINDING_KEY, Boolean.TRUE);
061            // let's try to continue
062            addTransientMessage(Severity.WARN, "org.nuxeo.seam.concurrent.unsaferun",
063                    "This page may be not up to date, an other concurrent requests is still running");
064            return true;
065        } else if (request.getMethod().equalsIgnoreCase("post")) {
066
067            if (FacesContext.getCurrentInstance() == null) {
068                // we are not inside JSF
069                // may be an Automation call inside the Seam context
070                // continuing is not safe !
071                return false;
072            }
073
074            String url = request.getHeader("referer");
075            if (url == null || url.length() == 0) {
076                url = VirtualHostHelper.getServerURL(request) + request.getRequestURI();
077            }
078            addTransientMessage(Severity.WARN, "org.nuxeo.seam.concurrent.skip",
079                    "Your request was not processed because you already have a requests in processing.");
080            // XXX should Mark Request for No Tx Commit !
081            return handleRedirect(ce, response, url);
082        } else {
083            return handleNoResponse(response);
084        }
085    }
086
087}