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.application;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.el.MethodExpression;
26  import javax.faces.FacesException;
27  import javax.faces.application.Application;
28  import javax.faces.application.FacesMessage;
29  import javax.faces.application.NavigationHandler;
30  import javax.faces.component.ActionSource2;
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.AbortProcessingException;
34  import javax.faces.event.ActionEvent;
35  import javax.faces.event.ActionListener;
36  
37  public class ActionListenerImpl implements ActionListener {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(ActionListenerImpl.class);
40  
41    private ActionListener base;
42  
43    private String errorOutcome = "error";
44  
45    public ActionListenerImpl(final ActionListener base) {
46      this.base = base;
47    }
48  
49    public void processAction(final ActionEvent event) throws AbortProcessingException {
50      try {
51        base.processAction(event);
52      } catch (Throwable e) {
53        if (e instanceof FacesException) {
54          Throwable fe = e;
55          while (fe != null) {
56            if (fe instanceof AbortProcessingException) {
57              throw (FacesException) e;
58            }
59            fe = fe.getCause();
60          }
61        }
62        LOG.error("Processing failed. Forwarding to error page. errorOutcome=" + errorOutcome, e.getCause());
63        final FacesContext facesContext = FacesContext.getCurrentInstance();
64        if (e.getCause() != null) {
65           FacesMessage facesMessage = new FacesMessage(e.getCause().toString());
66           facesContext.addMessage(null, facesMessage);
67        }
68        final UIComponent source = event.getComponent();
69        final ActionSource2 actionSource = (ActionSource2) source;
70        final Application application = facesContext.getApplication();
71        final MethodExpression expression = actionSource.getActionExpression();
72        // Retrieve the NavigationHandler instance..
73        final NavigationHandler navHandler = application.getNavigationHandler();
74        // Invoke nav handling..
75        final String navBinding = (null != expression) ? expression.getExpressionString() : null;
76        navHandler.handleNavigation(facesContext, navBinding, errorOutcome);
77        // Trigger a switch to Render Response if needed
78        facesContext.renderResponse();
79      }
80    }
81  
82    public String getErrorOutcome() {
83      return errorOutcome;
84    }
85  
86    public void setErrorOutcome(final String errorOutcome) {
87      this.errorOutcome = errorOutcome;
88    }
89  }