org.springframework.util.CheckUtils Maven / Gradle / Ivy
package org.springframework.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.client.ClientHttpRequestFactoryCustom;
public final class CheckUtils {
private static final Log logger = LogFactory.getLog(CheckUtils.class);
private static final String DEFAULT_PROTOCOL = "TLS";
public static final HostnameVerifier HOSTNAME_VERIFIER = ClientHttpRequestFactoryCustom.createHostnameVerifier();
public static final SSLSocketFactory SSL_SOCKET_FACTORY = ClientHttpRequestFactoryCustom.createSSLContext(DEFAULT_PROTOCOL).getSocketFactory();
/**
* @param type ({@code DIRECT}, {@code HTTP}, {@code SOCKS})
*/
public static Proxy createProxy(Type type, String host, int port) {
return new Proxy(type, InetSocketAddress.createUnresolved(host, port));
}
public static boolean isReachable(String hostname, int port, int timeout) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(hostname, port), timeout);
return true;
}
catch (IOException e) {
// logger.debug("Not reachable... from {}:{} ({})", hostname, port, e.getMessage());
return false;
}
}
/**
* {@code -Dsun.net.client.defaultConnectTimeout=1000}
*
* {@code -Dsun.net.client.defaultReadTimeout=1000}
*
* @see java.net.InetAddress#getByName(String)
* @see java.net.InetAddress#isReachable(java.net.NetworkInterface, int, int)
* @see java.net.NetworkInterface#getByInetAddress(InetAddress)
* @see java.net.InetSocketAddress#InetSocketAddress(String, int)
* @see java.net.Socket#connect(java.net.SocketAddress, int)
* @see javax.net.ssl.SSLSocketFactory#getDefault()
* @see javax.net.ssl.SSLSocketFactory#createSocket(String, int)
* @see java.net.Socket#setSoTimeout(int)
* @see java.net.Socket#isConnected()
* @see java.net.HttpURLConnection#getErrorStream()
*/
public static boolean isReachable(URL url) {
URLConnection urlConnection = null;
// try {
// HttpURLConnection httpURLConnection = (urlConnection = url.openConnection())
// instanceof HttpURLConnection ? (HttpURLConnection) urlConnection : null;
// }
// catch (IOException e1) {
// }
// try {
// urlConnection = url.openConnection();
// }
// catch (IOException e) {
// return false;
// }
// new AutoCloseableHttpURLConnection(httpURLConnection);
try (Reader reader = new InputStreamReader((urlConnection = url.openConnection()).getInputStream(), StandardCharsets.UTF_8)) {
return true;
}
catch (IOException e) {
// logger.debug("Not Reachable... from {} ({})", url, e.getMessage());
return false;
}
finally {
if (urlConnection instanceof HttpURLConnection) {
((HttpURLConnection) urlConnection).disconnect();
}
}
}
// public static boolean isReachablsse(URL url) {
// HttpURLConnection httpURLConnection = null;
//
// URLConnection urlConnection = url.openConnection();
//
// try (Reader reader = new InputStreamReader((httpURLConnection =
// url.openConnection()).getInputStream(), StandardCharsets.UTF_8)) {
// return true;
// }
// catch (IOException e) {
// log.debug("Not Reachable... from {} ({})", url, e.getMessage());
// return false;
// }
// finally {
// if (httpURLConnection instanceof HttpURLConnection) {
// ((HttpURLConnection) httpURLConnection).disconnect();
// }
// }
// }
/**
* @see org.springframework.util.StreamUtils#drain(InputStream)
*/
public static InputStream getInputStream(HttpURLConnection connection) throws IOException {
InputStream inputStream = connection.getErrorStream();
return (inputStream == null ? connection.getInputStream() : inputStream);
}
/**
* {@code -Djavax.net.ssl.debug=all}
*/
public static Collection getCertificates(URL url, int timeout) {
try {
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpsURLConnection) {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) urlConnection;
httpsURLConnection.setSSLSocketFactory(SSL_SOCKET_FACTORY);
httpsURLConnection.setHostnameVerifier(HOSTNAME_VERIFIER);
try {
httpsURLConnection.connect();
}
finally {
if (httpsURLConnection != null) {
httpsURLConnection.disconnect();
}
}
return Arrays.asList(httpsURLConnection.getServerCertificates());
}
}
catch (IOException e) {
// logger.warn("Connection fail... from {} {}", url, e.getMessage());
// logger.trace("Connection fail... from {}", url, e);
}
return Collections.emptySet();
}
}
/**
* @see javax.net.ssl.TrustManagerFactory#getInstance(String)
* @see javax.net.ssl.TrustManagerFactory#getDefaultAlgorithm()
* @see java.security.KeyStore#getInstance(String)
* @see java.security.KeyStore#getDefaultType()
* @see javax.net.ssl.TrustManagerFactory#init(java.security.KeyStore)
* @see javax.net.ssl.TrustManagerFactory#getTrustManagers()
*/
class TrustManagerImpl implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}
class HostnameVerifierImpl implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}