Java Code Examples for org.apache.http.conn.util.InetAddressUtils#isIPv6Address()

The following examples show how to use org.apache.http.conn.util.InetAddressUtils#isIPv6Address() . 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: NetBasicInfo.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
public String GetIp(Boolean isV4) {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements(); ) {

                InetAddress inetAddress = ipAddr.nextElement();
                if(isV4) {
                    // ipv4地址
                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                        return inetAddress.getHostAddress();
                    }
                }else{
                    // ipv6地址
                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv6Address(inetAddress.getHostAddress())) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (Exception ex) {
        //
    }
    return "";
}
 
Example 2
Source File: AbstractCommonHostnameVerifierDef.java    From steady with Apache License 2.0 5 votes vote down vote up
private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting "+hostname, uhe);
        return hostname;
    }
}
 
Example 3
Source File: WebTools.java    From zrlog with Apache License 2.0 5 votes vote down vote up
/**
 * 处理由于浏览器使用透明代理,或者是WebServer运行在诸如 nginx/apache 这类 HttpServer后面的情况,通过获取请求头真实IP地址
 *
 * @param request
 * @return
 */
public static String getRealIp(HttpServletRequest request) {
    String ip = null;
    //bae env
    if (ZrLogUtil.isBae() && request.getHeader("clientip") != null) {
        ip = request.getHeader("clientip");
    }
    if (ip == null || ip.length() == 0) {
        ip = request.getHeader("X-forwarded-for");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    if (InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip)) {
        return ip;
    }
    throw new IllegalArgumentException(ip + " not ipAddress");
}
 
Example 4
Source File: ConnectionConfiguration.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the host to use when establishing the connection. The host and port to use
 * might have been resolved by a DNS lookup as specified by the XMPP spec (and therefore
 * may not match the {@link #getServiceName service name}.
 *
 * @return the host to use when establishing the connection.
 */
public String getHost() {
    // 如果不是IP,则尝试将它以域名进行IP转换。
    if(!InetAddressUtils.isIPv4Address(host) && !InetAddressUtils.isIPv6Address(host)) {
        try {
            InetAddress address = InetAddress.getByName(host);
            host =  address.getHostAddress();
            Log.d(LOG_TAG, "transform host name to host address:"+host);
        } catch (UnknownHostException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return host;
}
 
Example 5
Source File: AbstractVerifierDef.java    From steady with Apache License 2.0 5 votes vote down vote up
private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting "+hostname, uhe);
        return hostname;
    }
}
 
Example 6
Source File: J_AbstractVerifier_F.java    From steady with Apache License 2.0 5 votes vote down vote up
private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting "+hostname, uhe);
        return hostname;
    }
}
 
Example 7
Source File: AbstractCommonHostnameVerifierFix.java    From steady with Apache License 2.0 5 votes vote down vote up
private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting "+hostname, uhe);
        return hostname;
    }
}
 
Example 8
Source File: J_AbstractVerifier_V.java    From steady with Apache License 2.0 5 votes vote down vote up
private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting "+hostname, uhe);
        return hostname;
    }
}
 
Example 9
Source File: J_AbstractVerifier_F.java    From steady with Apache License 2.0 4 votes vote down vote up
private static boolean isIPAddress(final String hostname) {
    return hostname != null &&
        (InetAddressUtils.isIPv4Address(hostname) ||
                InetAddressUtils.isIPv6Address(hostname));
}
 
Example 10
Source File: AbstractVerifierFix.java    From steady with Apache License 2.0 4 votes vote down vote up
private static boolean isIPAddress(final String hostname) {
    return hostname != null &&
        (InetAddressUtils.isIPv4Address(hostname) ||
                InetAddressUtils.isIPv6Address(hostname));
}
 
Example 11
Source File: URIBuilder.java    From RoboZombie with Apache License 2.0 4 votes vote down vote up
private String buildString() {
    StringBuilder sb = new StringBuilder();
    if (this.scheme != null) {
        sb.append(this.scheme).append(':');
    }
    if (this.encodedSchemeSpecificPart != null) {
        sb.append(this.encodedSchemeSpecificPart);
    } else {
        if (this.encodedAuthority != null) {
            sb.append("//").append(this.encodedAuthority);
        } else if (this.host != null) {
            sb.append("//");
            if (this.encodedUserInfo != null) {
                sb.append(this.encodedUserInfo).append("@");
            } else if (this.userInfo != null) {
                sb.append(encodeUserInfo(this.userInfo)).append("@");
            }
            if (InetAddressUtils.isIPv6Address(this.host)) {
                sb.append("[").append(this.host).append("]");
            } else {
                sb.append(this.host);
            }
            if (this.port >= 0) {
                sb.append(":").append(this.port);
            }
        }
        if (this.encodedPath != null) {
            sb.append(normalizePath(this.encodedPath));
        } else if (this.path != null) {
            sb.append(encodePath(normalizePath(this.path)));
        }
        if (this.encodedQuery != null) {
            sb.append("?").append(this.encodedQuery);
        } else if (this.queryParams != null) {
            sb.append("?").append(encodeQuery(this.queryParams));
        }
    }
    if (this.encodedFragment != null) {
        sb.append("#").append(this.encodedFragment);
    } else if (this.fragment != null) {
        sb.append("#").append(encodeFragment(this.fragment));
    }
    return sb.toString();
}
 
Example 12
Source File: AbstractCommonHostnameVerifierDef.java    From steady with Apache License 2.0 4 votes vote down vote up
private static boolean isIPAddress(final String hostname) {
    return hostname != null &&
        (InetAddressUtils.isIPv4Address(hostname) ||
                InetAddressUtils.isIPv6Address(hostname));
}
 
Example 13
Source File: PrivacyEnforcementService.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
/**
 * Masks ip v6 address by replacing last number of groups .
 */
private static String maskIpv6(String ip, Integer groupsNumber) {
    return ip != null && InetAddressUtils.isIPv6Address(ip) ? maskIp(ip, ":", groupsNumber) : ip;
}
 
Example 14
Source File: AbstractVerifierDef.java    From steady with Apache License 2.0 4 votes vote down vote up
private static boolean isIPAddress(final String hostname) {
    return hostname != null &&
        (InetAddressUtils.isIPv4Address(hostname) ||
                InetAddressUtils.isIPv6Address(hostname));
}
 
Example 15
Source File: Util.java    From Connect-SDK-Android-Core with Apache License 2.0 4 votes vote down vote up
public static boolean isIPv6Address(String ipAddress) {
    return InetAddressUtils.isIPv6Address(ipAddress);
}
 
Example 16
Source File: RequestEntityRestStorageService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
private static Jets3tProperties toProperties(final Host bookmark, final S3Protocol.AuthenticationHeaderSignatureVersion signatureVersion) {
    final Jets3tProperties properties = new Jets3tProperties();
    final Preferences preferences = PreferencesFactory.get();
    if(log.isDebugEnabled()) {
        log.debug(String.format("Configure for endpoint %s", bookmark));
    }
    // Use default endpoint for region lookup
    if(bookmark.getHostname().endsWith(preferences.getProperty("s3.hostname.default"))) {
        // Only for AWS
        properties.setProperty("s3service.s3-endpoint", preferences.getProperty("s3.hostname.default"));
        properties.setProperty("s3service.disable-dns-buckets",
            String.valueOf(preferences.getBoolean("s3.bucket.virtualhost.disable")));
    }
    else {
        properties.setProperty("s3service.s3-endpoint", bookmark.getHostname());
        if(InetAddressUtils.isIPv4Address(bookmark.getHostname()) || InetAddressUtils.isIPv6Address(bookmark.getHostname())) {
            properties.setProperty("s3service.disable-dns-buckets", String.valueOf(true));
        }
        else {
            properties.setProperty("s3service.disable-dns-buckets",
                String.valueOf(preferences.getBoolean("s3.bucket.virtualhost.disable")));
        }
    }
    properties.setProperty("s3service.enable-storage-classes", String.valueOf(true));
    if(StringUtils.isNotBlank(bookmark.getProtocol().getContext())) {
        if(!Scheme.isURL(bookmark.getProtocol().getContext())) {
            properties.setProperty("s3service.s3-endpoint-virtual-path",
                PathNormalizer.normalize(bookmark.getProtocol().getContext()));
        }
    }
    properties.setProperty("s3service.https-only", String.valueOf(bookmark.getProtocol().isSecure()));
    if(bookmark.getProtocol().isSecure()) {
        properties.setProperty("s3service.s3-endpoint-https-port", String.valueOf(bookmark.getPort()));
    }
    else {
        properties.setProperty("s3service.s3-endpoint-http-port", String.valueOf(bookmark.getPort()));
    }
    // The maximum number of retries that will be attempted when an S3 connection fails
    // with an InternalServer error. To disable retries of InternalError failures, set this to 0.
    properties.setProperty("s3service.internal-error-retry-max", String.valueOf(0));
    // The maximum number of concurrent communication threads that will be started by
    // the multi-threaded service for upload and download operations.
    properties.setProperty("s3service.max-thread-count", String.valueOf(1));
    properties.setProperty("httpclient.proxy-autodetect", String.valueOf(false));
    properties.setProperty("httpclient.retry-max", String.valueOf(0));
    properties.setProperty("storage-service.internal-error-retry-max", String.valueOf(0));
    properties.setProperty("storage-service.request-signature-version", signatureVersion.toString());
    properties.setProperty("storage-service.disable-live-md5", String.valueOf(true));
    properties.setProperty("storage-service.default-region", bookmark.getRegion());
    return properties;
}
 
