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.internal.lifecycle;
21  
22  import javax.faces.FacesException;
23  import javax.faces.application.Application;
24  import javax.faces.application.ViewHandler;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.PhaseId;
28  import javax.servlet.http.HttpServletResponse;
29  import java.io.IOException;
30  
31  /**
32   * Implements the lifecycle as described in Spec. 1.0 PFD Chapter 2
33   * <p/>
34   * render response phase (JSF Spec 2.2.6)
35   */
36  class RenderResponseExecutor implements PhaseExecutor {
37  
38    public boolean execute(FacesContext facesContext) {
39      Application application = facesContext.getApplication();
40      ViewHandler viewHandler = application.getViewHandler();
41  
42      try {
43        final UIViewRoot viewRoot = facesContext.getViewRoot();
44        if (viewRoot.getViewId() != null) {
45          viewHandler.renderView(facesContext, viewRoot);
46        } else {
47          Object respObj = facesContext.getExternalContext().getResponse();
48          if (respObj instanceof HttpServletResponse) {
49              HttpServletResponse respHttp = (HttpServletResponse) respObj;
50              respHttp.sendError(HttpServletResponse.SC_NOT_FOUND);
51              facesContext.responseComplete();
52          }
53        }
54      } catch (IOException e) {
55        throw new FacesException(e.getMessage(), e);
56      }
57      return false;
58    }
59  
60    public PhaseId getPhase() {
61      return PhaseId.RENDER_RESPONSE;
62    }
63  }