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