From c2349ff187bb32caf2fb25048422409be148c7f3 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Wed, 19 Jun 2024 01:37:00 +1200 Subject: [PATCH] Use `URIBuilder.appendPath` instead of custom logic Signed-off-by: Thomas Farr --- .../org/opensearch/client/RestClient.java | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/client/rest/src/main/java/org/opensearch/client/RestClient.java b/client/rest/src/main/java/org/opensearch/client/RestClient.java index 7b431d9577512..23acd0bbd9c76 100644 --- a/client/rest/src/main/java/org/opensearch/client/RestClient.java +++ b/client/rest/src/main/java/org/opensearch/client/RestClient.java @@ -674,8 +674,18 @@ private HttpUriRequestBase addRequestBody(HttpUriRequestBase httpRequest, HttpEn static URI buildUri(String pathPrefix, String path, Map params) { Objects.requireNonNull(path, "path must not be null"); try { - String fullPath = buildUriPath(pathPrefix, path); - URIBuilder uriBuilder = new URIBuilder(fullPath); + URIBuilder uriBuilder = new URIBuilder(); + + if (pathPrefix != null && !pathPrefix.isEmpty() && !"/".equals(pathPrefix)) { + uriBuilder.appendPath(pathPrefix); + } + if (!path.isEmpty() && !"/".equals(path)) { + uriBuilder.appendPath(path); + } + if (uriBuilder.getPathSegments().isEmpty()) { + uriBuilder.setPath("/"); + } + for (Map.Entry param : params.entrySet()) { uriBuilder.addParameter(param.getKey(), param.getValue()); } @@ -690,28 +700,6 @@ static URI buildUri(String pathPrefix, String path, Map params) } } - private static String buildUriPath(String pathPrefix, String path) { - pathPrefix = pathPrefix != null ? trimSlashes(pathPrefix) : ""; - path = path != null ? trimSlashes(path) : ""; - - if (!pathPrefix.isEmpty()) { - if (!path.isEmpty()) { - return "/" + pathPrefix + "/" + path; - } - return "/" + pathPrefix; - } - - return "/" + path; - } - - private static String trimSlashes(String str) { - int start = 0; - int end = str.length(); - while (start < end && str.charAt(start) == '/') ++start; - while (end > start && str.charAt(end - 1) == '/') --end; - return str.substring(start, end); - } - /** * Listener used in any async call to wrap the provided user listener (or SyncResponseListener in sync calls). * Allows to track potential failures coming from the different retry attempts and returning to the original listener