1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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(ActionListener base) {
46 this.base = base;
47 }
48
49 public void processAction(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 FacesContext facesContext = FacesContext.getCurrentInstance();
64 if (e.getCause() != null) {
65 FacesMessage facesMessage = new FacesMessage(e.getCause().toString());
66 facesContext.addMessage(null, facesMessage);
67 }
68 UIComponent source = event.getComponent();
69 ActionSource2 actionSource = (ActionSource2) source;
70 Application application = facesContext.getApplication();
71 MethodExpression expression = actionSource.getActionExpression();
72
73 NavigationHandler navHandler = application.getNavigationHandler();
74
75 String navBinding = (null != expression) ? expression.getExpressionString() : null;
76 navHandler.handleNavigation(facesContext, navBinding, errorOutcome);
77
78 facesContext.renderResponse();
79 }
80 }
81
82 public String getErrorOutcome() {
83 return errorOutcome;
84 }
85
86 public void setErrorOutcome(String errorOutcome) {
87 this.errorOutcome = errorOutcome;
88 }
89 }