From 512560e335276df535af32cd45108f1b26e2bc45 Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Sun, 2 Dec 2018 10:36:09 +0000 Subject: [PATCH] "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. ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/branches/release17.12@1847955 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ofbiz/base/util/UtilHttp.java | 117 +++++++++++++++++- .../webapp/event/ServiceEventHandler.java | 112 +---------------- 2 files changed, 119 insertions(+), 110 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java index 9d9fa5e55a..036aefb948 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java @@ -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.HttpServletRequest; 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.TrustSelfSignedStrategy; 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 multiPartMap = UtilGenerics.checkMap(request.getAttribute("multiPartMap")); + Map multiPartMap = getMultiPartParameterMap(request); if (UtilValidate.isNotEmpty(multiPartMap)) { paramMap.putAll(multiPartMap); } @@ -167,6 +176,112 @@ public final class UtilHttp { return canonicalizeParameterMap(paramMap); } + public static Map getMultiPartParameterMap(HttpServletRequest request) { + Map multiPartMap = new HashMap(); + 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 uploadedItems = null; + try { + uploadedItems = UtilGenerics.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 newList = new LinkedList(); + 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 getQueryStringOnlyParameterMap(String queryString) { Map paramMap = new HashMap<>(); if (UtilValidate.isNotEmpty(queryString)) { diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java index 55c8b4a06d..e8c9a74817 100644 --- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/ServiceEventHandler.java @@ -35,10 +35,6 @@ import javax.servlet.http.HttpServletRequest; 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 implements EventHandler { Debug.logVerbose("[Using delegator]: " + dispatcher.getDelegator().getDelegatorName(), module); } - boolean isMultiPart = ServletFileUpload.isMultipartContent(request); - Map multiPartMap = new HashMap(); - 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 uploadedItems = null; - try { - uploadedItems = UtilGenerics.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 newList = new LinkedList(); - 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 rawParametersMap = UtilHttp.getCombinedMap(request); Set urlOnlyParameterNames = UtilHttp.getUrlOnlyParameterMap(request).keySet(); @@ -252,15 +146,15 @@ public class ServiceEventHandler implements EventHandler { Object value = null; if (UtilValidate.isNotEmpty(modelParam.stringMapPrefix)) { - Map paramMap = UtilHttp.makeParamMapWithPrefix(request, multiPartMap, modelParam.stringMapPrefix, null); + Map 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 paramList = UtilHttp.makeParamListWithSuffix(request, multiPartMap, modelParam.stringListSuffix, null); + List 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)) {