Skip to content

Instantly share code, notes, and snippets.

@RaffaeleSgarro
Last active December 15, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaffaeleSgarro/5273970 to your computer and use it in GitHub Desktop.
Save RaffaeleSgarro/5273970 to your computer and use it in GitHub Desktop.
Encode/decode parameters in a format that is suitable to be sent as the encoded query string to Yahoo services with a HTTP API
package stackoverflow;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class YahooURI {
public static void main(String[] args) throws Exception {
Multimap p = new Multimap();
p.put("foo", "bar");
p.remove("foo", "404");
p.put("bar", "A string with spaces");
p.put("baz", "àèìòù");
p.put("qoo", "a b c");
p.put("qoo", "A string, with comma");
p.put("qoo", "à");
p.remove("qoo", "a b c");
String serialized = encode(p);
System.out.println(serialized);
p = decode(serialized);
System.out.println(p);
}
static String encode(Multimap data) throws UnsupportedEncodingException {
StringBuilder str = new StringBuilder();
String delim = "";
for (Entry<String, Set<String>> params : data) {
str.append(delim);
str.append(URLEncoder.encode(params.getKey(), "UTF8"));
str.append("=");
String delim2 = "";
for (String value : params.getValue()) {
// I think this is the only way to pass a literal comma
str.append(delim2).append(URLEncoder.encode(value, "UTF8").replace(",", "%2C"));
delim2 = ",";
}
delim = "&";
}
return str.toString();
}
static Multimap decode(String src) throws UnsupportedEncodingException {
Multimap result = new Multimap();
for (String token : src.split("&")) {
String[] pair = token.split("=");
String key = URLDecoder.decode(pair[0], "UTF8");
for (String value : pair[1].split(",")) {
result.put(key, URLDecoder.decode(value, "UTF8"));
}
}
return result;
}
static class Multimap implements Iterable<Entry<String, Set<String>>>{
Map<String, Set<String>> data = new LinkedHashMap<>();
public Set<String> get(String key) {
return data.get(key);
}
public void put(String key, String value) {
Set<String> set = get(key);
if (set == null) {
set = new HashSet<>();
data.put(key, set);
}
set.add(value);
}
public void remove(String key, String value) {
Set<String> set = get(key);
if (set != null) {
set.remove(value);
if (set.isEmpty()) {
data.remove(key);
}
}
}
@Override
public Iterator<Entry<String, Set<String>>> iterator() {
return data.entrySet().iterator();
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
String delim = "";
for (Entry<String, Set<String>> entry : data.entrySet()) {
for (String value : entry.getValue()) {
str.append(delim);
str.append(entry.getKey()).append(" -> ").append(value);
delim = "\n";
}
}
return str.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment