0,0 → 1,127 |
package ak.hostadmiral.core.action; |
|
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
|
import org.apache.struts.Globals; |
import org.apache.struts.action.ActionError; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.action.ActionMessages; |
import org.apache.struts.action.ExceptionHandler; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.action.ActionForm; |
import org.apache.struts.action.ActionForward; |
import org.apache.struts.config.ExceptionConfig; |
|
import org.apache.log4j.Logger; |
|
import ak.strutsx.ErrorHandlerX; |
|
import ak.hostadmiral.util.UserException; |
import ak.hostadmiral.util.ModelUserException; |
import ak.hostadmiral.util.ModelSecurityException; |
import ak.hostadmiral.util.ModelException; |
import ak.hostadmiral.util.FormException; |
|
public final class GeneralExceptionHandler |
extends ExceptionHandler |
{ |
private static final Logger logger = Logger.getLogger(GeneralExceptionHandler.class); |
|
public ActionForward execute(Exception ex, ExceptionConfig config, |
ActionMapping mapping, ActionForm formInstance, |
HttpServletRequest request, HttpServletResponse response) |
throws ServletException |
{ |
if(ex instanceof UserException) { |
return handleUserException((UserException)ex, |
config, mapping, formInstance, request, response); |
} |
else if(ex instanceof ModelUserException) { |
return handleModelUserException((ModelUserException)ex, |
config, mapping, formInstance, request, response); |
} |
else if(ex instanceof ModelSecurityException) { |
logger.error("security exception", ex); |
Exception subex = ((ModelSecurityException)ex).getChainedException(); |
if(subex != null && subex instanceof ModelException) |
logChainedException((ModelSecurityException)subex); |
|
return mapping.findForward("accessDenied"); |
} |
else if(ex instanceof ModelException) { |
logger.error("model exception", ex); |
Exception subex = ((ModelException)ex).getChainedException(); |
if(subex != null && subex instanceof ModelException) |
logChainedException((ModelException)subex); |
|
return mapping.findForward("generalError"); |
} |
else { |
logger.error("unhandled exception", ex); |
return mapping.findForward("generalError"); |
} |
} |
|
protected void logChainedException(ModelException ex) |
{ |
logger.error("chained exception", ex); |
|
Exception subex = ex.getChainedException(); |
if(subex != null && subex instanceof ModelException) |
logChainedException((ModelException)subex); |
} |
|
protected ActionForward handleUserException(UserException ex, ExceptionConfig config, |
ActionMapping mapping, ActionForm formInstance, |
HttpServletRequest request, HttpServletResponse response) |
throws ServletException |
{ |
logger.info("user exception handle:" + ex.getMessage()); |
|
// try to get property for this exception if any |
String property = ActionMessages.GLOBAL_MESSAGE; |
if(ex instanceof FormException) { |
FormException formEx = (FormException)ex; |
if(formEx.getProperty() != null) |
property = formEx.getProperty(); |
} |
|
// create new error message |
ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY); |
if(errors == null) { |
errors = new ActionErrors(); |
request.setAttribute(Globals.ERROR_KEY, errors); |
} |
errors.add(property, new ActionError(ex.getMessage(), ex.getValues())); |
|
// find forward |
if(mapping.getInput() == null) |
return mapping.findForward("error"); |
else |
return mapping.getInputForward(); |
} |
|
protected ActionForward handleModelUserException(ModelUserException ex, ExceptionConfig config, |
ActionMapping mapping, ActionForm formInstance, |
HttpServletRequest request, HttpServletResponse response) |
throws ServletException |
{ |
logger.info("model user exception handle:" + ex.getMessage()); |
|
// create new error message |
ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY); |
if(errors == null) { |
errors = new ActionErrors(); |
request.setAttribute(Globals.ERROR_KEY, errors); |
} |
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionError(ex.getMessage(), ex.getValues())); |
|
// find forward |
if(mapping.getInput() == null) |
return mapping.findForward("error"); |
else |
return mapping.getInputForward(); |
} |
} |