All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.king.platform.net.http.util.UriQueryParser Maven / Gradle / Ivy

There is a newer version: 3.0.26
Show newest version
// Copyright (C) king.com Ltd 2017
// https://github.com/king/king-http-client
// Author: Magnus Gustafsson
// License: Apache 2.0, https://raw.github.com/king/king-http-client/LICENSE-APACHE

package com.king.platform.net.http.util;


import io.netty.handler.codec.http.QueryStringDecoder;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UriQueryParser {
	private final QueryStringDecoder queryStringDecoder;
	private final Map> parameters;

	public UriQueryParser(String url) {
		queryStringDecoder = new QueryStringDecoder(url);
		parameters = queryStringDecoder.parameters();
	}

	public UriQueryParser(String url, Charset charset) {
		queryStringDecoder = new QueryStringDecoder(url, charset);
		parameters = queryStringDecoder.parameters();
	}

	public List getValue(String key) {
		return parameters.get(key);
	}

	public String getValueOrDefault(String key, String defValue) {
		List strings = parameters.get(key);
		if (strings == null || strings.isEmpty()) {
			return defValue;
		}
		return strings.get(0);
	}

	public boolean hasKey(String key) {
		return parameters.containsKey(key);
	}

	public List params() {
		List params = new ArrayList<>();
		for (Map.Entry> entry : parameters.entrySet()) {
			for (String value : entry.getValue()) {
				params.add(new Param(entry.getKey(), value));
			}
		}
		return params;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy