public static String setHost(String addr, String host) { int hostStart = getHostStart(addr); boolean ipv6 = isIpv6(addr); int hostEnd = getHostEnd(addr, ipv6, hostStart); if(hostEnd < 0 && hostStart < 0) { return host; } String prefix = hostStart > 0? addr.substring(0, hostStart) : ""; String suff = hostEnd > 0? addr.substring(hostEnd) : ""; return prefix + host + suff; }
/** * Return address with swarm port, it differ from usual http port and use some binary protocol. * @see DockerClusterConfig#getSwarmPort() * @param service service of node * @return address with swarm port */ private String getSwarmAddress(DockerService service) { String host = AddressUtils.getHost(service.getAddress()); return AddressUtils.setPort(host, config.getSwarmPort()); }
private String getContainerUri(String containerId, NodeRegistration nr) { String addr = nr.getNodeInfo().getAddress(); String host = AddressUtils.getHostPort(addr); String proto = AddressUtils.isHttps(addr)? "wss" : "ws"; return proto + "://" + host + "/containers/" + containerId + "/attach/ws?stream=true&stdin=true&stdout=true&stderr=true"; }
/** * Retrieve node address. Note that node may report incorrect address (for example 127.0.0.1), therefore we * must not prefer it over manually entered value (in other words - not replace existed address). * @param sn swarm node object * @return string with host and port */ private String getNodeAddress(SwarmNode sn) { if(sn == null) { return null; } String address = sn.getStatus().getAddress(); if(StringUtils.isEmpty(address)) { return address; } if(!AddressUtils.hasPort(address)) { address = AddressUtils.setPort(address, 2375); } return address; }
private String getAddress(String predefinedAddress, AbstractConfigurableEmbeddedServletContainer container) { String proto = (container.getSsl() == null)? "http://" : "https://"; if(StringUtils.hasText(predefinedAddress)) { String hostPort = AddressUtils.getHostPort(predefinedAddress); return proto + hostPort; } else { int port = container.getPort(); // server must have way to fix invalid host if it need String host = "localhost"; return proto + host + ":" + port; } } }
public static String setPort(String address, int port) { Assert.notNull(address, "Address is null or empty"); Assert.isTrue(port > 0 && port < Short.MAX_VALUE, "Invalid port number: " + port); int from = 0; if(isIpv6(address)) { from = address.indexOf(']'); } int portStart = address.lastIndexOf(':'); String hostAndProto = address; if(portStart > from) { hostAndProto = address.substring(0, portStart); } return hostAndProto + ":" + port; }
/** * Extract host:port pair from url. * @param addr any url with host & port * @return host:port */ public static String getHostPort(String addr) { if (addr == null) { return null; } int hostStart = getHostStart(addr); int portEnd = addr.indexOf('/', hostStart); if(portEnd < 0 && hostStart < 0) { return addr; } if(hostStart > 0) { if(portEnd < hostStart) { return addr.substring(hostStart); } return addr.substring(hostStart, portEnd); } return addr.substring(0, portEnd); }
private AsyncRestTemplate createNewRestTemplate(String addr) { // we use async client because usual client does not allow to interruption in some cases NettyRequestFactory factory = new NettyRequestFactory(); if(AddressUtils.isHttps(addr)) { try { initSsl(addr, factory); } catch (Exception e) { log.error("", e); } } final AsyncRestTemplate restTemplate = new AsyncRestTemplate(factory); List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new HttpAuthInterceptor(registryRepository)); if(!StringUtils.isEmpty(agentPassword)) { interceptors.add(new BasicAuthAsyncInterceptor("admin", agentPassword)); } restTemplate.setInterceptors(interceptors); return restTemplate; }
/** * Extract host from url. * @param addr any url with host * @return host */ public static String getHost(String addr) { if (addr == null) { return null; } int hostStart = getHostStart(addr); boolean ipv6 = isIpv6(addr); int hostEnd = getHostEnd(addr, ipv6, hostStart); if(hostEnd < 0 && hostStart < 0) { return addr; } if(hostStart > 0) { if(hostEnd < hostStart) { return addr.substring(hostStart); } return addr.substring(hostStart, hostEnd); } return addr.substring(0, hostEnd); }