Skip to content

Commit

Permalink
encode args without Apache
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Dec 19, 2023
1 parent 51acd74 commit 99f82b1
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions std-bits/base/src/main/java/org/enso/base/net/URIHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,45 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.http.client.utils.URIBuilder;

public class URIHelpers {
public record NameValuePair(String name, String value) {}

public static URI addQueryParameters(URI uri, List<NameValuePair> params)
throws URISyntaxException {
URIBuilder builder = new URIBuilder(uri);
StringBuilder query = new StringBuilder();
if (uri.getRawQuery() != null) {
query.append(uri.getRawQuery());
}

for (NameValuePair param : params) {
builder.addParameter(param.name(), param.value());
if (!query.isEmpty()) {
query.append("&");
}

query.append(encode(param.name)).append("=").append(encode(param.value));
}

StringBuilder uriBuilder = new StringBuilder();
uriBuilder
.append(uri.getScheme()).append("://")
.append(uri.getRawAuthority())
.append(uri.getRawPath());
if (!query.isEmpty()) {
uriBuilder.append("?").append(query);
}
return builder.build();

if (uri.getRawFragment() != null) {
uriBuilder.append("#").append(uri.getRawFragment());
}

return new URI(uriBuilder.toString());
}

private static String encode(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}
}

0 comments on commit 99f82b1

Please sign in to comment.