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

com.github.httpmock.request.Url Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package com.github.httpmock.request;

import static com.github.httpmock.util.CollectionUtil.emptyMap;

import java.util.Map;

import com.github.httpmock.util.KeyValuePair;
import com.github.httpmock.util.UrlNormalizer;

public class Url {

	private String path;
	private Map queryParameters;

	public Url(String url) {
		this.path = parsePath(url);
		this.queryParameters = parseQueryParameters(url);
	}

	private String parsePath(String url) {
		String path = url.split("[?]")[0];
		return UrlNormalizer.normalizeUrl(path);
	}

	private Map parseQueryParameters(String url) {
		String parameterString = parseParameterString(url);
		if (parameterString.equals(""))
			return emptyMap();

		return extractParametersFromQueryString(parameterString);
	}

	private Map extractParametersFromQueryString(
			String parameterString) {
		Map parameters = emptyMap();
		String[] keyValuePairs = parameterString.split("&");
		for (String keyValuePairString : keyValuePairs) {
			KeyValuePair keyValuePair = getKeyValuePair(keyValuePairString);
			parameters.put(keyValuePair.getKey(), keyValuePair.getValue());
		}
		return parameters;
	}

	private KeyValuePair getKeyValuePair(
			String keyValuePairString) {
		String[] keyValuePair = keyValuePairString.split("=");
		if (keyValuePair.length == 1)
			return new KeyValuePair<>(keyValuePair[0], null);
		return new KeyValuePair<>(keyValuePair[0], keyValuePair[1]);
	}

	private String parseParameterString(String url) {
		String[] urlParts = url.split("[?]");
		if (urlParts.length > 1)
			return urlParts[1];
		return "";
	}

	public String getPath() {
		return path;
	}

	public Map getQueryParameters() {
		return queryParameters;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy