From 7fcb24d3c15a0051ec88b9a3d608f3b9cf21a84e Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Tue, 30 Jul 2024 14:18:31 +1200 Subject: [PATCH] Refactor PercentCodec a bit Signed-off-by: Thomas Farr --- .../opensearch/client/util/PathEncoder.java | 1 - .../opensearch/client/util/PercentCodec.java | 157 ++++++++---------- 2 files changed, 72 insertions(+), 86 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/util/PathEncoder.java b/java-client/src/main/java/org/opensearch/client/util/PathEncoder.java index 0d1aa35703..2574aaa26a 100644 --- a/java-client/src/main/java/org/opensearch/client/util/PathEncoder.java +++ b/java-client/src/main/java/org/opensearch/client/util/PathEncoder.java @@ -28,7 +28,6 @@ public static void setCodec(PercentCodec codec) { PathEncoder.codec = codec; } - public static String encode(String pathSegment) { return getCodec().encode(pathSegment); } diff --git a/java-client/src/main/java/org/opensearch/client/util/PercentCodec.java b/java-client/src/main/java/org/opensearch/client/util/PercentCodec.java index f0f6c47813..a672c71c38 100644 --- a/java-client/src/main/java/org/opensearch/client/util/PercentCodec.java +++ b/java-client/src/main/java/org/opensearch/client/util/PercentCodec.java @@ -21,96 +21,83 @@ *

*/ public class PercentCodec { - private static final BitSet RFC3986_GEN_DELIMS_CHARS = new BitSet(256); - private static final BitSet RFC3986_SUB_DELIMS_CHARS = new BitSet(256); - private static final BitSet RFC3986_UNRESERVED_CHARS = new BitSet(256); - private static final BitSet RFC3986_PATHSAFE_NC_CHARS = new BitSet(256); - private static final BitSet RFC3986_PATHSAFE_CHARS = new BitSet(256); - private static final BitSet RFC3986_URIC_CHARS = new BitSet(256); - - static { - RFC3986_GEN_DELIMS_CHARS.set(':'); - RFC3986_GEN_DELIMS_CHARS.set('/'); - RFC3986_GEN_DELIMS_CHARS.set('?'); - RFC3986_GEN_DELIMS_CHARS.set('#'); - RFC3986_GEN_DELIMS_CHARS.set('['); - RFC3986_GEN_DELIMS_CHARS.set(']'); - RFC3986_GEN_DELIMS_CHARS.set('@'); - - RFC3986_SUB_DELIMS_CHARS.set('!'); - RFC3986_SUB_DELIMS_CHARS.set('$'); - RFC3986_SUB_DELIMS_CHARS.set('&'); - RFC3986_SUB_DELIMS_CHARS.set('\''); - RFC3986_SUB_DELIMS_CHARS.set('('); - RFC3986_SUB_DELIMS_CHARS.set(')'); - RFC3986_SUB_DELIMS_CHARS.set('*'); - RFC3986_SUB_DELIMS_CHARS.set('+'); - RFC3986_SUB_DELIMS_CHARS.set(','); - RFC3986_SUB_DELIMS_CHARS.set(';'); - RFC3986_SUB_DELIMS_CHARS.set('='); - - for (int i = 'a'; i <= 'z'; i++) { - RFC3986_UNRESERVED_CHARS.set(i); - } - for (int i = 'A'; i <= 'Z'; i++) { - RFC3986_UNRESERVED_CHARS.set(i); - } - // numeric characters - for (int i = '0'; i <= '9'; i++) { - RFC3986_UNRESERVED_CHARS.set(i); - } - RFC3986_UNRESERVED_CHARS.set('-'); - RFC3986_UNRESERVED_CHARS.set('.'); - RFC3986_UNRESERVED_CHARS.set('_'); - RFC3986_UNRESERVED_CHARS.set('~'); - - RFC3986_PATHSAFE_NC_CHARS.or(RFC3986_UNRESERVED_CHARS); - RFC3986_PATHSAFE_NC_CHARS.or(RFC3986_SUB_DELIMS_CHARS); - RFC3986_PATHSAFE_NC_CHARS.set('@'); - - RFC3986_PATHSAFE_CHARS.or(RFC3986_PATHSAFE_NC_CHARS); - RFC3986_PATHSAFE_CHARS.set(':'); - - RFC3986_URIC_CHARS.or(RFC3986_SUB_DELIMS_CHARS); - RFC3986_URIC_CHARS.or(RFC3986_UNRESERVED_CHARS); - } + private static class Chars { + private final BitSet set = new BitSet(256); - private static final BitSet RFC5987_UNRESERVED_CHARS = new BitSet(256); - - static { - // Alphanumeric characters - for (int i = 'a'; i <= 'z'; i++) { - RFC5987_UNRESERVED_CHARS.set(i); - } - for (int i = 'A'; i <= 'Z'; i++) { - RFC5987_UNRESERVED_CHARS.set(i); - } - for (int i = '0'; i <= '9'; i++) { - RFC5987_UNRESERVED_CHARS.set(i); - } - - // Additional characters as per RFC 5987 attr-char - RFC5987_UNRESERVED_CHARS.set('!'); - RFC5987_UNRESERVED_CHARS.set('#'); - RFC5987_UNRESERVED_CHARS.set('$'); - RFC5987_UNRESERVED_CHARS.set('&'); - RFC5987_UNRESERVED_CHARS.set('+'); - RFC5987_UNRESERVED_CHARS.set('-'); - RFC5987_UNRESERVED_CHARS.set('.'); - RFC5987_UNRESERVED_CHARS.set('^'); - RFC5987_UNRESERVED_CHARS.set('_'); - RFC5987_UNRESERVED_CHARS.set('`'); - RFC5987_UNRESERVED_CHARS.set('|'); - RFC5987_UNRESERVED_CHARS.set('~'); + public void add(char... chars) { + for (char c : chars) { + set.set(c); + } + } + + public void addRange(char start, char end) { + set.set(start, end + 1); + } + + public void add(Chars set) { + this.set.or(set.set); + } + + public boolean contains(int c) { + return set.get(c); + } } + private static final Chars RFC3986_GEN_DELIMS_CHARS = new Chars() { + { + add(':', '/', '?', '#', '[', ']', '@'); + } + }; + private static final Chars RFC3986_SUB_DELIMS_CHARS = new Chars() { + { + add('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='); + } + }; + private static final Chars RFC3986_UNRESERVED_CHARS = new Chars() { + { + addRange('a', 'z'); + addRange('A', 'Z'); + addRange('0', '9'); + add('-', '.', '_', '~'); + } + }; + private static final Chars RFC3986_PATH_NO_COLON_CHARS = new Chars() { + { + add(RFC3986_UNRESERVED_CHARS); + add(RFC3986_SUB_DELIMS_CHARS); + add('@'); + } + }; + private static final Chars RFC3986_PATH_CHARS = new Chars() { + { + add(RFC3986_PATH_NO_COLON_CHARS); + add(':'); + } + }; + private static final Chars RFC3986_URIC_CHARS = new Chars() { + { + add(RFC3986_SUB_DELIMS_CHARS); + add(RFC3986_UNRESERVED_CHARS); + } + }; + + private static final Chars RFC5987_UNRESERVED_CHARS = new Chars() { + { + addRange('a', 'z'); + addRange('A', 'Z'); + addRange('0', '9'); + // Additional characters as per RFC 5987 attr-char + add('!', '#', '$', '&', '+', '-', '.', '^', '_', '`', '|', '~'); + } + }; + private static final int RADIX = 16; private static void encode( final StringBuilder buf, final CharSequence content, final Charset charset, - final BitSet safeChars, + final Chars safeChars, final boolean blankAsPlus ) { if (content == null) { @@ -120,7 +107,7 @@ private static void encode( final ByteBuffer bb = (charset != null ? charset : StandardCharsets.UTF_8).encode(cb); while (bb.hasRemaining()) { final int b = bb.get() & 0xff; - if (safeChars.get(b)) { + if (safeChars.contains(b)) { buf.append((char) b); } else if (blankAsPlus && b == ' ') { buf.append("+"); @@ -165,12 +152,12 @@ 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_PATHSAFE_CHARS); + public static final PercentCodec RFC3986_PATHSAFE = new PercentCodec(RFC3986_PATH_CHARS); public static final PercentCodec RFC5987_UNRESERVED = new PercentCodec(RFC5987_UNRESERVED_CHARS); - private final BitSet unreserved; + private final Chars unreserved; - private PercentCodec(final BitSet unreserved) { + private PercentCodec(final Chars unreserved) { this.unreserved = unreserved; }