| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.utils; |
| 19 | |
|
| 20 | |
import java.io.IOException; |
| 21 | |
import java.net.Authenticator; |
| 22 | |
import java.net.HttpURLConnection; |
| 23 | |
import java.net.InetSocketAddress; |
| 24 | |
import java.net.PasswordAuthentication; |
| 25 | |
import java.net.Proxy; |
| 26 | |
import java.net.SocketAddress; |
| 27 | |
import java.net.URL; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public final class URLConnectionFactory { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
private URLConnectionFactory() { |
| 41 | |
} |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException { |
| 52 | 0 | HttpURLConnection conn = null; |
| 53 | |
Proxy proxy; |
| 54 | 0 | final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER); |
| 55 | |
try { |
| 56 | 0 | if (proxyUrl != null) { |
| 57 | 0 | final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT); |
| 58 | 0 | final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort); |
| 59 | |
|
| 60 | 0 | final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME); |
| 61 | 0 | final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD); |
| 62 | 0 | if (username != null && password != null) { |
| 63 | 0 | final Authenticator auth = new Authenticator() { |
| 64 | |
@Override |
| 65 | |
public PasswordAuthentication getPasswordAuthentication() { |
| 66 | 0 | if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) { |
| 67 | 0 | return new PasswordAuthentication(username, password.toCharArray()); |
| 68 | |
} |
| 69 | 0 | return super.getPasswordAuthentication(); |
| 70 | |
} |
| 71 | |
}; |
| 72 | 0 | Authenticator.setDefault(auth); |
| 73 | |
} |
| 74 | |
|
| 75 | 0 | proxy = new Proxy(Proxy.Type.HTTP, address); |
| 76 | 0 | conn = (HttpURLConnection) url.openConnection(proxy); |
| 77 | 0 | } else { |
| 78 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 79 | |
} |
| 80 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 60000); |
| 81 | 0 | conn.setConnectTimeout(timeout); |
| 82 | 0 | } catch (IOException ex) { |
| 83 | 0 | if (conn != null) { |
| 84 | |
try { |
| 85 | 0 | conn.disconnect(); |
| 86 | |
} finally { |
| 87 | 0 | conn = null; |
| 88 | 0 | } |
| 89 | |
} |
| 90 | 0 | throw new URLConnectionFailureException("Error getting connection.", ex); |
| 91 | 0 | } |
| 92 | 0 | return conn; |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public static HttpURLConnection createHttpURLConnection(URL url, boolean proxy) throws URLConnectionFailureException { |
| 105 | 0 | if (proxy) { |
| 106 | 0 | return createHttpURLConnection(url); |
| 107 | |
} |
| 108 | 0 | HttpURLConnection conn = null; |
| 109 | |
try { |
| 110 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 111 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 60000); |
| 112 | 0 | conn.setConnectTimeout(timeout); |
| 113 | 0 | } catch (IOException ioe) { |
| 114 | 0 | throw new URLConnectionFailureException("Error getting connection.", ioe); |
| 115 | 0 | } |
| 116 | 0 | return conn; |
| 117 | |
} |
| 118 | |
} |