From 916e9f5f4608806375d1b9a8a8002b0adf787807 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 1 Aug 2024 15:02:43 +1200 Subject: [PATCH] Tweak default encoding behavior to match prior behavior Signed-off-by: Thomas Farr --- .../org/opensearch/client/util/PathEncoder.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 5d43b7c19a..d0cc056ba6 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 @@ -33,8 +33,19 @@ static Optional get(String name) { } } + private static boolean isHttpClientV4InClasspath() { + try { + Class.forName("org.apache.http.client.utils.URLEncodedUtils"); + return true; + } catch (Exception ignored) { + return false; + } + } + 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_DEFAULT = isHttpClientV4InClasspath() + ? Encoding.HTTP_CLIENT_V4_EQUIV + : Encoding.HTTP_CLIENT_V5_EQUIV; private static final Encoding ENCODING = Optional.ofNullable(System.getProperty(ENCODING_PROPERTY)) .flatMap(Encoding::get)