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,
047          IOException {
048    
049        LifecycleFactory lFactory = (LifecycleFactory)
050            FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
051        Lifecycle lifecycle =
052            lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
053        FacesContextFactory fcFactory = (FacesContextFactory)
054            FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
055        FacesContext facesContext =
056            fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
057        try {
058          Application application = facesContext.getApplication();
059          ViewHandler viewHandler = application.getViewHandler();
060          String viewId = getFromViewId();
061          UIViewRoot view = viewHandler.createView(facesContext, viewId);
062          facesContext.setViewRoot(view);
063    
064    //    ExternalContext externalContext = facesContext.getExternalContext();
065    //    externalContext.getRequestMap().put("com.sun.faces.INVOCATION_PATH", "/faces");
066    
067          // invoke application
068          String outcome = invokeApplication(facesContext);
069    
070          if (facesContext.getResponseComplete()) {
071            return;
072          }
073          if (LOG.isDebugEnabled()) {
074            LOG.debug("outcome = '" + outcome + "'");
075          }
076          NavigationHandler navigationHandler = application.getNavigationHandler();
077          navigationHandler.handleNavigation(facesContext, null, outcome);
078          lifecycle.render(facesContext);
079        } finally {
080          facesContext.release();
081        }
082      }
083    
084      public abstract String invokeApplication(FacesContext facesContext);
085    
086      /**
087       * will be called to initialize the first ViewRoot,
088       * may be overwritten by extended classes
089       */
090      public String getFromViewId() {
091        return "";
092      }
093    }