@Override public String getHost() { return syncSource.getHost(); }
private static InetSocketAddress toUnresolvedAddress(HostAndPort address) { return InetSocketAddress.createUnresolved(address.getHost(), address.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); } }
private 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); } }
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()); }
@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()); } } }
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); } }
/** * @param ipAddresses A comma-separated list of IP addresses * @return true if all comma-separated IP addresses are valid * "8.8.4.4, 8.8.8.8" returns true * "8.8.4.4, " returns true * "8.8.4.4" returns true * "8.8.4.4 8.8.8.8" returns false * "8.8.4.4, google.com" returns false */ public static boolean allIpAddressesValid(String ipAddresses) { if (!Strings.isNullOrEmpty(ipAddresses)) { return Lists.newArrayList(Splitter.on(",") .trimResults() .omitEmptyStrings() .split(ipAddresses)).stream() .map(hostAndPort -> HostAndPort.fromString(hostAndPort).withDefaultPort(DnsClient.DEFAULT_DNS_PORT)) .allMatch(hostAndPort -> isValidIpAddress(hostAndPort.getHost())); } return false; } }
private void addLocations(FileBlockInfo fileBlockInfo) { Set<String> locations = new HashSet<>(); // add alluxio locations for (BlockLocation location : fileBlockInfo.getBlockInfo().getLocations()) { locations.add(location.getWorkerAddress().getHost()); } // add underFS locations for (String location : fileBlockInfo.getUfsLocations()) { locations.add(HostAndPort.fromString(location).getHost()); } mLocations.addAll(locations); }
private void reconnect(Connection connection, String hostAndPort) { HostAndPort h_p = HostAndPort.fromString(hostAndPort); connections.get(hostAndPort).remove(connection); connection.close(); addConnection(h_p.getHost(), h_p.getPort(), false); }
@ValidatorMethod @SuppressWarnings("unused") public void validateHttpBindAddress() throws ValidationException { try { final String host = getHttpBindAddress().getHost(); if (!InetAddresses.isInetAddress(host)) { final InetAddress inetAddress = InetAddress.getByName(host); } } catch (IllegalArgumentException | UnknownHostException e) { throw new ValidationException(e); } }
/** * Parse the authority part of a URI. The authority part may have one of the following formats: * <ul> * <li>{@code "group:<groupName>"} for a group endpoint</li> * <li>{@code "<host>:<port>"} for a host endpoint</li> * <li>{@code "<host>"} for a host endpoint with no port number specified</li> * </ul> * An IPv4 or IPv6 address can be specified in lieu of a host name, e.g. {@code "127.0.0.1:8080"} and * {@code "[::1]:8080"}. */ public static Endpoint parse(String authority) { requireNonNull(authority, "authority"); if (authority.startsWith("group:")) { return ofGroup(authority.substring(6)); } final HostAndPort parsed = HostAndPort.fromString(authority).withDefaultPort(0); return create(parsed.getHost(), parsed.getPort()); }
final String host = parsedHost.getHost();
public void testFromParts() { HostAndPort hp = HostAndPort.fromParts("gmail.com", 81); assertEquals("gmail.com", hp.getHost()); assertTrue(hp.hasPort()); assertEquals(81, hp.getPort()); try { HostAndPort.fromParts("gmail.com:80", 81); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { HostAndPort.fromParts("gmail.com", -1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
public void testRequireBracketsForIPv6() { // Bracketed IPv6 works fine. assertEquals("::1", HostAndPort.fromString("[::1]").requireBracketsForIPv6().getHost()); assertEquals("::1", HostAndPort.fromString("[::1]:80").requireBracketsForIPv6().getHost()); // Non-bracketed non-IPv6 works fine. assertEquals("x", HostAndPort.fromString("x").requireBracketsForIPv6().getHost()); assertEquals("x", HostAndPort.fromString("x:80").requireBracketsForIPv6().getHost()); // Non-bracketed IPv6 fails. try { HostAndPort.fromString("::1").requireBracketsForIPv6(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
/** * 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; }
public static TTransport create( HostAndPort address, Optional<SSLContext> sslContext, Optional<HostAndPort> socksProxy, int timeoutMillis, HiveMetastoreAuthentication authentication) throws TTransportException { try { TTransport rawTransport = createRaw(address, sslContext, socksProxy, timeoutMillis); TTransport authenticatedTransport = authentication.authenticate(rawTransport, address.getHost()); if (!authenticatedTransport.isOpen()) { authenticatedTransport.open(); } return new TTransportWrapper(authenticatedTransport, address); } catch (TTransportException e) { throw rewriteException(e, address); } }
public void testFromHost() { HostAndPort hp = HostAndPort.fromHost("gmail.com"); assertEquals("gmail.com", hp.getHost()); assertFalse(hp.hasPort()); hp = HostAndPort.fromHost("[::1]"); assertEquals("::1", hp.getHost()); assertFalse(hp.hasPort()); try { HostAndPort.fromHost("gmail.com:80"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { HostAndPort.fromHost("[gmail.com]"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
/** * Converts wire type to proto type. * * @param fileBlockInfo the wire representation to convert * @return converted proto representation */ public static alluxio.grpc.FileBlockInfo toProto(FileBlockInfo fileBlockInfo) { List<alluxio.grpc.WorkerNetAddress> ufsLocations = new ArrayList<>(); for (String ufsLocation : fileBlockInfo.getUfsLocations()) { HostAndPort address = HostAndPort.fromString(ufsLocation); ufsLocations.add(alluxio.grpc.WorkerNetAddress.newBuilder().setHost(address.getHost()) .setDataPort(address.getPortOrDefault(-1)).build()); } return alluxio.grpc.FileBlockInfo.newBuilder() .setBlockInfo(toProto(fileBlockInfo.getBlockInfo())).setOffset(fileBlockInfo.getOffset()) .addAllUfsLocations(ufsLocations).addAllUfsStringLocations(fileBlockInfo.getUfsLocations()) .build(); }