private void checkCertificateExpiryDate ( URL url , List < Metric > metrics ) throws Exception { SSLContext ctx = SSLContext . getInstance ( "TLS" ) ; ctx . init ( new KeyManager [ 0 ] , new TrustManager [ ] { new DefaultTrustManager ( ) } , new SecureRandom ( ) ) ; SSLContext . setDefault ( ctx ) ; HttpsURLConnection conn = ( HttpsURLConnection ) url . openConnection ( ) ; conn . setHostnameVerifier ( new HostnameVerifier ( ) { public boolean verify ( final String arg0 , final SSLSession arg1 ) { return true ; } } ) ; List < Date > expiryDates = new ArrayList < Date > ( ) ; conn . getResponseCode ( ) ; Certificate [ ] certs = conn . getServerCertificates ( ) ; for ( Certificate cert : certs ) { X509Certificate x509 = ( X509Certificate ) cert ; Date expiry = x509 . getNotAfter ( ) ; expiryDates . add ( expiry ) ; } conn . disconnect ( ) ; Date today = new Date ( ) ; for ( Date date : expiryDates ) { int diffInDays = ( int ) ( ( date . getTime ( ) - today . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) ) ; metrics . add ( new Metric ( "certificate" , "" , new BigDecimal ( diffInDays ) , null , null ) ) ; } }
stuff for checking certificate