001    package org.apache.myfaces.tobago.servlet;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    
023    import javax.faces.FactoryFinder;
024    import javax.faces.application.Application;
025    import javax.faces.application.NavigationHandler;
026    import javax.faces.application.ViewHandler;
027    import javax.faces.component.UIViewRoot;
028    import javax.faces.context.FacesContext;
029    import javax.faces.context.FacesContextFactory;
030    import javax.faces.lifecycle.Lifecycle;
031    import javax.faces.lifecycle.LifecycleFactory;
032    import javax.servlet.ServletException;
033    import javax.servlet.http.HttpServlet;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    import java.io.IOException;
037    
038    public abstract class NonFacesRequestServlet extends HttpServlet {
039    
040      private static final long serialVersionUID = -7448621953821447997L;
041    
042      private static final Log LOG = LogFactory.getLog(NonFacesRequestServlet.class);
043    
044      protected void service(HttpServletRequest request,
045          HttpServletResponse response) throws ServletException,
046          IOException {
047    
048        LifecycleFactory lFactory = (LifecycleFactory)
049            FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
050        Lifecycle lifecycle =
051            lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
052        FacesContextFactory fcFactory = (FacesContextFactory)
053            FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
054        FacesContext facesContext =
055            fcFactory.getFacesContext(getServletContext(), request, response,
056                lifecycle);
057        Application application = facesContext.getApplication();
058        ViewHandler viewHandler = application.getViewHandler();
059        String viewId = getFromViewId();
060        UIViewRoot view = viewHandler.createView(facesContext, viewId);
061        facesContext.setViewRoot(view);
062    
063    //    ExternalContext externalContext = facesContext.getExternalContext();
064    //    externalContext.getRequestMap().put("com.sun.faces.INVOCATION_PATH", "/faces");
065    
066        // invoke application
067        String outcome = invokeApplication(facesContext);
068    
069        if (facesContext.getResponseComplete()) {
070          return;
071        }
072        if (LOG.isDebugEnabled()) {
073          LOG.debug("outcome = '" + outcome + "'");
074        }
075        NavigationHandler navigationHandler = application.getNavigationHandler();
076        navigationHandler.handleNavigation(facesContext, null, outcome);
077        lifecycle.render(facesContext);
078      }
079    
080      public abstract String invokeApplication(FacesContext facesContext);
081    
082      /**
083       * will be called to initilize the first ViewRoot,
084       * may be overwritten by extended classes
085       */
086      public String getFromViewId() {
087        return "";
088      }
089    }