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