@ SuppressFBWarnings ( justification = "yes, there is a redundant null check in the catch - to suppress warnings we are leaving the null check" , value = { "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE" } ) public HttpURLConnection createHttpURLConnection ( URL url ) throws URLConnectionFailureException { HttpURLConnection conn = null ; final String proxyHost = settings . getString ( Settings . KEYS . PROXY_SERVER ) ; try { if ( proxyHost != null && ! matchNonProxy ( url ) ) { final int proxyPort = settings . getInt ( Settings . KEYS . PROXY_PORT ) ; final SocketAddress address = new InetSocketAddress ( proxyHost , proxyPort ) ; final String username = settings . getString ( Settings . KEYS . PROXY_USERNAME ) ; final String password = settings . getString ( Settings . KEYS . PROXY_PASSWORD ) ; if ( username != null && password != null ) { final Authenticator auth = new Authenticator ( ) { @ Override public PasswordAuthentication getPasswordAuthentication ( ) { if ( proxyHost . equals ( getRequestingHost ( ) ) || getRequestorType ( ) . equals ( Authenticator . RequestorType . PROXY ) ) { LOGGER . debug ( "Using the configured proxy username and password" ) ; try { if ( settings . getBoolean ( Settings . KEYS . PROXY_DISABLE_SCHEMAS , true ) ) { System . setProperty ( "jdk.http.auth.tunneling.disabledSchemes" , "" ) ; } } catch ( InvalidSettingException ex ) { LOGGER . trace ( "This exception can be ignored" , ex ) ; } return new PasswordAuthentication ( username , password . toCharArray ( ) ) ; } return super . getPasswordAuthentication ( ) ; } } ; Authenticator . setDefault ( auth ) ; } final Proxy proxy = new Proxy ( Proxy . Type . HTTP , address ) ; conn = ( HttpURLConnection ) url . openConnection ( proxy ) ; } else { conn = ( HttpURLConnection ) url . openConnection ( ) ; } final int connectionTimeout = settings . getInt ( Settings . KEYS . CONNECTION_TIMEOUT , 10000 ) ; conn . setConnectTimeout ( connectionTimeout ) ; conn . setInstanceFollowRedirects ( true ) ; } catch ( IOException ex ) { if ( conn != null ) { try { conn . disconnect ( ) ; } finally { conn = null ; } } throw new URLConnectionFailureException ( "Error getting connection." , ex ) ; } //conn.setRequestProperty("user-agent", // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); configureTLS ( url , conn ) ; return conn ; }
Utility method to create an HttpURLConnection . If the application is configured to use a proxy this method will retrieve the proxy settings and use them when setting up the connection .