Author: jleroux
Date: Sun Dec  2 10:36:09 2018
New Revision: 1847955

URL: http://svn.apache.org/viewvc?rev=1847955&view=rev
Log:
"Applied fix from trunk for revision: 1847478" 
------------------------------------------------------------------------
r1847478 | jleroux | 2018-11-26 17:41:52 +0100 (lun. 26 nov. 2018) | 21 lignes

Improved: Multi Part Input Parameters not Available in Groovy Event
(OFBIZ-5048)

If a form is of type enctype="multipart/form-data" and we are handling its 
submission through Groovy Event then in the parameters Map Ofbiz does not set 
the multipart input parameters from request parameters.

The same are available when multipart form submission is handled through 
service.

The reason being the code that sets the multipart parameters in request 
attribute is only available in ServiceEventHandler.java and in 
GroovyEventHandler the multipart are never set.

So I have created a method getMultiPartParameterMap in the class UtilHttp.java 
and put the common logic in that method so that when getCombinedMap method is 
called from the GroovEventHandler the method also call getMultiPartParameterMap 
and in the ServiceEventHandler I have written a call for 
getMultiPartParameterMap method.

Thanks: Vikramjit Singh for report and initial patch, Rohit Koushal for updated
and revised patch with some minor changes.
------------------------------------------------------------------------

Modified:
    ofbiz/ofbiz-framework/branches/release17.12/   (props changed)
    
ofbiz/ofbiz-framework/branches/release17.12/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
    
ofbiz/ofbiz-framework/branches/release17.12/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java

Propchange: ofbiz/ofbiz-framework/branches/release17.12/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sun Dec  2 10:36:09 2018
@@ -10,4 +10,4 @@
 /ofbiz/branches/json-integration-refactoring:1634077-1635900
 /ofbiz/branches/multitenant20100310:921280-927264
 /ofbiz/branches/release13.07:1547657
-/ofbiz/ofbiz-framework/trunk:1819499,1819598,1819800,1819805,1819811,1820038,1820262,1820374-1820375,1820441,1820457,1820644,1820658,1820790,1820823,1820949,1820966,1821012,1821036,1821112,1821115,1821144,1821186,1821219,1821226,1821230,1821386,1821613,1821628,1821965,1822125,1822310,1822377,1822383,1822393,1823467,1823562,1823876,1824314,1824316,1824732,1824803,1824847,1824855,1825192,1825211,1825216,1825233,1825450,1826374,1826502,1826592,1826671,1826674,1826805,1826938,1826997,1827439,1828255,1828316,1828346,1828424,1828512,1828514,1829690,1830936,1831074,1831078,1831234,1831608,1831831,1832577,1832662,1832756,1832800,1832944,1833173,1833211,1834181,1834191,1834736,1835235,1835887,1835891,1835953,1835964,1836144,1836871,1837857,1838032,1838256,1838381,1840189,1840199,1840828,1841657,1841662,1842372,1842921,1843225,1843893,1844943,1845418,1845420,1845466,1845544,1845552,1845558,1845933,1845995,1846097,1846107,1846214,1846594,1846632,1847398,1847670,1847715,1847890
+/ofbiz/ofbiz-framework/trunk:1819499,1819598,1819800,1819805,1819811,1820038,1820262,1820374-1820375,1820441,1820457,1820644,1820658,1820790,1820823,1820949,1820966,1821012,1821036,1821112,1821115,1821144,1821186,1821219,1821226,1821230,1821386,1821613,1821628,1821965,1822125,1822310,1822377,1822383,1822393,1823467,1823562,1823876,1824314,1824316,1824732,1824803,1824847,1824855,1825192,1825211,1825216,1825233,1825450,1826374,1826502,1826592,1826671,1826674,1826805,1826938,1826997,1827439,1828255,1828316,1828346,1828424,1828512,1828514,1829690,1830936,1831074,1831078,1831234,1831608,1831831,1832577,1832662,1832756,1832800,1832944,1833173,1833211,1834181,1834191,1834736,1835235,1835887,1835891,1835953,1835964,1836144,1836871,1837857,1838032,1838256,1838381,1840189,1840199,1840828,1841657,1841662,1842372,1842921,1843225,1843893,1844943,1845418,1845420,1845466,1845544,1845552,1845558,1845933,1845995,1846097,1846107,1846214,1846594,1846632,1847398,1847478,1847670,1847715,1847890

Modified: 
ofbiz/ofbiz-framework/branches/release17.12/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/branches/release17.12/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java?rev=1847955&r1=1847954&r2=1847955&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/branches/release17.12/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
 (original)
+++ 
ofbiz/ofbiz-framework/branches/release17.12/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
 Sun Dec  2 10:36:09 2018
