View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.servlet;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.FactoryFinder;
26  import javax.faces.application.Application;
27  import javax.faces.application.NavigationHandler;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.FacesContextFactory;
31  import javax.faces.lifecycle.Lifecycle;
32  import javax.faces.lifecycle.LifecycleFactory;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import java.io.IOException;
38  import java.lang.invoke.MethodHandles;
39  
40  /**
41   * @deprecated since Tobago 2.0.8 - Please use <f:initParam> instead - see also TOBAGO-1456
42   */
43  @Deprecated
44  public abstract class NonFacesRequestServlet extends HttpServlet {
45  
46    private static final long serialVersionUID = -7448621953821447997L;
47  
48    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49  
50    @Override
51    protected void service(final HttpServletRequest request, final HttpServletResponse response)
52        throws ServletException, IOException {
53  
54      final LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
55      final Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
56      final FacesContextFactory fcFactory
57          = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
58      final FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
59      try {
60  
61        // invoke application
62        final String outcome = invokeApplication(facesContext);
63  
64        if (facesContext.getResponseComplete()) {
65          return;
66        }
67        if (LOG.isDebugEnabled()) {
68          LOG.debug("outcome = '" + outcome + "'");
69        }
70  
71        final Application application = facesContext.getApplication();
72        if (facesContext.getViewRoot() == null) {
73          facesContext.setViewRoot(createViewRoot(facesContext));
74        }
75  
76        final NavigationHandler navigationHandler = application.getNavigationHandler();
77        navigationHandler.handleNavigation(facesContext, null, outcome);
78  
79  
80        lifecycle.render(facesContext);
81      } finally {
82        facesContext.release();
83      }
84    }
85  
86    protected UIViewRoot createViewRoot(final FacesContext facesContext) {
87      return facesContext.getApplication().getViewHandler().createView(facesContext, getFromViewId());
88    }
89  
90    public abstract String invokeApplication(FacesContext facesContext);
91  
92    /**
93     * will be called to initialize the first ViewRoot,
94     * may be overwritten by extended classes
95     */
96    public String getFromViewId() {
97      return "";
98    }
99  }