/** * @return Function that returns {@code true} when there's a response from the URL; otherwise {@code false} */ public static Callable<Boolean> responseFrom(final URL url) { return () -> { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); if (conn instanceof HttpsURLConnection) { // relax host and certificate checks as we just want to see if it's up ((HttpsURLConnection) conn).setHostnameVerifier((hostname, session) -> true); URLUtils.prepareForSSL(conn); } conn.setRequestMethod("HEAD"); conn.connect(); return true; } finally { if (conn != null) { conn.disconnect(); } } }; }
/** * Prepare url for authentication and ssl if necessary and returns the input stream from the url. * * @param url url to prepare * @param acceptAnyCertificate true if the certicate check should be skipped * * @return input stream from url * * @throws IOException re-thrown */ public static InputStream prepareInputStream( final URL url, final boolean acceptAnyCertificate ) throws IOException { final URLConnection conn = url.openConnection(); prepareForAuthentication( conn ); prepareHttpHeaders( conn ); if( acceptAnyCertificate ) { prepareForSSL( conn ); } return conn.getInputStream(); }