
io.github.mike10004.vhs.HttpRequests Maven / Gradle / Ivy
package io.github.mike10004.vhs;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import io.github.mike10004.vhs.repackaged.org.apache.http.client.utils.URLEncodedUtils;
import javax.annotation.Nullable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Stream;
class HttpRequests {
private HttpRequests() {}
/**
* Creates a lowercase-keyed multimap from a list of headers.
*/
public static Multimap indexHeaders(Stream extends Map.Entry> entryHeaders) {
Multimap headers = ArrayListMultimap.create();
entryHeaders.forEach(header -> {
headers.put(header.getKey().toLowerCase(), header.getValue());
});
return headers;
}
/**
* Parses the query string of a URI and creates a multimap.
* Map entries are created from parameters as follows:
*
* - {@code key=value} becomes {@code (key, Optional.of(value))}
* - {@code key=} becomes {@code (key, Optional.of("")}
* - {@code key} becomes {@code (key, Optional.empty())}
*
* @param uri the URI
* @return the multimap
*/
@Nullable
public static Multimap> parseQuery(URI uri) {
if (uri.getQuery() == null) {
return null;
}
List> nvps = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8);
Multimap> mm = ArrayListMultimap.create();
nvps.forEach(nvp -> {
mm.put(nvp.getKey().toLowerCase(), Optional.ofNullable(nvp.getValue()));
});
return mm;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy