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  
39  /**
40   * @deprecated since Tobago 2.0.8 - Please use <f:initParam> instead - see also TOBAGO-1456
41   */
42  @Deprecated
43  public abstract class NonFacesRequestServlet extends HttpServlet {
44  
45    private static final long serialVersionUID = -7448621953821447997L;
46  
47    private static final Logger LOG = LoggerFactory.getLogger(NonFacesRequestServlet.class);
48  
49    @Override
50    protected void service(final HttpServletRequest request, final HttpServletResponse response)
51        throws ServletException, IOException {
52  
53      final LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
54      final Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
55      final FacesContextFactory fcFactory
56          = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
57      final FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
58      try {
59  
60        // invoke application
61        final String outcome = invokeApplication(facesContext);
62  
63        if (facesContext.getResponseComplete()) {
64          return;
65        }
66        if (LOG.isDebugEnabled()) {
67          LOG.debug("outcome = '" + outcome + "'");
68        }
69  
70        final Application application = facesContext.getApplication();
71        if (facesContext.getViewRoot() == null) {
72          facesContext.setViewRoot(createViewRoot(facesContext));
73        }
74  
75        final NavigationHandler navigationHandler = application.getNavigationHandler();
76        navigationHandler.handleNavigation(facesContext, null, outcome);
77  
78  
79        lifecycle.render(facesContext);
80      } finally {
81        facesContext.release();
82      }
83    }
84  
85    protected UIViewRoot createViewRoot(final FacesContext facesContext) {
86      return facesContext.getApplication().getViewHandler().createView(facesContext, getFromViewId());
87    }
88  
89    public abstract String invokeApplication(FacesContext facesContext);
90  
91    /**
92     * will be called to initialize the first ViewRoot,
93     * may be overwritten by extended classes
94     */
95    public String getFromViewId() {
96      return "";
97    }
98  }