-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify to use URLEncodedUtils per version per Classpath
- Loading branch information
Showing
4 changed files
with
108 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
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
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.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
public class PathEncoder { | ||
|
||
private final static String httpClient4UtilClassName = "org.apache.http.client.utils.URLEncodedUtils"; | ||
private final static String httpClient5UtilClassName = "org.apache.hc.core5.net.URLEncodedUtils"; | ||
private static final boolean isHttpClient5UtilPresent = isPresent(httpClient5UtilClassName); | ||
|
||
public static String encode(String uri) { | ||
if (isHttpClient5UtilPresent) { | ||
return encodeByUtilClass(uri, httpClient5UtilClassName); | ||
} else { | ||
return encodeByUtilClass(uri, httpClient4UtilClassName); | ||
} | ||
} | ||
|
||
private static String encodeByUtilClass(String uri, String className) { | ||
try { | ||
Method formatSegments = Class.forName(className).getMethod("formatSegments", Iterable.class, Charset.class); | ||
String invoke = (String) formatSegments.invoke(null, Collections.singletonList(uri), StandardCharsets.UTF_8); | ||
return invoke.substring(1); | ||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException("Failed to encode URI using " + className, e); | ||
} | ||
} | ||
|
||
private static boolean isPresent(String className) { | ||
try { | ||
Class.forName(className); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
} |
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); | ||
} | ||
} |