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  /**
38   * @deprecated use JSF 2 exception handler
39   */
40  @Deprecated
41  public class ActionListenerImpl implements ActionListener {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(ActionListenerImpl.class);
44  
45    private ActionListener base;
46  
47    private String errorOutcome = "error";
48  
49    public ActionListenerImpl(final ActionListener base) {
50      this.base = base;
51    }
52  
53    public void processAction(final ActionEvent event) throws AbortProcessingException {
54      try {
55        base.processAction(event);
56      } catch (final Throwable e) {
57        if (e instanceof FacesException) {
58          Throwable fe = e;
59          while (fe != null) {
60            if (fe instanceof AbortProcessingException) {
61              throw (FacesException) e;
62            }
63            fe = fe.getCause();
64          }
65        }
66        final FacesContext facesContext = FacesContext.getCurrentInstance();
67        String errorLogMsg = "Processing failed. Forwarding to error page. errorOutcome=" + errorOutcome;
68        if (e.getCause() != null) {
69          LOG.error(errorLogMsg, e.getCause());
70          final FacesMessage facesMessage = new FacesMessage(e.getCause().toString());
71          facesContext.addMessage(null, facesMessage);
72        } else {
73          LOG.error(errorLogMsg, e);
74        }
75        final UIComponent source = event.getComponent();
76        final ActionSource2 actionSource = (ActionSource2) source;
77        final Application application = facesContext.getApplication();
78        final MethodExpression expression = actionSource.getActionExpression();
79        // Retrieve the NavigationHandler instance..
80        final NavigationHandler navHandler = application.getNavigationHandler();
81        // Invoke nav handling..
82        final String navBinding = (null != expression) ? expression.getExpressionString() : null;
83        navHandler.handleNavigation(facesContext, navBinding, errorOutcome);
84        // Trigger a switch to Render Response if needed
85        facesContext.renderResponse();
86      }
87    }
88  
89    public String getErrorOutcome() {
90      return errorOutcome;
91    }
92  
93    public void setErrorOutcome(final String errorOutcome) {
94      this.errorOutcome = errorOutcome;
95    }
96  }