Skip to content

Commit

Permalink
Use URIBuilder.appendPath instead of custom logic
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jun 18, 2024
1 parent fa00762 commit c2349ff
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions client/rest/src/main/java/org/opensearch/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,18 @@ private HttpUriRequestBase addRequestBody(HttpUriRequestBase httpRequest, HttpEn
static URI buildUri(String pathPrefix, String path, Map<String, String> 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<String, String> param : params.entrySet()) {
uriBuilder.addParameter(param.getKey(), param.getValue());
}
Expand All @@ -690,28 +700,6 @@ static URI buildUri(String pathPrefix, String path, Map<String, String> 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
Expand Down

0 comments on commit c2349ff

Please sign in to comment.