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

com.appslandia.sweetsop.http.HttpUrl Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.sweetsop.http;

import com.appslandia.common.utils.DateUtils;
import com.appslandia.common.utils.URLEncoding;

/**
 * 
 * @author Loc Ha
 */
public class HttpUrl {

	private static final char QUERY_PARAM_SEP = '=';

	protected final StringBuilder url;
	private boolean questMark = true;

	public HttpUrl(String baseUrl) {
		this.url = new StringBuilder().append(baseUrl);
	}

	public HttpUrl(String scheme, String domain, String basePath) {
		this(scheme, domain, 0, basePath);
	}

	public HttpUrl(String scheme, String domain, int port, String basePath) {
		StringBuilder url = new StringBuilder();
		url.append(scheme).append("://").append(domain);
		if ((port != 0) && (port != 80) && (port != 443)) {
			url.append(':').append(port);
		}
		url.append(basePath);
		this.url = url;
	}

	protected void prepareQuery() {
		if (this.questMark) {
			this.url.append('?');
			this.questMark = false;
		} else {
			this.url.append('&');
		}
	}

	public HttpUrl path(String value) {
		this.url.append(value);
		return this;
	}

	public HttpUrl path(int value) {
		this.url.append(value);
		return this;
	}

	public HttpUrl path(long value) {
		this.url.append(value);
		return this;
	}

	public HttpUrl page(String page) {
		this.url.append(page);
		return this;
	}

	public HttpUrl query(String name, String value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(URLEncoding.encode(value));
		}
		return this;
	}

	public HttpUrl query(String name, char[] value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(URLEncoding.encode(new String(value)));
		}
		return this;
	}

	public HttpUrl query(String name, boolean value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP).append(Boolean.toString(value));
		return this;
	}

	public HttpUrl query(String name, int value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP).append(Integer.toString(value));
		return this;
	}

	public HttpUrl query(String name, long value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP).append(Long.toString(value));
		return this;
	}

	public HttpUrl query(String name, float value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP).append(Float.toString(value));
		return this;
	}

	public HttpUrl query(String name, double value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP).append(Double.toString(value));
		return this;
	}

	public HttpUrl query(String name, Boolean value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(value.toString());
		}
		return this;
	}

	public HttpUrl query(String name, Integer value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(value.toString());
		}
		return this;
	}

	public HttpUrl query(String name, Long value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(value.toString());
		}
		return this;
	}

	public HttpUrl query(String name, Float value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(value.toString());
		}
		return this;
	}

	public HttpUrl query(String name, Double value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(value.toString());
		}
		return this;
	}

	public HttpUrl query(String name, java.sql.Date value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(DateUtils.iso8601Date(value));
		}
		return this;
	}

	public HttpUrl query(String name, java.sql.Time value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(URLEncoding.encode(DateUtils.iso8601Time(value)));
		}
		return this;
	}

	public HttpUrl query(String name, java.sql.Timestamp value) {
		prepareQuery();
		this.url.append(name).append(QUERY_PARAM_SEP);
		if (value != null) {
			this.url.append(URLEncoding.encode(DateUtils.iso8601DateTime(value)));
		}
		return this;
	}

	@Override
	public String toString() {
		return this.url.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy