# This patch file was generated by NetBeans IDE # This patch can be applied using context Tools: Apply Diff Patch action on respective folder. # It uses platform neutral UTF-8 encoding. # Above lines and this line are ignored by the patching process. --- C:\NetBeansProjects\apache-ivy-2.2.0\src\java\org\apache\ivy\util\url\HttpClientHandler.java +++ C:\NetBeansProjects\apache-ivy-2.2.0-src.tar\src\java\org\apache\ivy\util\url\HttpClientHandler.java @@ -21,10 +21,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.net.ProxySelector; -import java.net.URISyntaxException; import java.net.URL; import java.net.UnknownHostException; import java.text.ParseException; @@ -32,8 +28,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.logging.Level; -import java.util.logging.Logger; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; @@ -41,6 +35,7 @@ import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.NTCredentials; import org.apache.commons.httpclient.auth.AuthPolicy; import org.apache.commons.httpclient.auth.AuthScheme; @@ -65,15 +60,39 @@ private static final SimpleDateFormat LAST_MODIFIED_FORMAT = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss z", Locale.US); + // proxy configuration: obtain from system properties + private int proxyPort; + + private String proxyHost = null; + private String proxyUserName = null; private String proxyPasswd = null; private HttpClientHelper httpClientHelper; + private static HttpClient httpClient; + public HttpClientHandler() { + configureProxy(); } + private void configureProxy() { + proxyHost = System.getProperty("http.proxyHost"); + // TODO constant is better ... + if (useProxy()) { + proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "80")); + proxyUserName = System.getProperty("http.proxyUser"); + proxyPasswd = System.getProperty("http.proxyPassword"); + // It seems there is no equivalent in HttpClient for + // 'http.nonProxyHosts' property + Message.verbose("proxy configured: host=" + proxyHost + " port=" + proxyPort + " user=" + + proxyUserName); + } else { + Message.verbose("no proxy configured"); + } + } + public InputStream openStream(URL url) throws IOException { GetMethod get = doGet(url, 0); if (!checkStatusCode(url, get)) { @@ -249,41 +268,31 @@ } private HttpClient getClient(URL url) { - HttpClient httpClient = new HttpClient(); + if (httpClient == null) { + final MultiThreadedHttpConnectionManager connManager = + new MultiThreadedHttpConnectionManager(); + httpClient = new HttpClient(connManager); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + public void run() { + connManager.shutdown(); + } + })); + List authPrefs = new ArrayList(3); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.NTLM); // put it at the end to give less priority (IVY-213) httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); - try { - ProxySelector proxySelector = ProxySelector.getDefault(); - List proxies = proxySelector.select(new java.net.URI(url.toExternalForm())); - // Wenn eine direkte Verbindung möglich ist => bevorzugen! - if (!proxies.contains(Proxy.NO_PROXY)) { - for (Proxy proxy : proxies) { - if (!(proxy.address() instanceof InetSocketAddress)) { - continue; - } - InetSocketAddress proxyAddr = (InetSocketAddress) proxy.address(); - String proxyHost = proxyAddr.getHostName(); - int proxyPort = proxyAddr.getPort(); + if (useProxy()) { httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); - Message.verbose(String.format("Proxy: %s:%d for %s", proxyHost, proxyPort, url.toExternalForm())); if (useProxyAuthentication()) { httpClient.getState().setProxyCredentials( new AuthScope(proxyHost, proxyPort, AuthScope.ANY_REALM), createCredentials(proxyUserName, proxyPasswd)); } - break; } - } else { - Message.verbose(String.format("Proxy: Noproxy for %s", url.toExternalForm())); - } - } catch (URISyntaxException ex) { - Logger.getLogger(HttpClientHandler.class.getName()).log(Level.SEVERE, null, ex); - } // user-agent httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, @@ -292,10 +301,15 @@ // authentication httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new IvyCredentialsProvider()); + } return httpClient; } + private boolean useProxy() { + return proxyHost != null && proxyHost.trim().length() > 0; + } + private boolean useAuthentication(URL url) { return CredentialsStore.INSTANCE.hasCredentials(url.getHost()); }