Refine search
/** * Retrieves the host and port from the specified URI. * * @param uriString URI to retrieve the host and port from * @return the host and port from the URI as a String * @throws URISyntaxException if the specified URI is invalid or cannot be parsed */ public static String getHostAndPortFromUri(String uriString) throws URISyntaxException { URI uri = new URI(uriString); if (uri.getPort() == -1) { return uri.getHost(); } else { return HostAndPort.fromParts(uri.getHost(), uri.getPort()).toString(); } }
private List<InetSocketAddress> parseServerIpAddresses(String dnsServerIps) { // Parse and prepare DNS server IP addresses for Netty. return StreamSupport // Split comma-separated sever IP:port combos. .stream(Splitter.on(",").trimResults().omitEmptyStrings().split(dnsServerIps).spliterator(), false) // Parse as HostAndPort objects (allows convenient handling of port provided after colon). .map(hostAndPort -> HostAndPort.fromString(hostAndPort).withDefaultPort(DnsClient.DEFAULT_DNS_PORT)) // Convert HostAndPort > InetSocketAddress as required by Netty. .map(hostAndPort -> new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort())) .collect(Collectors.toList()); }
private static Iterable<InetSocketAddress> getZkAddresses(URI uri) { String zkServers = getZKServersFromDLUri(uri); String[] zkServerList = StringUtils.split(zkServers, ','); ImmutableList.Builder<InetSocketAddress> builder = ImmutableList.builder(); for (String zkServer : zkServerList) { HostAndPort hostAndPort = HostAndPort.fromString(zkServer).withDefaultPort(2181); builder.add(InetSocketAddress.createUnresolved( hostAndPort.getHostText(), hostAndPort.getPort())); } return builder.build(); }
/** * Provide a default port if the parsed string contained only a host. * * <p>You can chain this after {@link #fromString(String)} to include a port in case the port was * omitted from the input string. If a port was already provided, then this method is a no-op. * * @param defaultPort a port number, from [0..65535] * @return a HostAndPort instance, guaranteed to have a defined port. */ public HostAndPort withDefaultPort(int defaultPort) { checkArgument(isValidPort(defaultPort)); if (hasPort()) { return this; } return new HostAndPort(host, defaultPort, hasBracketlessColons); }
/** * Build a HostAndPort instance from a host only. * * <p>Note: Non-bracketed IPv6 literals are allowed. Use {@link #requireBracketsForIPv6()} to * prohibit these. * * @param host the host-only string to parse. Must not contain a port number. * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if {@code host} contains a port number. * @since 17.0 */ public static HostAndPort fromHost(String host) { HostAndPort parsedHost = fromString(host); checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host); return parsedHost; }
public static InetSocketAddress getInetSocketAddress(final String hostPortString, final Integer defaultPort) { final HostAndPort hostAndPort = HostAndPort.fromString(hostPortString); if (defaultPort != null) { return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(defaultPort)); } return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort()); }
public static URI parseServer(String server) { server = server.toLowerCase(ENGLISH); if (server.startsWith("http://") || server.startsWith("https://")) { return URI.create(server); } HostAndPort host = HostAndPort.fromString(server); try { return new URI("http", null, host.getHost(), host.getPortOrDefault(80), null, null, null); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
@Test(groups = {"Integration"}) public void testStartupAndShutdown() { elasticSearchNode = app.createAndManageChild(EntitySpec.create(ElasticSearchNode.class)); app.start(ImmutableList.of(testLocation)); EntityAsserts.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, true); String url = elasticSearchNode.sensors().get(ElasticSearchNode.DATASTORE_URL); assertNotNull(url); assertTrue(Networking.isReachable(HostAndPort.fromParts(URI.create(url).getHost(), URI.create(url).getPort()))); elasticSearchNode.stop(); EntityAsserts.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, false); }
@Test public void testHostAndPortWithUnbracketedIpv6() { URI uri = uriBuilder() .scheme("http") .port(8888) .hostAndPort(HostAndPort.fromParts("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", 8081)) .build(); assertEquals(uri.toASCIIString(), "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8081"); }
HostAndPort hp = HostAndPort.fromString(host) .withDefaultPort(port); LOG.info("Connecting to {}:{}", hp.getHostText(), hp.getPort()); return new URI(THRIFT_SCHEMA, null, hp.getHostText(), hp.getPort(), null, null, null);
public StaticHiveCluster(List<URI> metastoreUris, String metastoreUsername, HiveMetastoreClientFactory clientFactory) { requireNonNull(metastoreUris, "metastoreUris is null"); checkArgument(!metastoreUris.isEmpty(), "metastoreUris must specify at least one URI"); this.addresses = metastoreUris.stream() .map(StaticHiveCluster::checkMetastoreUri) .map(uri -> HostAndPort.fromParts(uri.getHost(), uri.getPort())) .collect(toList()); this.metastoreUsername = metastoreUsername; this.clientFactory = requireNonNull(clientFactory, "clientFactory is null"); }
@Test public void testHostAndPortWithHostOnly() { URI uri = uriBuilder() .scheme("http") .port(8888) .hostAndPort(HostAndPort.fromString("example.com")) .build(); assertEquals(uri.toASCIIString(), "http://example.com"); }
final InetAddress inetAddress = toInetAddress(bindAddress.getHost()); if (inetAddress != null && Tools.isWildcardInetAddress(inetAddress)) { final InetAddress guessedAddress; publishUri = new URI( getUriScheme(), null, guessedAddress.getHostAddress(), bindAddress.getPort(), path, null, publishUri = new URI( getUriScheme(), null, getHttpBindAddress().getHost(), getHttpBindAddress().getPort(), path, null,
private static TTransport createRaw(HostAndPort address, Optional<SSLContext> sslContext, Optional<HostAndPort> socksProxy, int timeoutMillis) throws TTransportException { Proxy proxy = socksProxy .map(socksAddress -> new Proxy(SOCKS, InetSocketAddress.createUnresolved(socksAddress.getHost(), socksAddress.getPort()))) .orElse(Proxy.NO_PROXY); Socket socket = new Socket(proxy); try { socket.connect(new InetSocketAddress(address.getHost(), address.getPort()), timeoutMillis); socket.setSoTimeout(timeoutMillis); if (sslContext.isPresent()) { // SSL will connect to the SOCKS address when present HostAndPort sslConnectAddress = socksProxy.orElse(address); socket = sslContext.get().getSocketFactory().createSocket(socket, sslConnectAddress.getHost(), sslConnectAddress.getPort(), true); } return new TSocket(socket); } catch (Throwable t) { // something went wrong, close the socket and rethrow try { socket.close(); } catch (IOException e) { t.addSuppressed(e); } throw new TTransportException(t); } }
@Test(dependsOnMethods = "testAssignToLoadBalancerRule", expectedExceptions = SshException.class) public void testRemoveFromLoadBalancerRule() throws Exception { if (networksDisabled) throw new SshException(); assertTrue(jobComplete.apply(client.getLoadBalancerClient().removeVirtualMachinesFromLoadBalancerRule( rule.getId(), vm.getId()))); assertEquals(client.getLoadBalancerClient().listVirtualMachinesAssignedToLoadBalancerRule(rule.getId()).size(), 0); assertEquals(rule.getState(), State.ADD); checkSSH(HostAndPort.fromParts(ip.getIPAddress(), 22)); }
@Test public void testReturnsReachable() throws Exception { SocketOpen secondSocketOpen = new SocketOpen() { @Override public boolean apply(HostAndPort input) { return HostAndPort.fromParts("1.2.3.5", 22).equals(input); } }; OpenSocketFinder finder = new ConcurrentOpenSocketFinder(secondSocketOpen, nodeRunning, userExecutor); HostAndPort result = finder.findOpenSocketOnNode(node, 22, 2000, MILLISECONDS); assertEquals(result, HostAndPort.fromParts("1.2.3.5", 22)); }
@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()); } } }