# 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
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
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
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
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
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
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
๊ฒฐ๊ณผ:
{one=["1"], two=[2], three=["3", 3a]}
1