Skip to content

Commit

Permalink
Switch to system property
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jul 31, 2024
1 parent 2820ff8 commit 75f9059
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,42 @@

package org.opensearch.client.util;

import java.util.Optional;

public class PathEncoder {
/**
* Percent encoding codec that matches Apache HTTP Client 4's path segment encoding.
*/
@Deprecated
public static final PercentCodec APACHE_HTTP_CLIENT_4_EQUIV_CODEC = PercentCodec.RFC3986_PATHSAFE;
/**
* Percent encoding codec that matches Apache HTTP Client 5's path segment encoding.
*/
public static final PercentCodec APACHE_HTTP_CLIENT_5_EQUIV_CODEC = PercentCodec.RFC3986_UNRESERVED;

public static final PercentCodec DEFAULT_CODEC = APACHE_HTTP_CLIENT_5_EQUIV_CODEC;

private static PercentCodec codec;

public static PercentCodec getCodec() {
if (codec == null) {
codec = DEFAULT_CODEC;
private enum Encoding {
RFC3986_PATH(PercentCodec.RFC3986_PATH),
HTTP_CLIENT_V4_EQUIV(PercentCodec.RFC3986_PATH),

RFC3986_UNRESERVED(PercentCodec.RFC3986_UNRESERVED),
HTTP_CLIENT_V5_EQUIV(PercentCodec.RFC3986_UNRESERVED);

private final PercentCodec percentCodec;

Encoding(PercentCodec percentCodec) {
this.percentCodec = percentCodec;
}
return codec;
}

public static void setCodec(PercentCodec codec) {
PathEncoder.codec = codec;
static Optional<Encoding> get(String name) {
try {
return Optional.of(Encoding.valueOf(name.toUpperCase()));
} catch (Exception ignored) {
return Optional.empty();
}
}
}
private static final String ENCODING_PROPERTY = "org.opensearch.path.encoding";
private static final Encoding ENCODING_DEFAULT = Encoding.HTTP_CLIENT_V5_EQUIV;

private static final Encoding ENCODING = Optional.ofNullable(System.getProperty(ENCODING_PROPERTY))
.flatMap(Encoding::get)
.orElse(ENCODING_DEFAULT);

public static String encode(String pathSegment) {
return getCodec().encode(pathSegment);
return ENCODING.percentCodec.encode(pathSegment);
}

public static void encode(StringBuilder dest, CharSequence pathSegment) {
getCodec().encode(dest, pathSegment);
ENCODING.percentCodec.encode(dest, pathSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Adapted from Apache HttpComponents HttpCore v5's <a href="https://github.com/apache/httpcomponents-core/blob/e009a923eefe79cf3593efbb0c18a3525ae63669/httpcore5/src/main/java/org/apache/hc/core5/net/PercentCodec.java">PercentCodec.java</a>
* </p>
*/
public class PercentCodec {
class PercentCodec {
private static class Chars {
private final BitSet set = new BitSet(256);

Expand Down Expand Up @@ -152,7 +152,7 @@ private static String decode(final CharSequence content, final Charset charset,
}

public static final PercentCodec RFC3986_UNRESERVED = new PercentCodec(RFC3986_UNRESERVED_CHARS);
public static final PercentCodec RFC3986_PATHSAFE = new PercentCodec(RFC3986_PATH_CHARS);
public static final PercentCodec RFC3986_PATH = new PercentCodec(RFC3986_PATH_CHARS);
public static final PercentCodec RFC5987_UNRESERVED = new PercentCodec(RFC5987_UNRESERVED_CHARS);

private final Chars unreserved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PercentCodecTest {
public static Collection<Object[]> testData() {
return Arrays.asList(
new Object[][] {
// <unencoded>, <encoded_unreserved>, <encoded_pathsafe>
// <unencoded>, <encoded_unreserved>, <encoded_path>
{ "test", "test", "test" },
{ "abc123", "abc123", "abc123" },
{ "a/b", "a%2Fb", "a%2Fb" },
Expand All @@ -34,12 +34,12 @@ public static Collection<Object[]> testData() {

private final String decoded;
private final String encodedRFC3986Unreserved;
private final String encodedRFC3986PathSafe;
private final String encodedRFC3986Path;

public PercentCodecTest(String decoded, String encodedRFC3986Unreserved, String encodedRFC3986PathSafe) {
public PercentCodecTest(String decoded, String encodedRFC3986Unreserved, String encodedRFC3986Path) {
this.decoded = decoded;
this.encodedRFC3986Unreserved = encodedRFC3986Unreserved;
this.encodedRFC3986PathSafe = encodedRFC3986PathSafe;
this.encodedRFC3986Path = encodedRFC3986Path;
}

@Test
Expand All @@ -53,12 +53,12 @@ public void test_RFC3986_UNRESERVED_decoding() {
}

@Test
public void test_RFC3986_PATHSAFE_encoding() {
assertEquals(this.encodedRFC3986PathSafe, PercentCodec.RFC3986_PATHSAFE.encode(this.decoded));
public void test_RFC3986_PATH_encoding() {
assertEquals(this.encodedRFC3986Path, PercentCodec.RFC3986_PATH.encode(this.decoded));
}

@Test
public void test_RFC3986_PATHSAFE_decoding() {
assertEquals(this.decoded, PercentCodec.RFC3986_PATHSAFE.decode(this.encodedRFC3986PathSafe));
public void test_RFC3986_PATH_decoding() {
assertEquals(this.decoded, PercentCodec.RFC3986_PATH.decode(this.encodedRFC3986Path));
}
}

0 comments on commit 75f9059

Please sign in to comment.