Salome HOME
d9a54e56b2673615f4e55317a03f45595d4aea6c
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ExceptionAction.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   13.12.2012
6  * @author         $Author$
7  * @version        $Revision$
8  * @copyright      OPEN CASCADE 2012-2014
9  *****************************************************************************/
10
11 package org.splat.simer;
12
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import org.apache.struts2.ServletActionContext;
17 import org.splat.exception.BusinessException;
18 import org.splat.i18n.I18nUtils;
19 import org.splat.log.AppLogger;
20
21 import com.opensymphony.xwork2.ActionContext;
22 import com.opensymphony.xwork2.ActionSupport;
23 import com.opensymphony.xwork2.util.ValueStack;
24
25 /**
26  * The action for exception processing.
27  * 
28  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
29  */
30 public class ExceptionAction extends ActionSupport {
31
32         /**
33          * The serial id.
34          */
35         private static final long serialVersionUID = 4818006367988105560L;
36
37         /**
38          * The action logger.
39          */
40         private final static AppLogger LOG = AppLogger
41                         .getLogger(ExceptionAction.class);
42
43         /**
44          * User interface message.
45          */
46         protected String _message;
47
48         /**
49          * the unexpected error i18n key.
50          */
51         private final static String UNEXPECTED_ERROR_KEY = "message.error.internal";
52
53         /**
54          * The name of the param in the http header.
55          */
56         private final static String PARAM_X_REQUESTED_WITH = "x-requested-with";
57
58         /**
59          * The value of a ajax request.
60          */
61         private final static String VALUE_AJAX_REQUEST = "XMLHttpRequest";
62
63         /**
64          * Process the exception.
65          * <ul>
66          * <li>log the exception</li>
67          * <li>display an unexpected error screen. This is configured in the struts config file</li>
68          * </ul>
69          * 
70          * @return the struts mapping
71          */
72         public String doProcess() {
73                 ValueStack valueStack = null;
74                 try {
75                         valueStack = ActionContext.getContext().getValueStack();
76                         Object obj = valueStack.findValue("exception");
77                         if (obj instanceof BusinessException) {
78                                 BusinessException bex = (BusinessException) obj;
79                                 LOG.error(bex.getMessageKey(), bex, bex.getContext());
80
81                                 try {
82                                         this._message = I18nUtils.getMessage(ActionContext
83                                                         .getContext().getLocale(), bex.getMessageKey(), bex
84                                                         .getContext());
85                                 } catch (RuntimeException e) {
86                                         this._message = e.getMessage();
87                                 }
88
89                         }/*
90                                  * else if (obj instanceof TechnicalException) { TechnicalException tex = (TechnicalException) obj;
91                                  * LOG.error(tex.getMessageKey(), tex, tex.getContext());
92                                  * 
93                                  * try { this._uiMessage = I18nUtils.getMessage(ActionContext.getContext().getLocale(),tex.getMessageKey(),
94                                  * tex.getContext()); } catch (RuntimeException e) { this._uiMessage = e.getMessage(); } } else if (obj instanceof
95                                  * DAORequestTimeoutException) { this._uiMessage =
96                                  * I18nUtils.getMessage(ActionContext.getContext().getLocale(),"err.a.request.timeout"); LOG.errorMsg(this._uiMessage,
97                                  * (Exception)obj); }
98                                  */
99                         else {
100
101                                 Throwable exc = (Throwable) obj;
102                                 StringBuffer msg = new StringBuffer();
103                                 if (exc.getMessage() != null) {
104                                         msg.append(exc.getMessage());
105                                         LOG.errorMsg(exc.getMessage(), exc);
106                                 }
107                                 Throwable cause = exc.getCause();
108                                 if (null != cause) {
109                                         LOG.errorMsg(cause.getMessage(), cause);
110                                         msg.append(" ").append(cause.getMessage());
111                                 }
112                                 try {
113                                         this._message = getText(ExceptionAction.UNEXPECTED_ERROR_KEY);
114                                 } catch (RuntimeException e) {
115                                         this._message = e.getMessage();
116                                 }
117                                 if (LOG.isDebugEnabled()) {
118                                         this._message += " " + exc.getClass().getSimpleName() + ": " + msg.toString();
119                                         LOG.debug("ExceptionAction: " + exc.getMessage(), exc);
120                                 }
121                         }
122                 } catch (RuntimeException e) {
123                         LOG.errorMsg(e.getMessage(), e);
124                         this._message = e.getMessage();
125                         Throwable cause = e.getCause();
126                         if (null != cause) {
127                                 LOG.errorMsg(cause.getMessage(), cause);
128                                 this._message = cause.getMessage();
129                         }
130                 }
131
132                 return getResult(valueStack);
133         }
134
135         /**
136          * Define the result name: error for ajax request and sub-action, otherwise return success.
137          * 
138          * @param valueStack
139          *            struts context value stack
140          * @return action result name
141          */
142         protected String getResult(final ValueStack valueStack) {
143                 String result;
144                 // If subaction or ajaxRequest
145                 if (checkIfSubAction(valueStack)
146                                 || (VALUE_AJAX_REQUEST.equalsIgnoreCase(ServletActionContext
147                                                 .getRequest().getHeader(PARAM_X_REQUESTED_WITH)))) {
148                         result = ERROR;
149                 } else {
150                         result = SUCCESS;
151                 }
152                 return result;
153         }
154
155         /**
156          * Check if the exception is raised inside sub action.
157          * 
158          * @param valueStack
159          *            the action value stack
160          * @return true if the exception is raised inside sub action, return false otherwise
161          */
162         private boolean checkIfSubAction(final ValueStack valueStack) {
163                 Set<ActionSupport> actionSet = new HashSet<ActionSupport>();
164
165                 if (valueStack != null && valueStack.getContext() != null) {
166                         // checks if the action is a chainActionResult. In that case, return false
167                         Object test = valueStack.getContext().get("CHAIN_HISTORY");
168
169                         if (test == null) {
170                                 int nValueStackSize = valueStack.size();
171                                 for (int i = 0; i < nValueStackSize; i++) {
172                                         String key = "[" + i + "]";
173                                         Object obj = valueStack.findValue(key);
174                                         if (obj instanceof ActionSupport
175                                                         && !(obj instanceof ExceptionAction)) {
176                                                 actionSet.add((ActionSupport) obj);
177                                         }
178
179                                         if (obj instanceof com.opensymphony.xwork2.util.CompoundRoot) {
180                                                 com.opensymphony.xwork2.util.CompoundRoot root = (com.opensymphony.xwork2.util.CompoundRoot) obj;
181                                                 int nSize = root.size();
182                                                 for (int j = 0; j < nSize; j++) {
183                                                         obj = root.get(j);
184                                                         if ((obj instanceof ActionSupport)
185                                                                         && !(obj instanceof ExceptionAction)) {
186                                                                 actionSet.add((ActionSupport) obj);
187                                                         }
188                                                 }
189                                         }
190                                 }
191                         } else {
192                                 actionSet.clear();
193                         }
194                 }
195                 return (actionSet.size() > 1);
196         }
197
198         /**
199          * Get the ui message.
200          * 
201          * @return the message
202          */
203         public String getMessage() {
204                 return _message;
205         }
206
207         /**
208          * Set the user interface message.
209          * 
210          * @param aMessage
211          *            the user interface message
212          */
213         public void setMessage(final String aMessage) {
214                 _message = aMessage;
215         }
216
217 }