Java Code Examples for com.google.common.net.HostAndPort#fromString()

The following examples show how to use com.google.common.net.HostAndPort#fromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyToServerConnection.java    From PowerTunnel with MIT License 6 votes vote down vote up
/**
 * Build an {@link InetSocketAddress} for the given hostAndPort.
 *
 * @param hostAndPort String representation of the host and port
 * @param proxyServer the current {@link DefaultHttpProxyServer}
 * @return a resolved InetSocketAddress for the specified hostAndPort
 * @throws UnknownHostException if hostAndPort could not be resolved, or if the input string could not be parsed into
 *          a host and port.
 */
public static InetSocketAddress addressFor(String hostAndPort, DefaultHttpProxyServer proxyServer)
        throws UnknownHostException {
    HostAndPort parsedHostAndPort;
    try {
        parsedHostAndPort = HostAndPort.fromString(hostAndPort);
    } catch (IllegalArgumentException e) {
        // we couldn't understand the hostAndPort string, so there is no way we can resolve it.
        throw new UnknownHostException(hostAndPort);
    }

    String host = parsedHostAndPort.getHost();
    int port = parsedHostAndPort.getPortOrDefault(80);

    return proxyServer.getServerResolver().resolve(host, port);
}
 
Example 2
Source File: TSOClient.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
@Override
public void nodeChanged() throws Exception {

    String tsoInfo = getCurrentTSOInfoFoundInZK(zkCurrentTsoPath);
    // TSO info includes the new TSO host:port address and epoch
    String[] currentTSOAndEpochArray = tsoInfo.split("#");
    HostAndPort hp = HostAndPort.fromString(currentTSOAndEpochArray[0]);
    setTSOAddress(hp.getHostText(), hp.getPort());
    epoch = Long.parseLong(currentTSOAndEpochArray[1]);
    LOG.info("CurrentTSO ZNode changed. New TSO Host & Port {}/Epoch {}", hp, getEpoch());
    if (currentChannel != null && currentChannel.isConnected()) {
        LOG.info("\tClosing channel with previous TSO {}", currentChannel);
        currentChannel.close();
    }

}
 
Example 3
Source File: SplitWork.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public List<EndpointAffinity> getAffinity() {
  List<EndpointAffinity> endpoints = new ArrayList<>();
  for(Affinity a : datasetSplit.getAffinitiesList()){
    HostAndPort hostAndPort = HostAndPort.fromString(a.getHost());
    NodeEndpoint endpoint = getMatchingNode(hostAndPort);
    if(endpoint != null){
      endpoints.add(new EndpointAffinity(hostAndPort, endpoint, a.getFactor(), affinityType == DistributionAffinity.HARD, affinityType == DistributionAffinity.HARD ? 1 : Integer.MAX_VALUE));
    } else {
      if (affinityType == DistributionAffinity.HARD) {
        // Throw an error if there is no endpoint on host
        throw UserException.resourceError()
            .message("No executors are available for data with hard affinity. " +
                "You may consider using \"registration.publish-host\" property if your network rules change.")
            .addContext("available executors %s", nodeMap.getHosts())
            .addContext("data affinity", a.getHost())
            .build(logger);
      }
    }
  }
  return endpoints;
}
 
Example 4
Source File: WinRmMachineLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
    super.init();

    // Register any pre-existing port-mappings with the PortForwardManager
    Map<Integer, String> tcpPortMappings = getConfig(TCP_PORT_MAPPINGS);
    if (tcpPortMappings != null) {
        PortForwardManager pfm = (PortForwardManager) getManagementContext().getLocationRegistry().getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC);
        for (Map.Entry<Integer, String> entry : tcpPortMappings.entrySet()) {
            int targetPort = entry.getKey();
            HostAndPort publicEndpoint = HostAndPort.fromString(entry.getValue());
            if (!publicEndpoint.hasPort()) {
                throw new IllegalArgumentException("Invalid portMapping ('"+entry.getValue()+"') for port "+targetPort+" in machine "+this);
            }
            pfm.associate(publicEndpoint.getHostText(), publicEndpoint, this, targetPort);
        }
    }
}
 
Example 5
Source File: KubernetesLocationYamlLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Assumes that the container entity uses port 8080.
 */
