org.bklab.flow.util.url.UrlParameterParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-vaadin-flow Show documentation
Show all versions of fluent-vaadin-flow Show documentation
Broderick Labs for fluent vaadin flow. Inherits common Vaadin components.
/*
* Copyright (c) 2008 - 2020. - Broderick Labs.
* Author: Broderick Johansson
* E-mail: [email protected]
* Modify date:2020-07-01 13:55:46
* _____________________________
* Project name: fluent-vaadin-flow
* Class name:org.bklab.flow.util.UrlParameterParser
* Copyright (c) 2008 - 2020. - Broderick Labs.
*/
package org.bklab.flow.util.url;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
public class UrlParameterParser {
private final Map> parameterMap;
private final Charset charset;
public UrlParameterParser(URL url) {
this(url, StandardCharsets.UTF_8);
}
public UrlParameterParser(String url) {
this(url, StandardCharsets.UTF_8);
}
public UrlParameterParser(URL url, Charset charset) {
this.charset = StandardCharsets.UTF_8;
this.parameterMap = url == null ? Collections.emptyMap() : splitQuery(url.getQuery());
}
public UrlParameterParser(String url, Charset charset) {
this.charset = StandardCharsets.UTF_8;
this.parameterMap = url == null ? Collections.emptyMap() : splitQuery(url);
}
public Set getKeys() {
return parameterMap.keySet();
}
public String getValue(String parameterName) {
List values = getValues(parameterName);
if (values != null) {
for (String value : values) {
return value;
}
}
return null;
}
public UrlParameterParser forEach(BiConsumer> consumer) {
parameterMap.forEach(consumer);
return this;
}
public UrlParameterParser forEachSingle(BiConsumer consumer) {
for (String key : parameterMap.keySet()) {
consumer.accept(key, getValue(key));
}
return this;
}
public List getValues(String parameterName) {
return parameterMap.getOrDefault(parameterName, new ArrayList<>());
}
public Map> getParameterMap() {
return parameterMap;
}
private Map> splitQuery(String parameter) {
return parameter == null || parameter.isBlank() ? Collections.emptyMap() : Arrays.stream(parameter.split("&"))
.map(this::splitQueryParameter)
.collect(Collectors.groupingBy(AbstractMap.SimpleImmutableEntry::getKey, LinkedHashMap::new,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
private AbstractMap.SimpleImmutableEntry splitQueryParameter(String it) {
final int idx = it.indexOf("=");
final String key = URLDecoder.decode(idx > 0 ? it.substring(0, idx) : it, charset);
final String value = idx > 0 && it.length() > idx + 1 ? URLDecoder.decode(it.substring(idx + 1), charset) : null;
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy