1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.struts.action.ExceptionHandler; 21 import org.apache.struts.action.ActionForward; 22 import org.apache.struts.action.ActionMapping; 23 import org.apache.struts.action.ActionForm; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 import org.apache.struts.config.ExceptionConfig; 27 import org.apache.struts.util.ModuleException; 28 import org.apache.struts.Globals; 29 import org.apache.beehive.netui.pageflow.ExpressionMessage; 30 import org.apache.beehive.netui.pageflow.config.PageFlowExceptionConfig; 31 import org.apache.beehive.netui.util.logging.Logger; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.ServletException ; 36 37 public class PageFlowExceptionHandler 38 extends ExceptionHandler 39 { 40 private static final Logger _log = Logger.getInstance( PageFlowExceptionHandler.class ); 41 42 public ActionForward execute( Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, 43 HttpServletRequest request, HttpServletResponse response ) 44 throws ServletException 45 { 46 ActionForward forward = null; 47 ActionMessage error = null; 48 String property = null; 49 String path = ae.getPath(); 50 51 forward = path != null ? new ActionForward( path ) : mapping.getInputForward(); 53 54 if ( ex instanceof ModuleException ) 56 { 57 error = ( ( ModuleException ) ex ).getError(); 58 property = ( ( ModuleException ) ex ).getProperty(); 59 } 60 else 61 { 62 if ( ae instanceof PageFlowExceptionConfig ) 63 { 64 String expressionMessage = ( ( PageFlowExceptionConfig ) ae ).getDefaultMessage(); 65 if ( expressionMessage != null ) 66 { 67 error = new ExpressionMessage( expressionMessage, new Object []{ ex.getMessage() } ); 68 } 69 } 70 71 if ( error == null ) error = new ActionMessage( ae.getKey(), ex.getMessage() ); 72 property = ae.getKey(); 73 } 74 75 if ( _log.isDebugEnabled() ) _log.debug( "Handling exception", ex ); 76 77 request.setAttribute( Globals.EXCEPTION_KEY, ex ); 79 storeException( request, property, error, forward, ae.getScope() ); 80 81 return forward; 82 } 83 84 protected void storeException( HttpServletRequest request, String property, ActionMessage error, 87 ActionForward forward, String scope ) 88 { 89 ActionMessages errors = new ActionMessages(); 90 errors.add( property, error ); 91 92 if ( "request".equals( scope ) ) 93 { 94 request.setAttribute( Globals.ERROR_KEY, errors ); 95 } 96 else 97 { 98 request.getSession().setAttribute( Globals.ERROR_KEY, errors ); 99 } 100 } 101 } 102 | Popular Tags |