protected <T extends Entity> T runTomcat(String yaml, Class<T> type) throws Exception {
    Entity app = createStartWaitAndLogApplication(yaml);
    T entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, type));

    Entities.dumpInfo(app);
    String publicMapped = assertAttributeEventuallyNonNull(entity, Sensors.newStringSensor("docker.port.8080.mapped.public"));
    HostAndPort publicPort = HostAndPort.fromString(publicMapped);

    assertReachableEventually(publicPort);
    assertHttpStatusCodeEventuallyEquals("http://" + publicPort.getHostText() + ":" + publicPort.getPort(), 200);

    return entity;
}
 
Example 6
Source File: KubernetesLocationYamlLiveTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Live"})
public void testNetcatServer() throws Exception {
    // Runs as root user (hence not `sudo yum install ...`)
    // breaks if shell.env uses attributeWhenReady, so not doing that - see testNetcatServerWithDslInShellEnv()
    String yaml = Joiner.on("\n").join(
            locationYaml,
            "services:",
            "  - type: " + VanillaSoftwareProcess.class.getName(),
            "    brooklyn.parameters:",
            "      - name: netcat.port",
            "        type: port",
            "        default: 8081",
            "    brooklyn.config:",
            "      install.command: |",
            "        yum install -y nc",
            "      launch.command: |",
            "        echo $MESSAGE | nc -l $NETCAT_PORT &",
            "        echo $! > $PID_FILE",
            "      shell.env:",
            "        MESSAGE: mymessage",
            "        NETCAT_PORT: $brooklyn:attributeWhenReady(\"netcat.port\")",
            "    brooklyn.enrichers:",
            "      - type: " + OnPublicNetworkEnricher.class.getName(),
            "        brooklyn.config:",
            "          " + OnPublicNetworkEnricher.SENSORS.getName() + ":",
            "            - netcat.port");

    Entity app = createStartWaitAndLogApplication(yaml);
    VanillaSoftwareProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaSoftwareProcess.class));

    String publicMapped = assertAttributeEventuallyNonNull(entity, Sensors.newStringSensor("netcat.endpoint.mapped.public"));
    HostAndPort publicPort = HostAndPort.fromString(publicMapped);

    assertTrue(Networking.isReachable(publicPort), "publicPort=" + publicPort);
}
 
Example 7
Source File: ResolvedHostnameCacheFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) {
    // the address *should* always be resolved at this point
    InetAddress resolvedAddress = resolvedRemoteAddress.getAddress();

    if (resolvedAddress != null) {
        // place the resolved host into the hostname cache, so subsequent requests will be able to identify the IP address
        HostAndPort parsedHostAndPort = HostAndPort.fromString(serverHostAndPort);
        String host = parsedHostAndPort.getHost();

        if (host != null && !host.isEmpty()) {
            resolvedAddresses.put(host, resolvedAddress.getHostAddress());
        }
    }
}
 
Example 8
Source File: ForeignDataCenterEndPointProviderTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndPointProvider() {
    InvalidationServiceEndPointAdapter adapter = new InvalidationServiceEndPointAdapter("emodb-cachemgr",
            HostAndPort.fromString("localhost:8080"), HostAndPort.fromString("localhost:8081"));

    DataCenter dataCenter1 = mockDataCenter("us-east", "http://emodb.qa.us-east-1.nexus.bazaarvoice.com:8081");
    DataCenter dataCenter2 = mockDataCenter("us-west-2", "http://emodb.qa.us-west-2.nexus.bazaarvoice.com:8081");
    DataCenter dataCenter3 = mockDataCenter("eu-west-2", "http://emodb.qa.eu-west-2.nexus.bazaarvoice.com:8081");

    DataCenters dataCenters = mock(DataCenters.class);
    when(dataCenters.getSelf()).thenReturn(dataCenter3);
    when(dataCenters.getAll()).thenReturn(ImmutableList.of(dataCenter1, dataCenter2, dataCenter3));

    ForeignDataCenterEndPointProvider provider = new ForeignDataCenterEndPointProvider(adapter, dataCenters);

    final boolean[] called = {false};
    provider.withEndPoints(new Function<Collection<EndPoint>, Void>() {
        @Override
        public Void apply(Collection<EndPoint> endPoints) {
            called[0] = true;

            Set<String> expected = new HashSet<>();
            expected.add("http://emodb.qa.us-east-1.nexus.bazaarvoice.com:8081/tasks/invalidate");
            expected.add("http://emodb.qa.us-west-2.nexus.bazaarvoice.com:8081/tasks/invalidate");

            for (EndPoint endPoint : endPoints) {
                Assert.assertTrue(endPoint.isValid());
                Assert.assertTrue(expected.remove(endPoint.getAddress()));
            }
            Assert.assertTrue(expected.isEmpty());

            return null;
        }
    });
    Assert.assertTrue(called[0]);
}
 
Example 9
Source File: SingerMain.java    From singer with Apache License 2.0 5 votes vote down vote up
public static void startOstrichService(SingerConfig singerConfig) {
  // do not start ostrich if Ostrich server is disabled 
  if (System.getenv(SingerMetrics.DISABLE_SINGER_OSTRICH) == null) {
    OstrichAdminService ostrichService = new OstrichAdminService(singerConfig.getOstrichPort());
    ostrichService.start();
  }
  // enable high granularity metrics we are running in canary
  if (singerConfig.isSetStatsPusherHostPort()) {
    LOG.info("Starting the stats pusher");
    try {
      @SuppressWarnings("unchecked")
      Class<StatsPusher> pusherClass = (Class<StatsPusher>) Class.forName(singerConfig.getStatsPusherClass());
      statsPusher = pusherClass.newInstance();
      HostAndPort pushHostPort = HostAndPort.fromString(singerConfig.getStatsPusherHostPort());
      // allows hostname to be overridden based on environment
      statsPusher.configure(SingerSettings.getEnvironment().getHostname()
          , singerConfig.isShadowModeEnabled()? SHADOW_METRICS_PREFIX : SINGER_METRICS_PREFIX
          , pushHostPort.getHost()
          , pushHostPort.getPort()
          , singerConfig.getStatsPusherFrequencyInSeconds() * 1000);
      statsPusher.start();
      LOG.info("Stats pusher started!");
    } catch (Throwable t) {
      // pusher fail is OK, do
      LOG.error("Exception when starting stats pusher: ", t);
    }
  }
}
 
Example 10
Source File: ServiceConfigManager.java    From grpc-swagger with MIT License 5 votes vote down vote up
@Nullable
public static HostAndPort getEndPoint(String fullServiceName) {
    ServiceConfig config = STORAGE.get(fullServiceName);
    if (config == null) {
        return null;
    }
    return HostAndPort.fromString(config.getEndPoint());
}
 
Example 11
Source File: HttpsAwareFiltersAdapter.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Returns the hostname (but not the port) the specified request for both HTTP and HTTPS requests.  The request may reflect
 * modifications from this or other filters. This filter instance must be currently handling the specified request;
 * otherwise the results are undefined.
 *
 * @param modifiedRequest a possibly-modified version of the request currently being processed
 * @return hostname of the specified request, without the port
 */
public String getHost(HttpRequest modifiedRequest) {
    String serverHost;
    if (isHttps()) {
        HostAndPort hostAndPort = HostAndPort.fromString(getHttpsRequestHostAndPort());
        serverHost = hostAndPort.getHost();
    } else {
        serverHost = HttpUtil.getHostFromRequest(modifiedRequest);
    }
    return serverHost;
}
 
