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.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
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 Logger LOG = LoggerFactory.getLogger(NonFacesRequestServlet.class);
043    
044      @Override
045      protected void service(HttpServletRequest request, HttpServletResponse response)
046          throws ServletException, IOException {
047    
048        LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
049        Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
050        FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
051        FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
052        try {
053    
054          // invoke application
055          String outcome = invokeApplication(facesContext);
056    
057          if (facesContext.getResponseComplete()) {
058            return;
059          }
060          if (LOG.isDebugEnabled()) {
061            LOG.debug("outcome = '" + outcome + "'");
062          }
063    
064          Application application = facesContext.getApplication();
065          if (facesContext.getViewRoot() == null) {
066            ViewHandler viewHandler = application.getViewHandler();
067            String viewId = getFromViewId();
068            UIViewRoot view = viewHandler.createView(facesContext, viewId);
069            facesContext.setViewRoot(view);
070          }
071    
072          NavigationHandler navigationHandler = application.getNavigationHandler();
073          navigationHandler.handleNavigation(facesContext, null, outcome);
074    
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    }