1 /*****************************************************************************
5 * Creation date 13.12.2012
8 * @copyright OPEN CASCADE 2012-2015
9 *****************************************************************************/
11 package org.splat.simer;
13 import java.util.HashSet;
16 import org.apache.struts2.ServletActionContext;
17 import org.splat.exception.BusinessException;
18 import org.splat.i18n.I18nUtils;
19 import org.splat.log.AppLogger;
21 import com.opensymphony.xwork2.ActionContext;
22 import com.opensymphony.xwork2.ActionSupport;
23 import com.opensymphony.xwork2.util.ValueStack;
26 * The action for exception processing.
28 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
30 public class ExceptionAction extends ActionSupport {
35 private static final long serialVersionUID = 4818006367988105560L;
40 private final static AppLogger LOG = AppLogger
41 .getLogger(ExceptionAction.class);
44 * User interface message.
46 protected String _message;
49 * the unexpected error i18n key.
51 private final static String UNEXPECTED_ERROR_KEY = "message.error.internal";
54 * The name of the param in the http header.
56 private final static String PARAM_X_REQUESTED_WITH = "x-requested-with";
59 * The value of a ajax request.
61 private final static String VALUE_AJAX_REQUEST = "XMLHttpRequest";
64 * Process the exception.
66 * <li>log the exception</li>
67 * <li>display an unexpected error screen. This is configured in the struts config file</li>
70 * @return the struts mapping
72 public String doProcess() {
73 ValueStack valueStack = null;
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());
82 this._message = I18nUtils.getMessage(ActionContext
83 .getContext().getLocale(), bex.getMessageKey(), bex
85 } catch (RuntimeException e) {
86 this._message = e.getMessage();
90 * else if (obj instanceof TechnicalException) { TechnicalException tex = (TechnicalException) obj;
91 * LOG.error(tex.getMessageKey(), tex, tex.getContext());
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,
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);
107 Throwable cause = exc.getCause();
109 LOG.errorMsg(cause.getMessage(), cause);
110 msg.append(" ").append(cause.getMessage());
113 this._message = getText(ExceptionAction.UNEXPECTED_ERROR_KEY);
114 } catch (RuntimeException e) {
115 this._message = e.getMessage();
117 if (LOG.isDebugEnabled()) {
118 this._message += " " + exc.getClass().getSimpleName() + ": " + msg.toString();
119 LOG.debug("ExceptionAction: " + exc.getMessage(), exc);
122 } catch (RuntimeException e) {
123 LOG.errorMsg(e.getMessage(), e);
124 this._message = e.getMessage();
125 Throwable cause = e.getCause();
127 LOG.errorMsg(cause.getMessage(), cause);
128 this._message = cause.getMessage();
132 return getResult(valueStack);
136 * Define the result name: error for ajax request and sub-action, otherwise return success.
139 * struts context value stack
140 * @return action result name
142 protected String getResult(final ValueStack valueStack) {
144 // If subaction or ajaxRequest
145 if (checkIfSubAction(valueStack)
146 || (VALUE_AJAX_REQUEST.equalsIgnoreCase(ServletActionContext
147 .getRequest().getHeader(PARAM_X_REQUESTED_WITH)))) {
156 * Check if the exception is raised inside sub action.
159 * the action value stack
160 * @return true if the exception is raised inside sub action, return false otherwise
162 private boolean checkIfSubAction(final ValueStack valueStack) {
163 Set<ActionSupport> actionSet = new HashSet<ActionSupport>();
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");
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);
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++) {
184 if ((obj instanceof ActionSupport)
185 && !(obj instanceof ExceptionAction)) {
186 actionSet.add((ActionSupport) obj);
195 return (actionSet.size() > 1);
199 * Get the ui message.
201 * @return the message
203 public String getMessage() {
208 * Set the user interface message.
211 * the user interface message
213 public void setMessage(final String aMessage) {