# Parse URL Query String

# Assignment

URL :

https://google.com.ua/oauth/authorize?
client_id=SS&response_type=code&scope=N_FULL&access_type=offline&
redirect_uri=http://localhost/Callback
1
2
3

์œ„ URL Query String์„ ์•„๋ž˜์ฒ˜๋Ÿผ ํŒŒ์‹ฑํ•ด์•ผํ•œ๋‹ค.

NAME               VALUE
------------------------
client_id          SS
response_type      code
scope              N_FULL
access_type        offline
redirect_uri       http://localhost/Callback

1
2
3
4
5
6
7
8

# Solution 1

URLEncodedUtils (opens new window) ๋ฅผ ์ด์šฉํ•œ๋‹ค.

import org.apache.hc.client5.http.utils.URLEncodedUtils

String url = "https://www.sungyub.com/sung_is_awesome.html?one=1&two=2&three=3&three=3a";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");

for (NameValuePair param : params) {
  System.out.println(param.getName() + " : " + param.getValue());
}
1
2
3
4
5
6
7
8

ํ•ด๋‹น URL๋ฅผ ๋Œ๋ ค๋ณด๋ฉด:

https://www.sungyub.com/sung_is_awesome.html?one=1&two=2&three=3&three=3a
1

๊ฒฐ๊ณผ:

one : 1
two : 2
three : 3
three : 3a
1
2
3
4

# Solution 2

Google Guava (opens new window)๋ฅผ ์ด์šฉํ•œ๋‹ค.

import java.util.Map;
import com.google.common.base.Splitter;

public class Parser {
    public static void main(String... args) {
        String uri = "https://www.sungyub.com/sung_is_awesome.html?one=1&two=2&three=3&three=3a";
        String query = uri.split("\\?")[1];
        final Map<String, String> map = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(query);
        System.out.println(map);
    }
}
1
2
3
4
5
6
7
8
9
10
11

๊ฒฐ๊ณผ:

{one=1, two=2, three=3, three=3a}
1

# Solution 3

Without using an external library ๐Ÿ˜Ÿ

Java8:

public Map<String, List<String>> splitQuery(URL url) {
    if (Strings.isNullOrEmpty(url.getQuery())) {
        return Collections.emptyMap();
    }
    return Arrays.stream(url.getQuery().split("&"))
            .map(this::splitQueryParameter)
            .collect(Collectors.groupingBy(SimpleImmutableEntry::getKey, LinkedHashMap::new, mapping(Map.Entry::getValue, toList())));
}

public SimpleImmutableEntry<String, String> splitQueryParameter(String it) {
    final int idx = it.indexOf("=");
    final String key = idx > 0 ? it.substring(0, idx) : it;
    final String value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
    return new SimpleImmutableEntry<>(key, value);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Java:

public static Map<String, List<String>> splitQuery(URL url) throws UnsupportedEncodingException {
  final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
  final String[] pairs = url.getQuery().split("&");
 
  for (String pair : pairs) {
    final int idx = pair.indexOf("=");
    final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
    if (!query_pairs.containsKey(key)) {
      query_pairs.put(key, new LinkedList<String>());
    }
    final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
    
    query_pairs.get(key).add(value);
  }
  
  return query_pairs;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

๊ฒฐ๊ณผ:

{one=["1"], two=[2], three=["3", 3a]}
1