Example 12
Source File: Replica.java    From TakinRPC with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Replica fromString(@Nonnull String info) {
    try {
        checkNotNull(info);
        HostAndPort hostAndPort = HostAndPort.fromString(info);
        InetAddress addr = InetAddress.getByName(hostAndPort.getHostText());
        InetSocketAddress saddr = new InetSocketAddress(addr, hostAndPort.getPort());
        return new Replica(saddr);
    } catch (UnknownHostException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 13
Source File: LocalhostSelfAddressProvider.java    From Ratel with Apache License 2.0 4 votes vote down vote up
@Override
public HostAndPort getHostAndPort() {
    LOGGER.warn("No explicit self address given. Assuming localhost:80.");
    return HostAndPort.fromString(LOCALHOST_80);
}
 
Example 14
Source File: ProxyToServerConnection.java    From g4proxy with Apache License 2.0 4 votes vote down vote up
/**
 * This method initializes our {@link ConnectionFlow} based on however this connection has been configured. If
 * the {@link #disableSni} value is true, this method will not pass peer information to the MitmManager when
 * handling CONNECTs.
 */
private void initializeConnectionFlow() {
    this.connectionFlow = new ConnectionFlow(clientConnection, this,
            connectLock)
            .then(ConnectChannel);

    if (chainedProxy != null && chainedProxy.requiresEncryption()) {
        connectionFlow.then(serverConnection.EncryptChannel(chainedProxy
                .newSslEngine()));
    }

    if (ProxyUtils.isCONNECT(initialRequest)) {
        // If we're chaining, forward the CONNECT request
        if (hasUpstreamChainedProxy()) {
            connectionFlow.then(
                    serverConnection.HTTPCONNECTWithChainedProxy);
        }

        MitmManager mitmManager = proxyServer.getMitmManager();
        boolean isMitmEnabled = mitmManager != null;

        if (isMitmEnabled) {
            // When MITM is enabled and when chained proxy is set up, remoteAddress
            // will be the chained proxy's address. So we use serverHostAndPort
            // which is the end server's address.
            HostAndPort parsedHostAndPort = HostAndPort.fromString(serverHostAndPort);

            // SNI may be disabled for this request due to a previous failed attempt to connect to the server
            // with SNI enabled.
            if (disableSni) {
                connectionFlow.then(serverConnection.EncryptChannel(proxyServer.getMitmManager()
                        .serverSslEngine()));
            } else {
                connectionFlow.then(serverConnection.EncryptChannel(proxyServer.getMitmManager()
                        .serverSslEngine(parsedHostAndPort.getHost(), parsedHostAndPort.getPort())));
            }

        	connectionFlow
                    .then(clientConnection.RespondCONNECTSuccessful)
                    .then(serverConnection.MitmEncryptClientChannel);
        } else {
            connectionFlow.then(serverConnection.StartTunneling)
                    .then(clientConnection.RespondCONNECTSuccessful)
                    .then(clientConnection.StartTunneling);
        }
    }
}
 
Example 15
Source File: HostAndPortMapper.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Object map(TreeNode treeNode, Type type) {
    return HostAndPort.fromString(treeNode.value());
}
 
Example 16
Source File: LocalDataCenterEndPointProviderTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testEndPointProviderWithDelay() throws Exception {
    InvalidationServiceEndPointAdapter adapter = new InvalidationServiceEndPointAdapter("emodb-cachemgr",
            HostAndPort.fromString("localhost:8080"), HostAndPort.fromString("localhost:8081"));
    ServiceEndPoint self = adapter.toSelfEndPoint();

    MetricRegistry metricRegistry = new MetricRegistry();

    ZooKeeperServiceRegistry serviceRegistry = new ZooKeeperServiceRegistry(_curator, metricRegistry);
    ServiceEndPoint host1 = getRemoteHost("169.254.1.1");
    ServiceEndPoint host2 = getRemoteHost("169.254.1.2");
    serviceRegistry.register(host1);

    SimpleLifeCycleRegistry lifeCycleRegistry = new SimpleLifeCycleRegistry();

    // To control for the unreliability of thread timing provide our own implementation for the invalidation service
    ExecutorService service = mock(ExecutorService.class);

    // Simulate the service being shutdown after exactly one iteration
    when(service.isShutdown())
            .thenReturn(false)
            .thenReturn(true);

    LocalDataCenterEndPointProvider provider = new LocalDataCenterEndPointProvider(
            _curator, adapter, self, metricRegistry, lifeCycleRegistry, service);
    lifeCycleRegistry.start();

    final Set<String> endPointsInvalidated = Sets.newHashSet();

    assertWithin(provider, Duration.ZERO, endPoints -> {
        endPoints.forEach(endPoint -> endPointsInvalidated.add(endPoint.getAddress()));
        return null;
    });

    Assert.assertEquals(endPointsInvalidated, ImmutableSet.of("http://169.254.1.1:8081/tasks/invalidate"));
    endPointsInvalidated.clear();

    // Register the second host
    serviceRegistry.register(host2);

    // Now that the second host is definitively registered execute the invalidation runnable synchronously
    ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(service).execute(runnableCaptor.capture());
    Runnable runnable = runnableCaptor.getValue();
    runnable.run();
    
    Assert.assertEquals(endPointsInvalidated, ImmutableSet.of("http://169.254.1.2:8081/tasks/invalidate"));

    serviceRegistry.unregister(host1);
    serviceRegistry.close();
    lifeCycleRegistry.stop();
}
 
Example 17
Source File: RedisCommandInvoker.java    From hanboDB with Apache License 2.0 4 votes vote down vote up
public void startAsyncReplication(String slaverHost) {
    HostAndPort hostAndPort = HostAndPort.fromString(slaverHost);
    Jedis jedis = new Jedis(hostAndPort.getHostText(), hostAndPort.getPort(), 60 * 1000, 60 * 1000);
    jedis.ping();
    slaverHostList.add(jedis);
}
 
Example 18
Source File: TSOClient.java    From phoenix-omid with Apache License 2.0 4 votes vote down vote up
private TSOClient(OmidClientConfiguration omidConf) throws IOException {

        // Start client with Nb of active threads = 3 as maximum.
        int tsoExecutorThreads = omidConf.getExecutorThreads();

        factory = new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(
                        new ThreadFactoryBuilder().setNameFormat("tsoclient-boss-%d").build()),
                Executors.newCachedThreadPool(
                        new ThreadFactoryBuilder().setNameFormat("tsoclient-worker-%d").build()), tsoExecutorThreads);
        // Create the bootstrap
        bootstrap = new ClientBootstrap(factory);

        requestTimeoutInMs = omidConf.getRequestTimeoutInMs();
        requestMaxRetries = omidConf.getRequestMaxRetries();
        tsoReconnectionDelayInSecs = omidConf.getReconnectionDelayInSecs();

        LOG.info("Connecting to TSO...");
        HostAndPort hp;
        switch (omidConf.getConnectionType()) {
            case HA:
                zkClient = ZKUtils.initZKClient(omidConf.getConnectionString(),
                                                omidConf.getZkNamespace(),
                                                omidConf.getZkConnectionTimeoutInSecs());
                zkCurrentTsoPath = omidConf.getZkCurrentTsoPath();
                configureCurrentTSOServerZNodeCache(zkCurrentTsoPath);
                String tsoInfo = getCurrentTSOInfoFoundInZK(zkCurrentTsoPath);
                // TSO info includes the new TSO host:port address and epoch
                String[] currentTSOAndEpochArray = tsoInfo.split("#");
                hp = HostAndPort.fromString(currentTSOAndEpochArray[0]);
                setTSOAddress(hp.getHostText(), hp.getPort());
                epoch = Long.parseLong(currentTSOAndEpochArray[1]);
                LOG.info("\t* Current TSO host:port found in ZK: {} Epoch {}", hp, getEpoch());
                break;
            case DIRECT:
            default:
                hp = HostAndPort.fromString(omidConf.getConnectionString());
                setTSOAddress(hp.getHostText(), hp.getPort());
                LOG.info("\t* TSO host:port {} will be connected directly", hp);
                break;
        }

        fsmExecutor = Executors.newSingleThreadScheduledExecutor(
                new ThreadFactoryBuilder().setNameFormat("tsofsm-%d").build());
        fsm = new StateMachine.FsmImpl(fsmExecutor);
        fsm.setInitState(new DisconnectedState(fsm));

        ChannelPipeline pipeline = bootstrap.getPipeline();
        pipeline.addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(8 * 1024, 0, 4, 0, 4));
        pipeline.addLast("lengthprepender", new LengthFieldPrepender(4));
        pipeline.addLast("protobufdecoder", new ProtobufDecoder(TSOProto.Response.getDefaultInstance()));
        pipeline.addLast("protobufencoder", new ProtobufEncoder());
        pipeline.addLast("handler", new Handler(fsm));

        bootstrap.setOption("tcpNoDelay", true);
        bootstrap.setOption("keepAlive", true);
        bootstrap.setOption("reuseAddress", true);
        bootstrap.setOption("connectTimeoutMillis", 100);
        lowLatency = false;



        conflictDetectionLevel = omidConf.getConflictAnalysisLevel();

    }
 