@@ -18,9 +18,12 @@
  
*******************************************************************************/
 package org.apache.ofbiz.base.util;
 
+import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -49,6 +52,10 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.http.conn.ssl.NoopHostnameVerifier;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@@ -56,8 +63,10 @@ import org.apache.http.conn.ssl.TrustSel
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.ssl.SSLContexts;
+import org.apache.ofbiz.entity.Delegator;
 import org.apache.ofbiz.entity.util.EntityUtilProperties;
 import org.apache.ofbiz.webapp.control.ConfigXMLReader;
+import org.apache.ofbiz.webapp.event.FileUploadProgressListener;
 import org.apache.ofbiz.widget.renderer.VisualTheme;
 import org.apache.oro.text.regex.MalformedPatternException;
 import org.apache.oro.text.regex.Pattern;
@@ -154,7 +163,7 @@ public final class UtilHttp {
 
         if (paramMap.size() == 0) {
             // nothing found in the parameters; maybe we read the stream 
instead
-            Map<String, Object> multiPartMap = 
UtilGenerics.checkMap(request.getAttribute("multiPartMap"));
+            Map<String, Object> multiPartMap = 
getMultiPartParameterMap(request);
             if (UtilValidate.isNotEmpty(multiPartMap)) {
                 paramMap.putAll(multiPartMap);
             }
@@ -167,6 +176,112 @@ public final class UtilHttp {
         return canonicalizeParameterMap(paramMap);
     }
 
+    public static Map<String, Object> 
getMultiPartParameterMap(HttpServletRequest request) {
+        Map<String, Object> multiPartMap = new HashMap<String, Object>();
+        Delegator delegator = (Delegator) request.getAttribute("delegator");
+        HttpSession session = request.getSession();
+        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
+        if (isMultiPart) {
+            // get the http upload configuration
+            String maxSizeStr = 
EntityUtilProperties.getPropertyValue("general", "http.upload.max.size", "-1", 
delegator);
+            long maxUploadSize = -1;
+            try {
+                maxUploadSize = Long.parseLong(maxSizeStr);
+            } catch (NumberFormatException e) {
+                Debug.logError(e, "Unable to obtain the max upload size from 
general.properties; using default -1", module);
+                maxUploadSize = -1;
+            }
+            // get the http size threshold configuration - files bigger than 
this will be
+            // temporarly stored on disk during upload
+            String sizeThresholdStr = 
EntityUtilProperties.getPropertyValue("general", 
"http.upload.max.sizethreshold", "10240", delegator);
+            int sizeThreshold = 10240; // 10K
+            try {
+                sizeThreshold = Integer.parseInt(sizeThresholdStr);
+            } catch (NumberFormatException e) {
+                Debug.logError(e, "Unable to obtain the threshold size from 
general.properties; using default 10K", module);
+                sizeThreshold = -1;
+            }
+            // directory used to temporarily store files that are larger than 
the configured size threshold
+            String tmpUploadRepository = 
EntityUtilProperties.getPropertyValue("general", "http.upload.tmprepository", 
"runtime/tmp", delegator);
+            String encoding = request.getCharacterEncoding();
+            // check for multipart content types which may have uploaded items
+
+            ServletFileUpload upload = new ServletFileUpload(new 
DiskFileItemFactory(sizeThreshold, new File(tmpUploadRepository)));
+
+            // create the progress listener and add it to the session
+            FileUploadProgressListener listener = new 
FileUploadProgressListener();
+            upload.setProgressListener(listener);
+            session.setAttribute("uploadProgressListener", listener);
+
+            if (encoding != null) {
+                upload.setHeaderEncoding(encoding);
+            }
+            upload.setSizeMax(maxUploadSize);
+
+            List<FileItem> uploadedItems = null;
+            try {
+                uploadedItems = 
UtilGenerics.<FileItem>checkList(upload.parseRequest(request));
+            } catch (FileUploadException e) {
+                Debug.logError("File upload error" + e, module);
+            }
+            if (uploadedItems != null) {
+                for (FileItem item: uploadedItems) {
+                    String fieldName = item.getFieldName();
+                    //byte[] itemBytes = item.get();
+                    /*
+                    Debug.logInfo("Item Info [" + fieldName + "] : " + 
item.getName() + " / " + item.getSize() + " / " +
+                            item.getContentType() + " FF: " + 
item.isFormField(), module);
+                    */
+                    if (item.isFormField() || item.getName() == null) {
+                        if (multiPartMap.containsKey(fieldName)) {
+                            Object mapValue = multiPartMap.get(fieldName);
+                            if (mapValue instanceof List<?>) {
+                                checkList(mapValue, 
Object.class).add(item.getString());
+                            } else if (mapValue instanceof String) {
+                                List<String> newList = new 
LinkedList<String>();
+                                newList.add((String) mapValue);
+                                newList.add(item.getString());
+                                multiPartMap.put(fieldName, newList);
+                            } else {
+                                Debug.logWarning("Form field found [" + 
fieldName + "] which was not handled!", module);
+                            }
+                        } else {
+                            if (encoding != null) {
+                                try {
+                                    multiPartMap.put(fieldName, 
item.getString(encoding));
+                                } catch (java.io.UnsupportedEncodingException 
uee) {
+                                    Debug.logError(uee, "Unsupported Encoding, 
using deafault", module);
+                                    multiPartMap.put(fieldName, 
item.getString());
+                                }
+                            } else {
+                                multiPartMap.put(fieldName, item.getString());
+                            }
+                        }
+                    } else {
+                        String fileName = item.getName();
+                        if (fileName.indexOf('\\') > -1 || 
fileName.indexOf('/') > -1) {
+                            // get just the file name IE and other browsers 
also pass in the local path
+                            int lastIndex = fileName.lastIndexOf('\\');
+                            if (lastIndex == -1) {
+                                lastIndex = fileName.lastIndexOf('/');
+                            }
+                            if (lastIndex > -1) {
+                                fileName = fileName.substring(lastIndex + 1);
+                            }
+                        }
+                        multiPartMap.put(fieldName, 
ByteBuffer.wrap(item.get()));
+                        multiPartMap.put("_" + fieldName + "_size", 
item.getSize());
+                        multiPartMap.put("_" + fieldName + "_fileName", 
fileName);
+                        multiPartMap.put("_" + fieldName + "_contentType", 
item.getContentType());
+                    }
+                }
+            }
+        }
+
+
+        return multiPartMap;
+    }
+
     public static Map<String, Object> getQueryStringOnlyParameterMap(String 
queryString) {
         Map<String, Object> paramMap = new HashMap<>();
         if (UtilValidate.isNotEmpty(queryString)) {

Modified: 
ofbiz/ofbiz-framework/branches/release17.12/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/branches/release17.12/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java?rev=1847955&r1=1847954&r2=1847955&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/branches/release17.12/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
 (original)
+++ 
ofbiz/ofbiz-framework/branches/release17.12/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java
 Sun Dec  2 10:36:09 2018
@@ -35,10 +35,6 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.UtilGenerics;
 import org.apache.ofbiz.base.util.UtilHttp;
@@ -131,108 +127,6 @@ public class ServiceEventHandler impleme
             Debug.logVerbose("[Using delegator]: " + 
dispatcher.getDelegator().getDelegatorName(), module);
         }
 
-        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
-        Map<String, Object> multiPartMap = new HashMap<String, Object>();
-        if (isMultiPart) {
-            // get the http upload configuration
-            String maxSizeStr = 
EntityUtilProperties.getPropertyValue("general", "http.upload.max.size", "-1", 
dctx.getDelegator());
-            long maxUploadSize = -1;
-            try {
-                maxUploadSize = Long.parseLong(maxSizeStr);
-            } catch (NumberFormatException e) {
-                Debug.logError(e, "Unable to obtain the max upload size from 
general.properties; using default -1", module);
-                maxUploadSize = -1;
-            }
-            // get the http size threshold configuration - files bigger than 
this will be
-            // temporarly stored on disk during upload
-            String sizeThresholdStr = 
EntityUtilProperties.getPropertyValue("general", 
"http.upload.max.sizethreshold", "10240", dctx.getDelegator());
-            int sizeThreshold = 10240; // 10K
-            try {
-                sizeThreshold = Integer.parseInt(sizeThresholdStr);
-            } catch (NumberFormatException e) {
-                Debug.logError(e, "Unable to obtain the threshold size from 
general.properties; using default 10K", module);
-                sizeThreshold = -1;
-            }
-            // directory used to temporarily store files that are larger than 
the configured size threshold
-            String tmpUploadRepository = 
EntityUtilProperties.getPropertyValue("general", "http.upload.tmprepository", 
"runtime/tmp", dctx.getDelegator());
-            String encoding = request.getCharacterEncoding();
-            // check for multipart content types which may have uploaded items
-
-            ServletFileUpload upload = new ServletFileUpload(new 
DiskFileItemFactory(sizeThreshold, new File(tmpUploadRepository)));
-
-            // create the progress listener and add it to the session
-            FileUploadProgressListener listener = new 
FileUploadProgressListener();
-            upload.setProgressListener(listener);
-            session.setAttribute("uploadProgressListener", listener);
-
-            if (encoding != null) {
-                upload.setHeaderEncoding(encoding);
-            }
-            upload.setSizeMax(maxUploadSize);
-
-            List<FileItem> uploadedItems = null;
-            try {
-                uploadedItems = 
UtilGenerics.<FileItem>checkList(upload.parseRequest(request));
-            } catch (FileUploadException e) {
-                throw new EventHandlerException("Problems reading uploaded 
data", e);
-            }
-            if (uploadedItems != null) {
-                for (FileItem item: uploadedItems) {
-                    String fieldName = item.getFieldName();
-                    //byte[] itemBytes = item.get();
-                    /*
-                    Debug.logInfo("Item Info [" + fieldName + "] : " + 
item.getName() + " / " + item.getSize() + " / " +
-                            item.getContentType() + " FF: " + 
item.isFormField(), module);
-                    */
-                    if (item.isFormField() || item.getName() == null) {
-                        if (multiPartMap.containsKey(fieldName)) {
-                            Object mapValue = multiPartMap.get(fieldName);
-                            if (mapValue instanceof List<?>) {
-                                checkList(mapValue, 
Object.class).add(item.getString());
-                            } else if (mapValue instanceof String) {
-                                List<String> newList = new 
LinkedList<String>();
-                                newList.add((String) mapValue);
-                                newList.add(item.getString());
-                                multiPartMap.put(fieldName, newList);
-                            } else {
-                                Debug.logWarning("Form field found [" + 
fieldName + "] which was not handled!", module);
-                            }
-                        } else {
-                            if (encoding != null) {
-                                try {
-                                    multiPartMap.put(fieldName, 
item.getString(encoding));
-                                } catch (java.io.UnsupportedEncodingException 
uee) {
-                                    Debug.logError(uee, "Unsupported Encoding, 
using deafault", module);
-                                    multiPartMap.put(fieldName, 
item.getString());
-                                }
-                            } else {
-                                multiPartMap.put(fieldName, item.getString());
-                            }
-                        }
-                    } else {
-                        String fileName = item.getName();
-                        if (fileName.indexOf('\\') > -1 || 
fileName.indexOf('/') > -1) {
-                            // get just the file name IE and other browsers 
also pass in the local path
-                            int lastIndex = fileName.lastIndexOf('\\');
-                            if (lastIndex == -1) {
-                                lastIndex = fileName.lastIndexOf('/');
-                            }
-                            if (lastIndex > -1) {
-                                fileName = fileName.substring(lastIndex + 1);
-                            }
-                        }
-                        multiPartMap.put(fieldName, 
ByteBuffer.wrap(item.get()));
-                        multiPartMap.put("_" + fieldName + "_size", 
Long.valueOf(item.getSize()));
-                        multiPartMap.put("_" + fieldName + "_fileName", 
fileName);
-                        multiPartMap.put("_" + fieldName + "_contentType", 
item.getContentType());
-                    }
-                }
-            }
-        }
-
-        // store the multi-part map as an attribute so we can access the 
parameters
-        request.setAttribute("multiPartMap", multiPartMap);
-
         Map<String, Object> rawParametersMap = 
UtilHttp.getCombinedMap(request);
         Set<String> urlOnlyParameterNames = 
UtilHttp.getUrlOnlyParameterMap(request).keySet();
 
@@ -252,15 +146,15 @@ public class ServiceEventHandler impleme
 
             Object value = null;
             if (UtilValidate.isNotEmpty(modelParam.stringMapPrefix)) {
-                Map<String, Object> paramMap = 
UtilHttp.makeParamMapWithPrefix(request, multiPartMap, 
modelParam.stringMapPrefix, null);
+                Map<String, Object> paramMap = 
UtilHttp.makeParamMapWithPrefix(request, rawParametersMap, 
modelParam.stringMapPrefix, null);
                 value = paramMap;
                 if (Debug.verboseOn()) Debug.logVerbose("Set [" + 
modelParam.name + "]: " + paramMap, module);
             } else if (UtilValidate.isNotEmpty(modelParam.stringListSuffix)) {
-                List<Object> paramList = 
UtilHttp.makeParamListWithSuffix(request, multiPartMap, 
modelParam.stringListSuffix, null);
+                List<Object> paramList = 
UtilHttp.makeParamListWithSuffix(request, rawParametersMap, 
modelParam.stringListSuffix, null);
                 value = paramList;
             } else {
                 // first check the multi-part map
-                value = multiPartMap.get(name);
+                value = rawParametersMap.get(name);
 
                 // next check attributes; do this before parameters so that 
attribute which can be changed by code can override parameters which can't
                 if (UtilValidate.isEmpty(value)) {


Reply via email to