Skip to content

Instantly share code, notes, and snippets.

@SharkFourSix
Created August 11, 2021 15:04
Show Gist options
  • Save SharkFourSix/49969e5c2816a32b28a6b699cbbbf660 to your computer and use it in GitHub Desktop.
Save SharkFourSix/49969e5c2816a32b28a6b699cbbbf660 to your computer and use it in GitHub Desktop.
URL Query Parameter Manipulator: Add/Remove Query Parameters
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
public class UrlUtil {
private final URL url;
private final Map<String, List<String>> parameters;
public UrlUtil(String url) throws MalformedURLException {
this.url = new URL(url);
this.parameters = new LinkedHashMap<>();
parse();
}
private void addParameterWithValue(String parameter, String value) {
boolean emptyValue = value == null || value.isBlank();
List<String> values = this.parameters.computeIfAbsent(parameter, p -> new LinkedList<>());
if (!emptyValue) {
values.add(value);
}
}
private void parse() {
String query = url.getQuery();
if (query != null) {
String[] parts = query.split("&");
if (parts.length > 0) {
for (String pair : parts) {
String[] map = pair.split("=");
addParameterWithValue(map[0], map.length == 2 ? map[1] : null);
}
}
}
}
public UrlUtil addParameter(String parameter, String value) {
addParameterWithValue(parameter, value);
return this;
}
public String getParameter(String name) {
List<String> values = parameters.get(name);
return values != null && !values.isEmpty() ? values.get(0) : "";
}
private void buildQuery(StringBuilder builder) {
StringJoiner joiner = new StringJoiner("&");
if (!this.parameters.isEmpty()) {
for (String name : parameters.keySet()) {
// k-> [1,2,3,4]
// k=1&k=2&k=3&k=4
List<String> values = this.parameters.get(name);
if (values.isEmpty()) {
joiner.add(name + '=');
} else {
for (String value : values) {
joiner.add(name + '=' + value);
}
}
}
builder.append('?').append(joiner);
}
}
public String build() {
StringBuilder builder = new StringBuilder();
// <scheme>://<authority><path>?<query>#<fragment>
String scheme = url.getProtocol();
String authority = url.getAuthority();
String path = url.getPath();
String fragment = url.getRef();
builder.append(scheme)
.append("://")
.append(authority);
if (!path.isBlank()) {
builder.append(path);
}
buildQuery(builder);
if (fragment != null) {
builder.append('#').append(fragment);
}
return builder.toString();
}
public URL buildURL() throws MalformedURLException {
return new URL(build());
}
/**
* Remove parameter with specified value. Other occurrences with different values will be left intact
*
* @param parameter .
* @param value .
*/
public void removeParameter(String parameter, String value) {
List<String> values = this.parameters.get(parameter);
if (values != null) {
values.remove(value);
if (values.isEmpty()) {
this.parameters.remove(parameter);
}
}
}
/**
* Remove all occurrences of a parameter.
*
* @param parameter .
*/
public void removeParameter(String parameter) {
this.parameters.remove(parameter);
}
public static String getRequestPath(URL url) {
String path = url.getPath();
String query = url.getQuery();
if (!path.startsWith("/")) {
path = "/" + path;
}
if (query != null) {
query = '?' + query;
}
return path + query;
}
@Override
public String toString() {
return url.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment