forked from opensearch-project/opensearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify to not use URLEncodedUtils (opensearch-project#999)
* modify to not use URLEncodedUtils Signed-off-by: AntCode97 <[email protected]> * add a license header Signed-off-by: AntCode97 <[email protected]> * add pathEncoder test Signed-off-by: AntCode97 <[email protected]> * modify to use ByteBuffer in encodeBytes method Signed-off-by: AntCode97 <[email protected]> * modify to use URLEncoder Signed-off-by: AntCode97 <[email protected]> * delete PathEncoder Signed-off-by: AntCode97 <[email protected]> * modify to use URLEncodedUtils per version per Classpath Signed-off-by: AntCode97 <[email protected]> * fix to use MethodHandle instead of reflection Signed-off-by: AntCode97 <[email protected]> * add CHANGELOG.md Signed-off-by: AntCode97 <[email protected]> --------- Signed-off-by: AntCode97 <[email protected]> Signed-off-by: Andriy Redko <[email protected]>
- Loading branch information
Showing
5 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
java-client/src/main/java/org/opensearch/client/util/PathEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.opensearch.client.util; | ||
|
||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
public class PathEncoder { | ||
private final static String HTTP_CLIENT4_UTILS_CLASS = "org.apache.http.client.utils.URLEncodedUtils"; | ||
private final static String HTTP_CLIENT5_UTILS_CLASS = "org.apache.hc.core5.net.URLEncodedUtils"; | ||
private final static MethodHandle FORMAT_SEGMENTS_MH; | ||
|
||
static { | ||
Class<?> clazz = null; | ||
try { | ||
// Try Apache HttpClient4 first since this is a default one | ||
clazz = Class.forName(HTTP_CLIENT4_UTILS_CLASS); | ||
} catch (final ClassNotFoundException ex) { | ||
try { | ||
// Fallback to Apache HttpClient4 | ||
clazz = Class.forName(HTTP_CLIENT5_UTILS_CLASS); | ||
} catch (final ClassNotFoundException ex1) { | ||
clazz = null; | ||
} | ||
} | ||
|
||
if (clazz == null) { | ||
throw new IllegalStateException( | ||
"Either '" + HTTP_CLIENT5_UTILS_CLASS + "' or '" + HTTP_CLIENT4_UTILS_CLASS + "' is required by not found on classpath" | ||
); | ||
} | ||
|
||
try { | ||
FORMAT_SEGMENTS_MH = MethodHandles.lookup() | ||
.findStatic(clazz, "formatSegments", MethodType.methodType(String.class, Iterable.class, Charset.class)); | ||
} catch (final NoSuchMethodException | IllegalAccessException ex) { | ||
throw new IllegalStateException("Unable to find 'formatSegments' method in " + clazz + " class"); | ||
} | ||
} | ||
|
||
public static String encode(String uri) { | ||
try { | ||
return ((String) FORMAT_SEGMENTS_MH.invoke(Collections.singletonList(uri), StandardCharsets.UTF_8)).substring(1); | ||
} catch (final Throwable ex) { | ||
throw new RuntimeException("Unable to encode URI: " + uri, ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
java-client/src/test/java/org/opensearch/client/util/PathEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.opensearch.client.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class PathEncoderTest { | ||
|
||
@Test | ||
public void testEncode() { | ||
// Test with a simple string | ||
String simpleString = "test"; | ||
String encodedSimpleString = PathEncoder.encode(simpleString); | ||
assertEquals(simpleString, encodedSimpleString); | ||
|
||
// Test with a string that contains special characters | ||
String specialString = "a/b"; | ||
String encodedSpecialString = PathEncoder.encode(specialString); | ||
assertEquals("a%2Fb", encodedSpecialString); | ||
|
||
// Test with a string that contains alphanumeric characters | ||
String alphanumericString = "abc123"; | ||
String encodedAlphanumericString = PathEncoder.encode(alphanumericString); | ||
assertEquals("abc123", encodedAlphanumericString); | ||
|
||
// Test with a string that contains multiple segments | ||
String multiSegmentString = "a/b/c/_refresh"; | ||
String encodedMultiSegmentString = PathEncoder.encode(multiSegmentString); | ||
assertEquals("a%2Fb%2Fc%2F_refresh", encodedMultiSegmentString); | ||
} | ||
} |