Example 19
Source File: CassandraConfiguration.java    From emodb with Apache License 2.0 4 votes vote down vote up
private com.datastax.driver.core.Cluster.Builder newCqlDriverBuilder(ConnectionPoolConfiguration poolConfig,
                                                                     MetricRegistry metricRegistry) {
    performHostDiscovery(metricRegistry);

    String[] seeds = _seeds.split(",");
    List<String> contactPoints = Lists.newArrayListWithCapacity(seeds.length);

    // Each seed may be a host name or a host name and port (e.g.; "1.2.3.4" or "1.2.3.4:9160").  These need
    // to be converted into host names only.
    for (String seed : seeds) {
        HostAndPort hostAndPort = HostAndPort.fromString(seed);
        seed = hostAndPort.getHostText();
        if (hostAndPort.hasPort()) {
            if (hostAndPort.getPort() == _thriftPort) {
                _log.debug("Seed {} found using RPC port; swapping for native port {}", seed, _cqlPort);
            } else if (hostAndPort.getPort() != _cqlPort) {
                throw new IllegalArgumentException(String.format(
                        "Seed %s found with invalid port %s.  The port must match either the RPC (thrift) port %s " +
                        "or the native (CQL) port %s", seed, hostAndPort.getPort(), _thriftPort, _cqlPort));
            }
        }

        contactPoints.add(seed);
    }

    PoolingOptions poolingOptions = new PoolingOptions();
    if (poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).isPresent()) {
        poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).get());
    }
    if (poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).isPresent()) {
        poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).get());
    }

    SocketOptions socketOptions = new SocketOptions();
    if (poolConfig.getConnectTimeout().or(getConnectTimeout()).isPresent()) {
        socketOptions.setConnectTimeoutMillis(poolConfig.getConnectTimeout().or(getConnectTimeout()).get());
    }
    if (poolConfig.getSocketTimeout().or(getSocketTimeout()).isPresent()) {
        socketOptions.setReadTimeoutMillis(poolConfig.getSocketTimeout().or(getSocketTimeout()).get());
    }

    AuthProvider authProvider = _authenticationCredentials != null
            ? new PlainTextAuthProvider(_authenticationCredentials.getUsername(), _authenticationCredentials.getPassword())
            : AuthProvider.NONE;

    return com.datastax.driver.core.Cluster.builder()
            .addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
            .withPort(_cqlPort)
            .withPoolingOptions(poolingOptions)
            .withSocketOptions(socketOptions)
            .withRetryPolicy(Policies.defaultRetryPolicy())
            .withAuthProvider(authProvider);
}
 
Example 20
Source File: BrowserUpHttpUtil.java    From browserup-proxy with Apache License 2.0 3 votes vote down vote up
/**
 * Removes a port from a host+port if the string contains the specified port. If the host+port does not contain
 * a port, or contains another port, the string is returned unaltered. For example, if hostWithPort is the
 * string {@code www.website.com:443}, this method will return {@code www.website.com}.
 *
 * <b>Note:</b> The hostWithPort string is not a URI and should not contain a scheme or resource. This method does
 * not attempt to validate the specified host; it <i>might</i> throw IllegalArgumentException if there was a problem
 * parsing the hostname, but makes no guarantees. In general, it should be validated externally, if necessary.
 *
 * @param hostWithPort string containing a hostname and optional port
 * @param portNumber port to remove from the string
 * @return string with the specified port removed, or the original string if it did not contain the portNumber
 */
public static String removeMatchingPort(String hostWithPort, int portNumber) {
    HostAndPort parsedHostAndPort = HostAndPort.fromString(hostWithPort);
    if (parsedHostAndPort.hasPort() && parsedHostAndPort.getPort() == portNumber) {
        // HostAndPort.getHostText() strips brackets from ipv6 addresses, so reparse using fromHost
        return HostAndPort.fromHost(parsedHostAndPort.getHost()).toString();
    } else {
        return hostWithPort;
    }
}