[ofbiz-framework] branch trunk updated: Extend UtilHttp.urlEncodeArgs API with preserveEmpty-parameter (OFBIZ-10198)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Extend UtilHttp.urlEncodeArgs API with preserveEmpty-parameter (OFBIZ-10198)

mbrohl
This is an automated email from the ASF dual-hosted git repository.

mbrohl pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 765fdbb  Extend UtilHttp.urlEncodeArgs API with preserveEmpty-parameter (OFBIZ-10198)
765fdbb is described below

commit 765fdbbbd8583281b64b0c701ffb6be1f7b39c53
Author: Michael Brohl <[hidden email]>
AuthorDate: Thu Jan 30 21:33:41 2020 +0100

    Extend UtilHttp.urlEncodeArgs API with preserveEmpty-parameter
    (OFBIZ-10198)
   
    This patch extends the current UtilHttp.urlEncodeArgs() method with an
    additional input parameter: boolean preserveEmpty.
   
    It allows to get a query string with empty parameters, useful if the
    receiver expects mandatory parameters or wants to explicit
    override/clear existing values with an empty one.
   
    Thanks Martin Becker for reporting and providing the patch.
---
 .../main/java/org/apache/ofbiz/base/util/UtilHttp.java  | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 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 ed8babb..ad0f3ac 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
@@ -914,19 +914,26 @@ public final class UtilHttp {
         }
     }
 
-    /** URL Encodes a Map of arguements */
+    /** URL Encodes a Map of arguments */
     public static String urlEncodeArgs(Map<String, ? extends Object> args) {
         return urlEncodeArgs(args, true);
     }
 
-    /** URL Encodes a Map of arguements */
+    /** URL Encodes a Map of arguments */
     public static String urlEncodeArgs(Map<String, ? extends Object> args, boolean useExpandedEntites) {
+        return urlEncodeArgs(args, useExpandedEntites, false);
+    }
+
+    /** URL Encodes a Map of arguments */
+    public static String urlEncodeArgs(Map<String, ? extends Object> args, boolean useExpandedEntites, boolean preserveEmpty) {
         StringBuilder buf = new StringBuilder();
         if (args != null) {
             for (Map.Entry<String, ? extends Object> entry: args.entrySet()) {
                 String name = entry.getKey();
                 Object value = entry.getValue();
-                String valueStr = null;
+                if (preserveEmpty && value == null) {
+                    value = "";
+                }
                 if (name == null || value == null) {
                     continue;
                 }
@@ -941,6 +948,8 @@ public final class UtilHttp {
                 } else {
                     col = Arrays.asList(value);
                 }
+
+                String valueStr = null;
                 for (Object colValue: col) {
                     if (colValue instanceof String) {
                         valueStr = (String) colValue;
@@ -950,7 +959,7 @@ public final class UtilHttp {
                         valueStr = colValue.toString();
                     }
 
-                    if (UtilValidate.isNotEmpty(valueStr)) {
+                    if (UtilValidate.isNotEmpty(valueStr) || preserveEmpty) {
                         if (buf.length() > 0) {
                             if (useExpandedEntites) {
                                 buf.append("&amp;");