From efbe1bcfb3e7f07def05ca371923f6d94f1b8d78 Mon Sep 17 00:00:00 2001 From: Thespica Date: Wed, 17 Jul 2024 16:59:58 +0800 Subject: [PATCH 1/8] add method and test --- .../hugegraph/rest/AbstractRestClient.java | 13 +++++ .../unit/rest/AbstractRestClientTest.java | 57 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index 2b08e69b..5ab1c463 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -19,7 +19,10 @@ import java.io.FileInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; import java.security.KeyStore; import java.util.Arrays; import java.util.Collection; @@ -36,6 +39,7 @@ import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; +import com.google.common.net.UrlEscapers; import org.apache.commons.lang3.StringUtils; import org.apache.hugegraph.util.JsonUtilCommon; import org.jetbrains.annotations.NotNull; @@ -411,6 +415,15 @@ public void close() { } } + public static String encode(String raw) { + try { + URI uri = new URI(null, null, raw, null); + return uri.toASCIIString(); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Failed to encode string: " + raw, e); + } + } + public void resetAuthContext() { this.authContext.remove(); } diff --git a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java new file mode 100644 index 00000000..f87be418 --- /dev/null +++ b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.unit.rest; + +import org.apache.hugegraph.rest.AbstractRestClient; +import org.junit.Assert; +import org.junit.Test; + +public class AbstractRestClientTest { + + @Test + public void testEncodeWithSpaces() { + String raw = "hello world"; + String expected = "hello%20world"; + String encoded = AbstractRestClient.encode(raw); + Assert.assertEquals(expected, encoded); + } + + @Test + public void testEncodeWithSpecialCharacters() { + String raw = "hello@world!"; + String expected = "hello%40world%21"; + String encoded = AbstractRestClient.encode(raw); + Assert.assertEquals(expected, encoded); + } + + @Test + public void testEncodeWithChineseCharacters() { + String raw = "你好"; + String expected = "%E4%BD%A0%E5%A5%BD"; + String encoded = AbstractRestClient.encode(raw); + Assert.assertEquals(expected, encoded); + } + + @Test + public void testEncodeWithNullInput() { + String raw = null; + Assert.assertThrows(NullPointerException.class, () -> { + AbstractRestClient.encode(raw); + }); + } +} From ebf9e46b742e36add1614592c342c87effa6bffe Mon Sep 17 00:00:00 2001 From: Thespica Date: Wed, 17 Jul 2024 17:06:40 +0800 Subject: [PATCH 2/8] delete useless import --- .../java/org/apache/hugegraph/rest/AbstractRestClient.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index 5ab1c463..e143c7b9 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -19,10 +19,8 @@ import java.io.FileInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URLEncoder; import java.security.KeyStore; import java.util.Arrays; import java.util.Collection; @@ -39,7 +37,6 @@ import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; -import com.google.common.net.UrlEscapers; import org.apache.commons.lang3.StringUtils; import org.apache.hugegraph.util.JsonUtilCommon; import org.jetbrains.annotations.NotNull; From f749e9443106563b5df837c18d040f223103b5d9 Mon Sep 17 00:00:00 2001 From: Thespica Date: Thu, 18 Jul 2024 16:00:52 +0800 Subject: [PATCH 3/8] regist test for abstract rest client --- .../src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java | 1 + 1 file changed, 1 insertion(+) diff --git a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java index 83892819..4bb4e504 100644 --- a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java @@ -83,6 +83,7 @@ EventHubTest.class, PerfUtilTest.class, StopwatchTest.class, + AbstractRestClientTest.class, RestClientTest.class, RestResultTest.class, VersionTest.class, From a9b7f2d74f666be43772c9aeaebf93c924a02676 Mon Sep 17 00:00:00 2001 From: Thespica Date: Thu, 18 Jul 2024 16:40:01 +0800 Subject: [PATCH 4/8] add import --- .../src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java | 1 + 1 file changed, 1 insertion(+) diff --git a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java index 4bb4e504..80095d1c 100644 --- a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java @@ -22,6 +22,7 @@ import org.apache.hugegraph.unit.config.HugeConfigTest; import org.apache.hugegraph.unit.config.OptionSpaceTest; import org.apache.hugegraph.unit.event.EventHubTest; +import org.apache.hugegraph.unit.rest.AbstractRestClientTest; import org.apache.hugegraph.unit.version.VersionTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; From 4360cd1075f32b0cefc839da292b572f34e74264 Mon Sep 17 00:00:00 2001 From: Thespica Date: Tue, 30 Jul 2024 21:14:54 +0800 Subject: [PATCH 5/8] use URL encoder --- .../org/apache/hugegraph/rest/AbstractRestClient.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index e143c7b9..345e6bdb 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -19,8 +19,10 @@ import java.io.FileInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URI; -import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.util.Arrays; import java.util.Collection; @@ -414,10 +416,9 @@ public void close() { public static String encode(String raw) { try { - URI uri = new URI(null, null, raw, null); - return uri.toASCIIString(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Failed to encode string: " + raw, e); + return URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()).replace("+", "%20"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } } From f53cf14dac74055cc9550b0fb6b87c42a3f558c9 Mon Sep 17 00:00:00 2001 From: Thespica Date: Sun, 18 Aug 2024 15:30:01 +0800 Subject: [PATCH 6/8] replace + by %2B --- .../java/org/apache/hugegraph/rest/AbstractRestClient.java | 4 ++-- .../apache/hugegraph/unit/rest/AbstractRestClientTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index 345e6bdb..72ecfae9 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -416,9 +416,9 @@ public void close() { public static String encode(String raw) { try { - return URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()).replace("+", "%20"); + return URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()).replace("+", "%2B"); } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new IllegalArgumentException("Failed to encode string: %s" + raw); } } diff --git a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java index f87be418..b2d2b1a3 100644 --- a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java +++ b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/AbstractRestClientTest.java @@ -26,7 +26,7 @@ public class AbstractRestClientTest { @Test public void testEncodeWithSpaces() { String raw = "hello world"; - String expected = "hello%20world"; + String expected = "hello%2Bworld"; String encoded = AbstractRestClient.encode(raw); Assert.assertEquals(expected, encoded); } From b9989e53bdbe22f8b03abd20aa9ec1f63724552f Mon Sep 17 00:00:00 2001 From: Thespica Date: Sun, 18 Aug 2024 15:51:11 +0800 Subject: [PATCH 7/8] add more replace rules --- .../org/apache/hugegraph/rest/AbstractRestClient.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index 72ecfae9..c9611687 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -416,9 +416,16 @@ public void close() { public static String encode(String raw) { try { - return URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()).replace("+", "%2B"); + String encoded = URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()); + return encoded.replace("+", "%2B") + .replace(" ", "%20") + .replace("/", "%2F") + .replace("?", "%3F") + .replace("#", "%23") + .replace("&", "%26") + .replace("=", "%3D"); } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("Failed to encode string: %s" + raw); + throw new IllegalArgumentException("Failed to encode string: " + raw, e); } } From 3903d24a67ac7c044de695a709a68a0a48ea0c59 Mon Sep 17 00:00:00 2001 From: Thespica Date: Sun, 18 Aug 2024 15:57:31 +0800 Subject: [PATCH 8/8] shrink replace rules --- .../org/apache/hugegraph/rest/AbstractRestClient.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index c9611687..c13a686b 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -416,14 +416,7 @@ public void close() { public static String encode(String raw) { try { - String encoded = URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()); - return encoded.replace("+", "%2B") - .replace(" ", "%20") - .replace("/", "%2F") - .replace("?", "%3F") - .replace("#", "%23") - .replace("&", "%26") - .replace("=", "%3D"); + return URLEncoder.encode(raw, StandardCharsets.UTF_8.toString()).replace("+", "%2B"); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("Failed to encode string: " + raw, e); }