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