Example 17
Source File: HostParser.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isv6Address(final String address) {
    if(IPV6_STD_PATTERN.matcher(address).matches()) {
        return true;
    }
    return InetAddressUtils.isIPv6Address(address);
}
 
Example 18
Source File: URIBuilder.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
private String buildString(Charset charset) {
    final StringBuilder sb = new StringBuilder();
    if (this.scheme != null) {
        sb.append(this.scheme).append(':');
    }
    if (this.encodedSchemeSpecificPart != null) {
        sb.append(this.encodedSchemeSpecificPart);
    } else {
        if (this.encodedAuthority != null) {
            sb.append("//").append(this.encodedAuthority);
        } else if (this.host != null) {
            sb.append("//");
            if (this.encodedUserInfo != null) {
                sb.append(this.encodedUserInfo).append("@");
            } else if (this.userInfo != null) {
                sb.append(encodeUserInfo(this.userInfo, charset)).append("@");
            }
            if (InetAddressUtils.isIPv6Address(this.host)) {
                sb.append("[").append(this.host).append("]");
            } else {
                sb.append(this.host);
            }
            if (this.port >= 0) {
                sb.append(":").append(this.port);
            }
        }
        if (this.encodedPath != null) {
            sb.append(normalizePath(this.encodedPath));
        } else if (this.path != null) {
            sb.append(encodePath(normalizePath(this.path), charset));
        }
        if (this.encodedQuery != null) {
            sb.append("?").append(this.encodedQuery);
        } else if (this.queryParams != null) {
            sb.append("?").append(encodeQuery(this.queryParams, charset));
        }
    }
    if (this.encodedFragment != null) {
        sb.append("#").append(this.encodedFragment);
    } else if (this.fragment != null) {
        sb.append("#").append(encodeFragment(this.fragment, charset));
    }
    return sb.toString();
}
 
Example 19
Source File: AbstractCommonHostnameVerifierFix.java    From steady with Apache License 2.0 4 votes vote down vote up
private static boolean isIPAddress(final String hostname) {
    return hostname != null &&
        (InetAddressUtils.isIPv4Address(hostname) ||
                InetAddressUtils.isIPv6Address(hostname));
}
 
Example 20
Source File: URIBuilder.java    From android-open-project-demo with Apache License 2.0 4 votes vote down vote up
private String buildString(Charset charset) {
    final StringBuilder sb = new StringBuilder();
    if (this.scheme != null) {
        sb.append(this.scheme).append(':');
    }
    if (this.encodedSchemeSpecificPart != null) {
        sb.append(this.encodedSchemeSpecificPart);
    } else {
        if (this.encodedAuthority != null) {
            sb.append("//").append(this.encodedAuthority);
        } else if (this.host != null) {
            sb.append("//");
            if (this.encodedUserInfo != null) {
                sb.append(this.encodedUserInfo).append("@");
            } else if (this.userInfo != null) {
                sb.append(encodeUserInfo(this.userInfo, charset)).append("@");
            }
            if (InetAddressUtils.isIPv6Address(this.host)) {
                sb.append("[").append(this.host).append("]");
            } else {
                sb.append(this.host);
            }
            if (this.port >= 0) {
                sb.append(":").append(this.port);
            }
        }
        if (this.encodedPath != null) {
            sb.append(normalizePath(this.encodedPath));
        } else if (this.path != null) {
            sb.append(encodePath(normalizePath(this.path), charset));
        }
        if (this.encodedQuery != null) {
            sb.append("?").append(this.encodedQuery);
        } else if (this.queryParams != null) {
            sb.append("?").append(encodeQuery(this.queryParams, charset));
        }
    }
    if (this.encodedFragment != null) {
        sb.append("#").append(this.encodedFragment);
    } else if (this.fragment != null) {
        sb.append("#").append(encodeFragment(this.fragment, charset));
    }
    return sb.toString();
}