/** * Parse a {@link URI} into an {@link IpPort}. * * <p> * A uri without port is allowed, in which case the port will be inferred from the scheme. {@code http} is 80, and * {@code https} is 443. * </p> * <p> * The host of the {@code uri} should not be null, or it will be treated as an illegal param, * and an {@link IllegalArgumentException} will be thrown. * </p> */ public static IpPort parseIpPort(URI uri) { return parseIpPort(uri, false); }
public static IpPort parseIpPort(String scheme, String authority) { if (authority == null) { return null; } return parseIpPort(URI.create(scheme + "://" + authority)); }
/** * The format of address should be {@code IPv4:port} or {@code [IPv6]:port}, or {@code host:port}, * or you will not get expected result. * * Note that the IPv6 address should be wrapped by square brackets. * @return IpPort parsed from input param, or {@code null} if the param is null. */ public static IpPort parseIpPort(String address) { if (address == null) { return null; } URI uri = URI.create("http://" + address); return parseIpPort(uri, true); }
/** * @param uriAddress the address containing IP and port info. * @return IpPort parsed from input param, or {@code null} if the param is null. */ public static IpPort parseIpPortFromURI(String uriAddress) { if (uriAddress == null) { return null; } try { return parseIpPort(new URI(uriAddress)); } catch (URISyntaxException e) { return null; } }
/** * 对于配置为0.0.0.0的地址,let it go * schema, e.g. http * adddress, e.g 0.0.0.0:8080 * return 实际监听的地址 */ public static String getRealListenAddress(String schema, String address) { if (address == null) { return null; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI); if (ipPort == null) { LOGGER.error("address {} is not valid.", address); return null; } return originalURI.toString(); } catch (URISyntaxException e) { LOGGER.error("address {} is not valid.", address); return null; } }
public ArrayList<IpPort> getIpPort() { List<String> uriList = Deployment.getSystemBootStrapInfo(DeploymentProvider.SYSTEM_KEY_SERVICE_CENTER).getAccessURL(); ArrayList<IpPort> ipPortList = new ArrayList<>(); uriList.forEach(anUriList -> { try { URI uri = new URI(anUriList.trim()); this.ssl = "https".equals(uri.getScheme()); ipPortList.add(NetUtils.parseIpPort(uri)); } catch (Exception e) { LOGGER.error("servicecomb.service.registry.address invalid : {}", anUriList, e); } }); return ipPortList; }
/** * In the case that listening address configured as 0.0.0.0, the publish address will be determined * by the query result for the net interfaces. * * @return the publish address, or {@code null} if the param {@code address} is null. */ public static String getPublishAddress(String schema, String address) { if (address == null) { return address; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI); if (ipPort == null) { LOGGER.warn("address {} not valid.", address); return null; } IpPort publishIpPort = genPublishIpPort(schema, ipPort); URIBuilder builder = new URIBuilder(originalURI); return builder.setHost(publishIpPort.getHostOrIp()).setPort(publishIpPort.getPort()).build().toString(); } catch (URISyntaxException e) { LOGGER.warn("address {} not valid.", address); return null; } }
public static IpPort parseIpPortFromURI(String uriAddress) { if (uriAddress == null) { return null; } try { URI uri = new URI(uriAddress); String authority = uri.getAuthority(); return parseIpPort(uri.getScheme(), authority); } catch (URISyntaxException e) { return null; } }
/** * 对于配置为0.0.0.0的地址,let it go * schema, e.g. http * adddress, e.g 0.0.0.0:8080 * return 实际监听的地址 */ public static String getRealListenAddress(String schema, String address) { if (address == null) { return null; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI.getAuthority()); if (ipPort == null) { LOGGER.error("address {} is not valid.", address); return null; } return originalURI.toString(); } catch (URISyntaxException e) { LOGGER.error("address {} is not valid.", address); return null; } }
public static IpPort parseIpPort(String scheme, String authority) { if (authority == null) { return null; } int idx = authority.indexOf(':'); if (idx != -1) { return parseIpPort(authority); } if (scheme.equals("http")) { return new IpPort(authority, 80); } if (scheme.equals("https")) { return new IpPort(authority, 443); } return null; }
public ArrayList<IpPort> getIpPort() { DynamicStringProperty property = DynamicPropertyFactory.getInstance() .getStringProperty("servicecomb.service.registry.address", "https://127.0.0.1:30100"); List<String> uriList = Arrays.asList(property.get().split(",")); ArrayList<IpPort> ipPortList = new ArrayList<>(); uriList.forEach(anUriList -> { try { URI uri = new URI(anUriList.trim()); this.ssl = "https".equals(uri.getScheme()); ipPortList.add(NetUtils.parseIpPort(uri.getScheme(), uri.getAuthority())); } catch (Exception e) { LOGGER.error("servicecomb.service.registry.address invalid : {}", anUriList, e); } }); return ipPortList; }
/** * 对于配置为0.0.0.0的地址,通过查询网卡地址,转换为实际监听的地址。 */ public static String getPublishAddress(String schema, String address) { if (address == null) { return address; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI.getAuthority()); if (ipPort == null) { LOGGER.warn("address {} not valid.", address); return null; } IpPort publishIpPort = genPublishIpPort(schema, ipPort); URIBuilder builder = new URIBuilder(originalURI); return builder.setHost(publishIpPort.getHostOrIp()).setPort(publishIpPort.getPort()).build().toString(); } catch (URISyntaxException e) { LOGGER.warn("address {} not valid.", address); return null; } }