You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ma...@apache.org on 2002/12/10 07:03:21 UTC

cvs commit: jakarta-struts/web/upload display.jsp upload.jsp

martinc     2002/12/09 22:03:21

  Modified:    src/share/org/apache/struts/util RequestUtils.java
               src/upload/org/apache/struts/webapp/upload UploadAction.java
                        UploadForm.java
               web/upload display.jsp upload.jsp
  Log:
  Pick up query string parameters when processing multipart/file-data (i.e.
  file upload) requests. Enhance the struts-upload application to include
  this scenario and indicate successful parameter handling.
  
  PR: 12884
  Submitted by: Serge Huber, Christopher Murray
  
  Revision  Changes    Path
  1.74      +79 -38    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.73
  retrieving revision 1.74
  diff -u -r1.73 -r1.74
  --- RequestUtils.java	24 Nov 2002 21:59:43 -0000	1.73
  +++ RequestUtils.java	10 Dec 2002 06:03:21 -0000	1.74
  @@ -67,6 +67,7 @@
   import java.net.URL;
   import java.net.URLEncoder;
   import java.util.ArrayList;
  +import java.util.Collections;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Hashtable;
  @@ -104,6 +105,7 @@
   import org.apache.struts.config.ModuleConfig;
   import org.apache.struts.taglib.html.Constants;
   import org.apache.struts.upload.MultipartRequestHandler;
  +import org.apache.struts.upload.MultipartRequestWrapper;
   
   /**
    * General purpose utility methods related to processing a servlet request
  @@ -121,7 +123,7 @@
       /**
        * Commons Logging instance.
        */
  -    protected static Log LOG = LogFactory.getLog(RequestUtils.class);
  +    protected static Log log = LogFactory.getLog(RequestUtils.class);
   
       /**
        * The default Locale for our server.
  @@ -538,8 +540,8 @@
           }
   
           // Look up any existing form bean instance
  -        if (LOG.isDebugEnabled()) {
  -            LOG.debug(
  +        if (log.isDebugEnabled()) {
  +            log.debug(
                   " Looking for ActionForm bean instance in scope '"
                       + mapping.getScope()
                       + "' under attribute key '"
  @@ -560,13 +562,13 @@
               if (config.getDynamic()) {
                   String className = ((DynaBean) instance).getDynaClass().getName();
                   if (className.equals(config.getName())) {
  -                    if (LOG.isDebugEnabled()) {
  -                        LOG.debug(
  +                    if (log.isDebugEnabled()) {
  +                        log.debug(
                               " Recycling existing DynaActionForm instance "
                                   + "of type '"
                                   + className
                                   + "'");
  -                        LOG.trace(" --> " + instance);
  +                        log.trace(" --> " + instance);
                       }
                       return (instance);
                   }
  @@ -574,19 +576,19 @@
                   try {
                       Class configClass = applicationClass(config.getType());
                       if (configClass.isAssignableFrom(instance.getClass())) {
  -                        if (LOG.isDebugEnabled()) {
  -                            LOG.debug(
  +                        if (log.isDebugEnabled()) {
  +                            log.debug(
                                   " Recycling existing ActionForm instance "
                                       + "of class '"
                                       + instance.getClass().getName()
                                       + "'");
  -                            LOG.trace(" --> " + instance);
  +                            log.trace(" --> " + instance);
                           }
                           return (instance);
                       }
                       return (instance);
                   } catch (Throwable t) {
  -                    LOG.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
  +                    log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
                       return (null);
                   }
               }
  @@ -598,31 +600,31 @@
                   DynaActionFormClass dynaClass =
                       DynaActionFormClass.createDynaActionFormClass(config);
                   instance = (ActionForm) dynaClass.newInstance();
  -                if (LOG.isDebugEnabled()) {
  -                    LOG.debug(
  +                if (log.isDebugEnabled()) {
  +                    log.debug(
                           " Creating new DynaActionForm instance "
                               + "of type '"
                               + config.getType()
                               + "'");
  -                    LOG.trace(" --> " + instance);
  +                    log.trace(" --> " + instance);
                   }
               } catch (Throwable t) {
  -                LOG.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
  +                log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
                   return (null);
               }
           } else {
               try {
                   instance = (ActionForm) applicationInstance(config.getType());
  -                if (LOG.isDebugEnabled()) {
  -                    LOG.debug(
  +                if (log.isDebugEnabled()) {
  +                    log.debug(
                           " Creating new ActionForm instance "
                               + "of type '"
                               + config.getType()
                               + "'");
  -                    LOG.trace(" --> " + instance);
  +                    log.trace(" --> " + instance);
                   }
               } catch (Throwable t) {
  -                LOG.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
  +                log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
                   return (null);
               }
           }
  @@ -877,8 +879,8 @@
           HashMap properties = new HashMap();
           // Iterator of parameter names
           Enumeration names = null;
  -        // Hashtable for multipart values
  -        Hashtable multipartElements = null;
  +        // Map for multipart parameters
  +        Map multipartParameters = null;
   
           String contentType = request.getContentType();
           String method = request.getMethod();
  @@ -927,8 +929,9 @@
                       return;
                   }
                   //retrive form values and put into properties
  -                multipartElements = multipartHandler.getAllElements();
  -                names = multipartElements.keys();
  +                multipartParameters = getAllParametersForMultipartRequest(
  +                        request, multipartHandler);
  +                names = Collections.enumeration(multipartParameters.keySet());
               }
               request.removeAttribute(Globals.MAPPING_KEY);
           }
  @@ -951,7 +954,7 @@
                   stripped = stripped.substring(0, stripped.length() - suffix.length());
               }
               if (isMultipart) {
  -                properties.put(stripped, multipartElements.get(name));
  +                properties.put(stripped, multipartParameters.get(name));
               } else {
                   properties.put(stripped, request.getParameterValues(name));
               }
  @@ -991,13 +994,13 @@
               try {
                   multipartHandler = (MultipartRequestHandler) applicationInstance(multipartClass);
               } catch (ClassNotFoundException cnfe) {
  -                LOG.error(
  +                log.error(
                       "MultipartRequestHandler class \""
                           + multipartClass
                           + "\" in mapping class not found, "
                           + "defaulting to global multipart class");
               } catch (InstantiationException ie) {
  -                LOG.error(
  +                log.error(
                       "InstantiaionException when instantiating "
                           + "MultipartRequestHandler \""
                           + multipartClass
  @@ -1005,7 +1008,7 @@
                           + "defaulting to global multipart class, exception: "
                           + ie.getMessage());
               } catch (IllegalAccessException iae) {
  -                LOG.error(
  +                log.error(
                       "IllegalAccessException when instantiating "
                           + "MultipartRequestHandler \""
                           + multipartClass
  @@ -1056,6 +1059,44 @@
       }
   
       /**
  +     * Create a map containing all of the parameters supplied for a multipart
  +     * request, keyed by parameter name. In addition to text and file elements
  +     * from the multipart body, query string parameters are included as well.
  +     *
  +     * @param request The (wrapped) HTTP request whose parameters are to be
  +     *                added to the map.
  +     * @param multipartHandler The multipart handler used to parse the request.
  +     *
  +     * @return the map containing all parameters for this multipart request.
  +     */
  +    private static Map getAllParametersForMultipartRequest(
  +            HttpServletRequest request,
  +            MultipartRequestHandler multipartHandler) {
  +        Map parameters = new HashMap();
  +        Enumeration enum;
  +
  +        Hashtable elements = multipartHandler.getAllElements();
  +        enum = elements.keys();
  +        while (enum.hasMoreElements()) {
  +            String key = (String) enum.nextElement();
  +            parameters.put(key, elements.get(key));
  +        }
  +
  +        if (request instanceof MultipartRequestWrapper) {
  +            request = ((MultipartRequestWrapper)request).getRequest();
  +            enum = request.getParameterNames();
  +            while (enum.hasMoreElements()) {
  +                String key = (String) enum.nextElement();
  +                parameters.put(key, request.getParameterValues(key));
  +            }
  +        } else {
  +            log.debug("Gathering multipart parameters for unwrapped request");
  +        }
  +
  +        return parameters;
  +    }
  +
  +    /**
        * Return true if a message string for the specified message key
        * is present for the specified Locale.
        *
  @@ -1455,8 +1496,8 @@
               matchPath = request.getServletPath();
           }
   
  -        if (LOG.isDebugEnabled()) {
  -            LOG.debug("Get module name for path " + matchPath);
  +        if (log.isDebugEnabled()) {
  +            log.debug("Get module name for path " + matchPath);
           }
   
           String prefix = ""; // Initialize prefix before we try lookup
  @@ -1477,8 +1518,8 @@
               }
           }
   
  -        if (LOG.isDebugEnabled()) {
  -            LOG.debug("Module name found: " + (prefix.equals("") ? "default" : prefix));
  +        if (log.isDebugEnabled()) {
  +            log.debug("Module name found: " + (prefix.equals("") ? "default" : prefix));
           }
           return prefix;
       }
  @@ -1635,7 +1676,7 @@
           } catch (JspException e) {
               throw e;
           } catch (Exception e) {
  -            LOG.debug(e, e);
  +            log.debug(e, e);
           }
   
           return errors;
  @@ -1662,11 +1703,11 @@
               encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" });
   
           } catch (IllegalAccessException e) {
  -            LOG.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
  +            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
           } catch (InvocationTargetException e) {
  -            LOG.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
  +            log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
           } catch (NoSuchMethodException e) {
  -            LOG.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
  +            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
           }
   
           return encodedURL;
  
  
  
  1.7       +8 -4      jakarta-struts/src/upload/org/apache/struts/webapp/upload/UploadAction.java
  
  Index: UploadAction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/upload/org/apache/struts/webapp/upload/UploadAction.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- UploadAction.java	11 Mar 2002 06:13:13 -0000	1.6
  +++ UploadAction.java	10 Dec 2002 06:03:21 -0000	1.7
  @@ -114,6 +114,9 @@
               //retrieve the text data
               String text = theForm.getTheText();
   
  +            //retrieve the query string value
  +            String queryValue = theForm.getQueryParam();
  +
               //retrieve the file representation
               FormFile file = theForm.getTheFile();
   
  @@ -176,6 +179,7 @@
   
               //place the data into the request for retrieval from display.jsp
               request.setAttribute("text", text);
  +            request.setAttribute("queryValue", queryValue);
               request.setAttribute("fileName", fileName);
               request.setAttribute("contentType", contentType);
               request.setAttribute("size", size);
  
  
  
  1.7       +23 -4     jakarta-struts/src/upload/org/apache/struts/webapp/upload/UploadForm.java
  
  Index: UploadForm.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/upload/org/apache/struts/webapp/upload/UploadForm.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- UploadForm.java	16 Nov 2002 06:04:29 -0000	1.6
  +++ UploadForm.java	10 Dec 2002 06:03:21 -0000	1.7
  @@ -94,6 +94,11 @@
       protected String theText;
   
       /**
  +     * The value of the embedded query string parameter
  +     */
  +    protected String queryParam;
  +
  +    /**
        * Whether or not to write to a file
        */
       protected boolean writeFile;
  @@ -122,6 +127,20 @@
        */
       public void setTheText(String theText) {
           this.theText = theText;
  +    }
  +
  +    /**
  +     * Retrieve the value of the query string parameter
  +     */
  +    public String getQueryParam() {
  +        return queryParam;
  +    }
  +
  +    /**
  +     * Set the value of the query string parameter
  +     */
  +    public void setQueryParam(String queryParam) {
  +        this.queryParam = queryParam;
       }
   
       /**
  
  
  
  1.3       +2 -0      jakarta-struts/web/upload/display.jsp
  
  Index: display.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/upload/display.jsp,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- display.jsp	11 Apr 2001 22:56:27 -0000	1.2
  +++ display.jsp	10 Dec 2002 06:03:21 -0000	1.3
  @@ -2,6 +2,8 @@
   
   <b>The Text</b>: <%= request.getAttribute("text") %> <br />
   
  +<b>The Query Parameter</b>: <%= request.getAttribute("queryValue") %> <br />
  +
   <b>The File name</b>: <%= request.getAttribute("fileName") %> <br />
   
   <b>The File content type</b>: <%= request.getAttribute("contentType") %> <br />
  
  
  
  1.5       +1 -1      jakarta-struts/web/upload/upload.jsp
  
  Index: upload.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/upload/upload.jsp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- upload.jsp	6 Mar 2002 19:39:09 -0000	1.4
  +++ upload.jsp	10 Dec 2002 06:03:21 -0000	1.5
  @@ -37,7 +37,7 @@
   	The most important part is to declare your form's enctype to be "multipart/form-data",
   	and to have a form:file element that maps to your ActionForm's FormFile property
   -->
  -<html:form action="upload.do" enctype="multipart/form-data">
  +<html:form action="upload.do?queryParam=Successful" enctype="multipart/form-data">
   
   	Please enter some text, just to demonstrate the handling of text elements as opposed to file elements:<br />
   	<html:text property="theText" /><br /><br />
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>