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

io.mstream.trader.commons.config.model.BaseUrl Maven / Gradle / Ivy

package io.mstream.trader.commons.config.model;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import static java.lang.String.format;

public class BaseUrl {

    private final String protocol;
    private final String host;
    private final int port;
    private final String basePath;
    private final String url;

    @JsonCreator
    public BaseUrl(
            @JsonProperty(value = "protocol", required = true) String protocol,
            @JsonProperty(value = "host", required = true) String host,
            @JsonProperty(value = "port") Integer port,
            @JsonProperty(value = "basePath") String basePath
    ) {
        this.protocol = protocol;
        this.host = host;
        this.port = defaultPort(port, protocol);
        this.basePath = basePath == null ? "" : basePath;
        url = format("%s://%s:%d%s", protocol, host, port, basePath);
    }

    private int defaultPort(Integer port, String protocol) {
        if (port != null) {
            return port;
        }
        switch (protocol) {
            case "http":
                return 80;
            case "https":
                return 443;
            default:
                throw new IllegalArgumentException(
                        format(
                                "can't guess the default port for %s protocol",
                                protocol
                        )
                );
        }
    }

    public String url() {
        return url;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getBasePath() {
        return basePath;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy