craigmcc    02/03/22 17:38:15

  Modified:    src/share/org/apache/struts/actions SwitchAction.java
               src/share/org/apache/struts/util RequestUtils.java
  Log:
  Correct handling of request attributes and, as Martin suggested, refactor
  the manipulations into a RequestUtils method that others can use as well.
  
  Submitted by: Martin Cooper <martin.cooper at tumbleweed.com>
  
  Revision  Changes    Path
  1.2       +8 -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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SwitchAction.java 21 Mar 2002 01:42:42 -0000      1.1
  +++ SwitchAction.java 23 Mar 2002 01:38:15 -0000      1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java,v 1.1 
2002/03/21 01:42:42 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/03/21 01:42:42 $
  + * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java,v 1.2 
2002/03/23 01:38:15 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/03/23 01:38:15 $
    *
    * ====================================================================
    *
  @@ -74,6 +74,7 @@
   import org.apache.struts.action.ActionMapping;
   import org.apache.struts.config.ApplicationConfig;
   import org.apache.struts.util.MessageResources;
  +import org.apache.struts.util.RequestUtils;
   
   
   /**
  @@ -94,7 +95,7 @@
    * </ul>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2002/03/21 01:42:42 $
  + * @version $Revision: 1.2 $ $Date: 2002/03/23 01:38:15 $
    * @since 1.1
    */
   
  @@ -152,24 +153,14 @@
           }
   
           // Switch to the requested sub-application
  -        ApplicationConfig config = (ApplicationConfig)
  -            getServlet().getServletContext().getAttribute
  -            (Action.APPLICATION_KEY + prefix);
  -        if (config == null) {
  +        RequestUtils.selectApplication(prefix, request,
  +                                       getServlet().getServletContext());
  +        if (request.getAttribute(Action.APPLICATION_KEY) == null) {
               String message = messages.getMessage("switch.prefix", prefix);
               log.error(message);
               response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                                  message);
               return (null);
  -        }
  -        request.setAttribute(Action.APPLICATION_KEY + prefix, config);
  -        MessageResources resources = (MessageResources)
  -            getServlet().getServletContext().getAttribute
  -            (Action.MESSAGES_KEY + prefix);
  -        if (resources != null) {
  -            request.setAttribute(Action.MESSAGES_KEY + prefix, resources);
  -        } else {
  -            request.removeAttribute(Action.MESSAGES_KEY + prefix);
           }
   
           // Forward control to the specified application-relative URI
  
  
  
  1.34      +36 -12    
jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
  
  Index: RequestUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- RequestUtils.java 10 Mar 2002 01:23:30 -0000      1.33
  +++ RequestUtils.java 23 Mar 2002 01:38:15 -0000      1.34
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.33 
2002/03/10 01:23:30 craigmcc Exp $
  - * $Revision: 1.33 $
  - * $Date: 2002/03/10 01:23:30 $
  + * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.34 
2002/03/23 01:38:15 craigmcc Exp $
  + * $Revision: 1.34 $
  + * $Date: 2002/03/23 01:38:15 $
    *
    * ====================================================================
    *
  @@ -110,7 +110,7 @@
    *
    * @author Craig R. McClanahan
    * @author Ted Husted
  - * @version $Revision: 1.33 $ $Date: 2002/03/10 01:23:30 $
  + * @version $Revision: 1.34 $ $Date: 2002/03/23 01:38:15 $
    */
   
   public class RequestUtils {
  @@ -1203,6 +1203,37 @@
        * Select the sub-application to which the specified request belongs, and
        * add corresponding request attributes to this request.
        *
  +     * @param prefix The sub-application prefix of the desired sub-application
  +     * @param request The servlet request we are processing
  +     * @param context The ServletContext for this web application
  +     */
  +    public static void selectApplication(String prefix,
  +                                         HttpServletRequest request,
  +                                         ServletContext context) {
  +
  +        // Expose the resources for this sub-application
  +        ApplicationConfig config = (ApplicationConfig)
  +            context.getAttribute(Action.APPLICATION_KEY + prefix);
  +        if (config != null) {
  +            request.setAttribute(Action.APPLICATION_KEY, config);
  +        } else {
  +            request.removeAttribute(Action.APPLICATION_KEY);
  +        }
  +        MessageResources resources = (MessageResources)
  +            context.getAttribute(Action.MESSAGES_KEY + prefix);
  +        if (resources != null) {
  +            request.setAttribute(Action.MESSAGES_KEY, resources);
  +        } else {
  +            request.removeAttribute(Action.MESSAGES_KEY);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Select the sub-application to which the specified request belongs, and
  +     * add corresponding request attributes to this request.
  +     *
        * @param request The servlet request we are processing
        * @param context The ServletContext for this web application
        */
  @@ -1223,14 +1254,7 @@
           }
   
           // Expose the resources for this sub-application
  -        ApplicationConfig config = (ApplicationConfig)
  -            context.getAttribute(Action.APPLICATION_KEY + prefix);
  -        if (config != null)
  -            request.setAttribute(Action.APPLICATION_KEY, config);
  -        MessageResources resources = (MessageResources)
  -            context.getAttribute(Action.MESSAGES_KEY + prefix);
  -        if (resources != null)
  -            request.setAttribute(Action.MESSAGES_KEY, resources);
  +        selectApplication(prefix, request, context);
   
       }
   
  
  
  

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

Reply via email to