Example usage for com.google.common.net HostAndPort withDefaultPort

List of usage examples for com.google.common.net HostAndPort withDefaultPort

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort withDefaultPort.

Prototype

public HostAndPort withDefaultPort(int defaultPort) 

Source Link

Document

Provide a default port if the parsed string contained only a host.

Usage

From source file:com.github.jcustenborder.kafka.connect.utils.config.ConfigUtils.java

static HostAndPort hostAndPort(String input, Integer defaultPort) {
    final HostAndPort result = HostAndPort.fromString(input);

    if (null != defaultPort) {
        result.withDefaultPort(defaultPort);
    }/*ww w  .j  a va2  s.  c o  m*/

    return result;
}

From source file:com.github.jcustenborder.kafka.connect.utils.config.validators.ValidHostAndPort.java

void validate(final String setting, final String input) {
    HostAndPort hostAndPort = HostAndPort.fromString(input);
    if (this.requireBracketsForIPv6) {
        hostAndPort = hostAndPort.requireBracketsForIPv6();
    }//from w  w  w.j  ava2  s  .co m
    if (null != this.defaultPort) {
        hostAndPort.withDefaultPort(this.defaultPort);
    }

    if (Strings.isNullOrEmpty(hostAndPort.getHostText())) {
        throw new ConfigException(String.format("'%s'(%s) host cannot be blank or null.", setting, input));
    }

    if (this.portRequired && !hostAndPort.hasPort()) {
        throw new ConfigException(String.format("'%s'(%s) must specify a port.", setting, input));
    }

}

From source file:com.eightkdata.mongowp.client.wrapper.MongoClientConfiguration.java

public MongoClientConfiguration(HostAndPort hostAndPort, SocketFactory socketFactory, boolean sslEnabled,
        boolean sslAllowInvalidHostnames,
        ImmutableList<MongoAuthenticationConfiguration> authenticationConfiguration) {
    super();// w  ww .  jav  a2 s. c  om
    this.hostAndPort = hostAndPort.withDefaultPort(27017);
    this.socketFactory = socketFactory;
    this.sslEnabled = sslEnabled;
    this.sslAllowInvalidHostnames = sslAllowInvalidHostnames;
    this.authenticationConfigurations = authenticationConfiguration;
}

From source file:org.seedstack.consul.internal.ConsulPlugin.java

private Consul buildRemoteConsul(String consulClientName, ConsulConfig.ClientConfig consulClientConfig,
        CryptoPlugin cryptoPlugin) {// w ww. j av a2 s. c om
    Builder consulBuilder = Consul.builder();

    // Base options
    String url = consulClientConfig.getUrl();
    HostAndPort host = consulClientConfig.getHost();
    if (!Strings.isNullOrEmpty(url)) {
        consulBuilder.withUrl(consulClientConfig.getUrl());
    } else if (host != null) {
        consulBuilder.withHostAndPort(host.withDefaultPort(DEFAULT_CONSUL_PORT));
    } else {
        throw SeedException.createNew(ConsulErrorCode.NO_URL_OR_HOST_SPECIFIED).put("consulClientName",
                consulClientName);
    }
    Optional.ofNullable(consulClientConfig.getAclToken()).filter(s -> !s.isEmpty())
            .ifPresent(consulBuilder::withAclToken);
    consulBuilder.withPing(consulClientConfig.isPing());

    // Credentials
    String username = consulClientConfig.getUser();
    String password = consulClientConfig.getPassword();
    if (!Strings.isNullOrEmpty(username) && password != null) {
        consulBuilder.withBasicAuth(username, password);
    }

    // Timeouts
    ConsulConfig.ClientConfig.TimeoutConfig timeouts = consulClientConfig.getTimeouts();
    Optional.ofNullable(timeouts.getConnect()).ifPresent(consulBuilder::withConnectTimeoutMillis);
    Optional.ofNullable(timeouts.getRead()).ifPresent(consulBuilder::withReadTimeoutMillis);
    Optional.ofNullable(timeouts.getWrite()).ifPresent(consulBuilder::withWriteTimeoutMillis);

    // Custom classes
    Optional.ofNullable(consulClientConfig.getHostnameVerifier()).map(this::instantiateClass)
            .ifPresent(consulBuilder::withHostnameVerifier);
    Optional.ofNullable(consulClientConfig.getConsulBookend()).map(this::instantiateClass)
            .ifPresent(consulBuilder::withConsulBookend);
    Optional.ofNullable(consulClientConfig.getExecutorService()).map(this::instantiateClass)
            .ifPresent(consulBuilder::withExecutorService);

    // Proxy (consulBuilder::withProxy) is already handled by JVM-wide proxy configured in seed-core

    // SSL
    cryptoPlugin.sslContext().ifPresent(consulBuilder::withSslContext);

    // HTTP headers
    consulBuilder.withHeaders(consulClientConfig.getHeaders());

    Consul consul;
    try {
        consul = consulBuilder.build();
        LOGGER.info("Building Consul client {} for remote instance at {}", consulClientName,
                !Strings.isNullOrEmpty(url) ? url : host);
    } catch (Exception e) {
        throw SeedException.createNew(ConsulErrorCode.CANNOT_CREATE_CLIENT).put("consulClientName",
                consulClientName);
    }

    return consul;
}