You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by jo...@apache.org on 2023/02/10 01:16:10 UTC

[incubator-eventmesh] branch master updated: [ISSUE #3080] Refactor NetUtils (#3081)

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

jonyang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 27f61f155 [ISSUE #3080] Refactor NetUtils (#3081)
27f61f155 is described below

commit 27f61f1559c3a2643cca539efa7c4e78172bfb61
Author: mxsm <lj...@gmail.com>
AuthorDate: Fri Feb 10 09:16:02 2023 +0800

    [ISSUE #3080] Refactor NetUtils (#3081)
---
 .../apache/eventmesh/common/utils/NetUtils.java    | 45 +++++++++++-----------
 .../eventmesh/common/utils}/NetUtilsTest.java      | 23 ++++++++++-
 2 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/NetUtils.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/NetUtils.java
index f8f4856c4..f6b3ae9d4 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/NetUtils.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/NetUtils.java
@@ -22,31 +22,31 @@ import static org.apache.eventmesh.common.Constants.SUCCESS_CODE;
 import org.apache.eventmesh.common.Constants;
 import org.apache.eventmesh.common.enums.HttpMethod;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.http.Consts;
 
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.net.InetSocketAddress;
 import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import com.sun.net.httpserver.HttpExchange;
 
+import lombok.extern.slf4j.Slf4j;
+
 /**
  * NetUtils
  */
+@Slf4j
 public class NetUtils {
 
-    private static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
-
     /**
      * Transform the url form string to Map
      *
@@ -54,20 +54,20 @@ public class NetUtils {
      * @return url parameters map
      */
     public static Map<String, String> formData2Dic(String formData) {
-        Map<String, String> result = new HashMap<>();
-        if (formData == null || formData.trim().length() == 0) {
-            return result;
+        if (StringUtils.isBlank(formData)) {
+            return new HashMap<>();
         }
         final String[] items = formData.split(Constants.AND);
+        Map<String, String> result = new HashMap<>(items.length);
         Arrays.stream(items).forEach(item -> {
             final String[] keyAndVal = item.split(Constants.EQ);
             if (keyAndVal.length == 2) {
                 try {
-                    final String key = URLDecoder.decode(keyAndVal[0], StandardCharsets.UTF_8.name());
-                    final String val = URLDecoder.decode(keyAndVal[1], StandardCharsets.UTF_8.name());
+                    final String key = URLDecoder.decode(keyAndVal[0], Constants.DEFAULT_CHARSET.name());
+                    final String val = URLDecoder.decode(keyAndVal[1], Constants.DEFAULT_CHARSET.name());
                     result.put(key, val);
                 } catch (UnsupportedEncodingException e) {
-                    logger.warn("formData2Dic:param decode failed...", e);
+                    log.warn("formData2Dic:param decode failed...", e);
                 }
             }
         });
@@ -87,16 +87,17 @@ public class NetUtils {
 
     public static String parsePostBody(HttpExchange exchange)
         throws IOException {
-        StringBuilder body = new StringBuilder();
-        if (HttpMethod.POST.name().equalsIgnoreCase(exchange.getRequestMethod())
-            || HttpMethod.PUT.name().equalsIgnoreCase(exchange.getRequestMethod())) {
-            try (InputStreamReader reader =
-                     new InputStreamReader(exchange.getRequestBody(), Consts.UTF_8)) {
-                char[] buffer = new char[256];
-                int read;
-                while ((read = reader.read(buffer)) != -1) {
-                    body.append(buffer, 0, read);
-                }
+
+        if (!HttpMethod.POST.name().equalsIgnoreCase(exchange.getRequestMethod())
+            && !HttpMethod.PUT.name().equalsIgnoreCase(exchange.getRequestMethod())) {
+            return "";
+        }
+        StringBuilder body = new StringBuilder(1024);
+        try (InputStreamReader reader = new InputStreamReader(exchange.getRequestBody(), Constants.DEFAULT_CHARSET.name())) {
+            char[] buffer = new char[256];
+            int readIndex;
+            while ((readIndex = reader.read(buffer)) != -1) {
+                body.append(buffer, 0, readIndex);
             }
         }
         return body.toString();
diff --git a/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/util/NetUtilsTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java
similarity index 69%
rename from eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/util/NetUtilsTest.java
rename to eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java
index 303bef6b3..a1131fde9 100644
--- a/eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/util/NetUtilsTest.java
+++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java
@@ -15,17 +15,22 @@
  * limitations under the License.
  */
 
-package org.apache.eventmesh.runtime.util;
+package org.apache.eventmesh.common.utils;
 
-import org.apache.eventmesh.common.utils.NetUtils;
+import org.apache.eventmesh.common.enums.HttpMethod;
 
+import java.io.ByteArrayInputStream;
 import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.sun.net.httpserver.HttpExchange;
 
 public class NetUtilsTest {
 
@@ -51,4 +56,18 @@ public class NetUtilsTest {
         result = NetUtils.addressToString(clients);
         Assert.assertEquals(result, localAddress + "|");
     }
+
+    @Test
+    public void testParsePostBody() throws Exception {
+
+        HttpExchange exchange = Mockito.mock(HttpExchange.class);
+        String expected = "mxsm";
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(expected.getBytes(StandardCharsets.UTF_8));
+        Mockito.when(exchange.getRequestMethod()).thenReturn(HttpMethod.POST.name());
+        Mockito.when(exchange.getRequestBody()).thenReturn(inputStream);
+
+        String actual = NetUtils.parsePostBody(exchange);
+        Assert.assertEquals(expected, actual);
+
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org