dgraham     2003/07/11 16:47:58

  Modified:    src/share/org/apache/struts/actions IncludeAction.java
                        ForwardAction.java DispatchAction.java
                        SwitchAction.java
  Log:
  Throw exceptions from execute() method instead of sending a failure
  response to the client.  This allows the exception handling mechanism to
  respond to the error gracefully.  PR# 16774.
  
  Revision  Changes    Path
  1.5       +18 -23    
jakarta-struts/src/share/org/apache/struts/actions/IncludeAction.java
  
  Index: IncludeAction.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/IncludeAction.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- IncludeAction.java        22 Sep 2002 05:58:46 -0000      1.4
  +++ IncludeAction.java        11 Jul 2003 23:47:57 -0000      1.5
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,14 +58,14 @@
    * <http://www.apache.org/>.
    *
    */
  -
  -
  + 
   package org.apache.struts.actions;
   
  -
   import javax.servlet.RequestDispatcher;
  +import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  +
   import org.apache.struts.action.Action;
   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionForward;
  @@ -73,7 +73,6 @@
   import org.apache.struts.upload.MultipartRequestWrapper;
   import org.apache.struts.util.MessageResources;
   
  -
   /**
    * <p>An <strong>Action</strong> that includes the context-relative
    * URI specified by the <code>parameter</code> property of our associated
  @@ -100,7 +99,6 @@
    * @author Craig R. McClanahan
    * @version $Revision$ $Date$
    */
  -
   public class IncludeAction extends Action {
   
   
  @@ -132,27 +130,24 @@
        *
        * @exception Exception if an error occurs
        */
  -    public ActionForward execute(ActionMapping mapping,
  -                              ActionForm form,
  -                              HttpServletRequest request,
  -                              HttpServletResponse response)
  -     throws Exception {
  +    public ActionForward execute(
  +        ActionMapping mapping,
  +        ActionForm form,
  +        HttpServletRequest request,
  +        HttpServletResponse response)
  +        throws Exception {
   
           // Create a RequestDispatcher the corresponding resource
           String path = mapping.getParameter();
           if (path == null) {
  -            response.sendError
  -                (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                 messages.getMessage("include.path"));
  -            return (null);
  +            throw new ServletException(messages.getMessage("include.path"));
           }
  +
           RequestDispatcher rd =
               servlet.getServletContext().getRequestDispatcher(path);
  +
           if (rd == null) {
  -            response.sendError
  -                (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                 messages.getMessage("include.rd", path));
  -            return (null);
  +            throw new ServletException(messages.getMessage("include.rd", path));
           }
   
           // Unwrap the multipart request, if there is one.
  
  
  
  1.8       +14 -18    
jakarta-struts/src/share/org/apache/struts/actions/ForwardAction.java
  
  Index: ForwardAction.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/ForwardAction.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ForwardAction.java        7 Mar 2003 05:00:20 -0000       1.7
  +++ ForwardAction.java        11 Jul 2003 23:47:57 -0000      1.8
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -59,19 +59,18 @@
    *
    */
   
  -
   package org.apache.struts.actions;
   
  -
  +import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  +
   import org.apache.struts.action.Action;
   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionForward;
   import org.apache.struts.action.ActionMapping;
   import org.apache.struts.util.MessageResources;
   
  -
   /**
    * <p>An <strong>Action</strong> that forwards to the context-relative
    * URI specified by the <code>parameter</code> property of our associated
  @@ -98,7 +97,6 @@
    * @author Craig R. McClanahan
    * @version $Revision$ $Date$
    */
  -
   public class ForwardAction extends Action {
   
   
  @@ -130,20 +128,18 @@
        *
        * @exception Exception if an error occurs
        */
  -    public ActionForward execute(ActionMapping mapping,
  -                              ActionForm form,
  -                              HttpServletRequest request,
  -                              HttpServletResponse response)
  -     throws Exception {
  +    public ActionForward execute(
  +        ActionMapping mapping,
  +        ActionForm form,
  +        HttpServletRequest request,
  +        HttpServletResponse response)
  +        throws Exception {
   
           // Create a RequestDispatcher the corresponding resource
           String path = mapping.getParameter();
   
           if (path == null) {
  -            response.sendError
  -                (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                 messages.getMessage("forward.path"));
  -            return (null);
  +            throw new ServletException(messages.getMessage("forward.path"));
           }
   
           // Let the controller handle the request
  
  
  
  1.16      +32 -38    
jakarta-struts/src/share/org/apache/struts/actions/DispatchAction.java
  
  Index: DispatchAction.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/DispatchAction.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- DispatchAction.java       10 Jul 2003 02:59:06 -0000      1.15
  +++ DispatchAction.java       11 Jul 2003 23:47:57 -0000      1.16
  @@ -65,6 +65,7 @@
   import java.lang.reflect.Method;
   import java.util.HashMap;
   
  +import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   
  @@ -203,10 +204,10 @@
           if (parameter == null) {
               String message =
                   messages.getMessage("dispatch.handler", mapping.getPath());
  +                
               log.error(message);
  -            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                               message);
  -            return (null);
  +            
  +            throw new ServletException(message);
           }
   
           // Identify the method name to be dispatched to.
  @@ -222,23 +223,24 @@
        * Method which is dispatched to when there is no value for specified
        * request parameter included in the request.  Subclasses of
        * <code>DispatchAction</code> should override this method if they wish
  -     * to provide default behavior different than producing an HTTP
  -     * "Bad Request" error.
  -     *
  +     * to provide default behavior different than throwing a ServletException.
        */
  -    protected ActionForward unspecified(ActionMapping mapping,
  -                                     ActionForm form,
  -                                     HttpServletRequest request,
  -                                     HttpServletResponse response)
  +    protected ActionForward unspecified(
  +        ActionMapping mapping,
  +        ActionForm form,
  +        HttpServletRequest request,
  +        HttpServletResponse response)
           throws Exception {
   
           String message =
  -            messages.getMessage("dispatch.parameter", mapping.getPath(),
  -                                mapping.getParameter());
  +            messages.getMessage(
  +                "dispatch.parameter",
  +                mapping.getPath(),
  +                mapping.getParameter());
  +
           log.error(message);
  -        response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
  -        return (null);
   
  +        throw new ServletException(message);
       }
   
   
  @@ -265,36 +267,31 @@
           Method method = null;
           try {
               method = getMethod(name);
  +            
           } catch (NoSuchMethodException e) {
               String message =
  -                messages.getMessage("dispatch.method", mapping.getPath(),
  -                                    name);
  +                messages.getMessage("dispatch.method", mapping.getPath(), name);
               log.error(message, e);
  -            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                               message);
  -            return (null);
  +            throw e;
           }
   
           ActionForward forward = null;
           try {
               Object args[] = { mapping, form, request, response };
               forward = (ActionForward) method.invoke(this, args);
  +            
           } catch (ClassCastException e) {
               String message =
  -                messages.getMessage("dispatch.return", mapping.getPath(),
  -                                    name);
  +                messages.getMessage("dispatch.return", mapping.getPath(), name);    
            
               log.error(message, e);
  -            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                               message);
  -            return (null);
  +            throw e;
  +            
           } catch (IllegalAccessException e) {
               String message =
  -                messages.getMessage("dispatch.error", mapping.getPath(),
  -                                    name);
  +                messages.getMessage("dispatch.error", mapping.getPath(), name);
               log.error(message, e);
  -            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
  -                               message);
  -            return (null);
  +            throw e;
  +            
           } catch (InvocationTargetException e) {
               // Rethrow the target exception if possible so that the
               // exception handling machinery can deal with it
  @@ -303,12 +300,9 @@
                   throw ((Exception) t);
               } else {
                   String message =
  -                    messages.getMessage("dispatch.error", mapping.getPath(),
  -                                        name);
  +                    messages.getMessage("dispatch.error", mapping.getPath(), name);
                   log.error(message, e);
  -                response.sendError
  -                    (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
  -                return (null);
  +                throw new ServletException(t);
               }
           }
   
  
  
  
  1.10      +12 -17    
jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java
  
  Index: SwitchAction.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SwitchAction.java 28 Nov 2002 07:12:52 -0000      1.9
  +++ SwitchAction.java 11 Jul 2003 23:47:57 -0000      1.10
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -61,19 +61,19 @@
   
   package org.apache.struts.actions;
   
  -
  +import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.apache.struts.Globals;
   import org.apache.struts.action.Action;
   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionForward;
   import org.apache.struts.action.ActionMapping;
   import org.apache.struts.util.MessageResources;
   import org.apache.struts.util.RequestUtils;
  -import org.apache.struts.Globals;
  -
   
   /**
    * <p>A standard <strong>Action</strong> that switches to a new module
  @@ -96,7 +96,6 @@
    * @version $Revision$ $Date$
    * @since Struts 1.1
    */
  -
   public class SwitchAction extends Action {
   
   
  @@ -145,20 +144,16 @@
           if ((page == null) || (prefix == null)) {
               String message = messages.getMessage("switch.required");
               log.error(message);
  -            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
  -                               message);
  -            return (null);
  +            throw new ServletException(message);
           }
   
           // Switch to the requested module
  -        RequestUtils.selectModule(prefix, request,
  -                                       getServlet().getServletContext());
  +        RequestUtils.selectModule(prefix, request, 
getServlet().getServletContext());
  +        
           if (request.getAttribute(Globals.MODULE_KEY) == null) {
               String message = messages.getMessage("switch.prefix", prefix);
               log.error(message);
  -            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
  -                               message);
  -            return (null);
  +            throw new ServletException(message);
           }
   
           // Forward control to the specified module-relative URI
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to