net.java.trueupdate.installer.cargo.Uris Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trueupdate-installer-cargo Show documentation
Show all versions of trueupdate-installer-cargo Show documentation
Provides an update installer for applications running in a container
which is supported by the generic Cargo API.
/*
* Copyright (C) 2013 Schlichtherle IT Services & Stimulus Software.
* All rights reserved. Use is subject to license terms.
*/
package net.java.trueupdate.installer.cargo;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
/**
* Provides functions for {@link URI}s.
*
* @author Christian Schlichtherle
*/
final class Uris {
/**
* Returns the decoded query parameters of the given URI as a multi-valued
* map.
* Every list value has at least one element, which may be an empty string.
*/
static Map> queryParameters(final URI uri) {
final Map> map = new LinkedHashMap>();
final String rawQuery = uri.getRawQuery();
if (null != rawQuery) {
for (final String parameter : rawQuery.split("&")) {
final int i = parameter.indexOf('=');
final String name;
final String value;
if (0 <= i) {
name = decode(parameter.substring(0, i));
value = decode(parameter.substring(i + 1));
} else {
name = decode(parameter);
value = "";
}
List list = map.get(name);
if (null == list)
map.put(name, list = new LinkedList());
list.add(value);
}
}
return map;
}
private static String decode(final String queryParameter) {
try {
return new URI("?" + queryParameter).getQuery();
} catch (URISyntaxException ex) {
throw new AssertionError(ex);
}
}
}