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.application.ViewHandler;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.FacesContextFactory;
32  import javax.faces.lifecycle.Lifecycle;
33  import javax.faces.lifecycle.LifecycleFactory;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.IOException;
39  
40  public abstract class NonFacesRequestServlet extends HttpServlet {
41  
42    private static final long serialVersionUID = -7448621953821447997L;
43  
44    private static final Logger LOG = LoggerFactory.getLogger(NonFacesRequestServlet.class);
45  
46    @Override
47    protected void service(HttpServletRequest request, HttpServletResponse response)
48        throws ServletException, IOException {
49  
50      LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
51      Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
52      FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
53      FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
54      try {
55  
56        // invoke application
57        String outcome = invokeApplication(facesContext);
58  
59        if (facesContext.getResponseComplete()) {
60          return;
61        }
62        if (LOG.isDebugEnabled()) {
63          LOG.debug("outcome = '" + outcome + "'");
64        }
65  
66        Application application = facesContext.getApplication();
67        if (facesContext.getViewRoot() == null) {
68          ViewHandler viewHandler = application.getViewHandler();
69          String viewId = getFromViewId();
70          UIViewRoot view = viewHandler.createView(facesContext, viewId);
71          facesContext.setViewRoot(view);
72        }
73  
74        NavigationHandler navigationHandler = application.getNavigationHandler();
75        navigationHandler.handleNavigation(facesContext, null, outcome);
76  
77  
78        lifecycle.render(facesContext);
79      } finally {
80        facesContext.release();
81      }
82    }
83  
84    public abstract String invokeApplication(FacesContext facesContext);
85  
86    /**
87     * will be called to initialize the first ViewRoot,
88     * may be overwritten by extended classes
89     */
90    public String getFromViewId() {
91      return "";
92    }
93  }