Index: . =================================================================== --- . (revision 696168) +++ . (working copy) @@ -65,12 +65,19 @@ private String proxyPasswd = null; + private int requestMethod; + private HttpClientHelper httpClientHelper; - + private static HttpClient httpClient; public HttpClientHandler() { + this(REQUEST_METHOD_HEAD); + } + + public HttpClientHandler(int requestMethod) { configureProxy(); + this.requestMethod = requestMethod; } private void configureProxy() { @@ -92,7 +99,7 @@ } public InputStream openStream(URL url) throws IOException { - GetMethod get = doGet(url); + GetMethod get = doGet(url, 0); if (!checkStatusCode(url, get)) { throw new IOException( "The HTTP response code for " + url + " did not indicate a success." @@ -102,7 +109,7 @@ } public void download(URL src, File dest, CopyProgressListener l) throws IOException { - GetMethod get = doGet(src); + GetMethod get = doGet(src, 0); // We can only figure the content we got is want we want if the status is success. if (!checkStatusCode(src, get)) { throw new IOException( @@ -141,11 +148,16 @@ } public URLInfo getURLInfo(URL url, int timeout) { - HeadMethod head = null; + HttpMethodBase method = null; + try { - head = doHead(url, timeout); - if (checkStatusCode(url, head)) { - return new URLInfo(true, getResponseContentLength(head), getLastModified(head)); + if (this.requestMethod == REQUEST_METHOD_HEAD) { + method = doHead(url, timeout); + } else { + method = doGet(url, timeout); + } + if (checkStatusCode(url, method)) { + return new URLInfo(true, getResponseContentLength(method), getLastModified(method)); } } catch (HttpException e) { Message.error("HttpClientHandler: " + e.getMessage() + ":" + e.getReasonCode() + "=" @@ -153,7 +165,7 @@ } catch (UnknownHostException e) { Message.warn("Host " + e.getMessage() + " not found. url=" + url); Message.info("You probably access the destination server through " - + "a proxy server that is not well configured."); + + "a proxy server that is not well configured."); } catch (IOException e) { Message.error("HttpClientHandler: " + e.getMessage() + " url=" + url); } catch (IllegalArgumentException e) { @@ -160,8 +172,8 @@ // thrown by HttpClient to indicate the URL is not valid, this happens for instance // when trying to download a dynamic version (cfr IVY-390) } finally { - if (head != null) { - head.releaseConnection(); + if (method != null) { + method.releaseConnection(); } } return UNAVAILABLE; @@ -166,7 +178,7 @@ } return UNAVAILABLE; } - + private boolean checkStatusCode(URL url, HttpMethodBase method) throws IOException { int status = method.getStatusCode(); if (status == HttpStatus.SC_OK) { @@ -180,7 +192,7 @@ } else if (String.valueOf(status).startsWith("5")) { Message.error("SERVER ERROR: " + method.getStatusText() + " url=" + url); } - + return false; } @@ -199,8 +211,8 @@ } } - private long getResponseContentLength(HeadMethod head) { - return getHttpClientHelper().getResponseContentLength(head); + private long getResponseContentLength(HttpMethodBase method) { + return getHttpClientHelper().getResponseContentLength(method); } private HttpClientHelper getHttpClientHelper() { @@ -230,9 +242,11 @@ return helper.getHttpClientMajorVersion(); } - private GetMethod doGet(URL url) throws IOException { + private GetMethod doGet(URL url, int timeout) throws IOException { HttpClient client = getClient(url); + client.setTimeout(timeout); + GetMethod get = new GetMethod(url.toExternalForm()); get.setDoAuthentication(useAuthentication(url) || useProxyAuthentication()); client.executeMethod(get); @@ -241,6 +255,7 @@ private HeadMethod doHead(URL url, int timeout) throws IOException { HttpClient client = getClient(url); + client.setTimeout(timeout); HeadMethod head = new HeadMethod(url.toExternalForm()); @@ -251,10 +266,10 @@ private HttpClient getClient(URL url) { if (httpClient == null) { - final MultiThreadedHttpConnectionManager connManager + final MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager(); httpClient = new HttpClient(connManager); - + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { connManager.shutdown(); @@ -260,7 +275,7 @@ connManager.shutdown(); } })); - + List authPrefs = new ArrayList(2); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); @@ -266,7 +281,7 @@ authPrefs.add(AuthPolicy.BASIC); // Exclude the NTLM authentication scheme because it is not supported by this class httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); - + if (useProxy()) { httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); if (useProxyAuthentication()) { @@ -274,7 +289,7 @@ new UsernamePasswordCredentials(proxyUserName, proxyPasswd)); } } - + // user-agent httpClient.getParams().setParameter( "http.useragent", "Apache Ivy/" + Ivy.getIvyVersion()); @@ -279,7 +294,7 @@ httpClient.getParams().setParameter( "http.useragent", "Apache Ivy/" + Ivy.getIvyVersion()); } - + Credentials c = getCredentials(url); if (c != null) { Message.debug("found credentials for " + url + ": " + c); @@ -286,7 +301,7 @@ httpClient.getState().setCredentials(c.getRealm(), c.getHost(), new UsernamePasswordCredentials(c.getUserName(), c.getPasswd())); } - + return httpClient; } @@ -305,15 +320,15 @@ private boolean useProxyAuthentication() { return (proxyUserName != null && proxyUserName.trim().length() > 0); } - + private static final class GETInputStream extends InputStream { - private InputStream is; + private final InputStream is; - private GetMethod get; + private final GetMethod get; private GETInputStream(GetMethod get) throws IOException { this.get = get; - is = get.getResponseBodyAsStream(); + this.is = get.getResponseBodyAsStream(); } public int available() throws IOException { @@ -372,8 +387,8 @@ private HttpClientHelper3x() { } - public long getResponseContentLength(HeadMethod head) { - return head.getResponseContentLength(); + public long getResponseContentLength(HttpMethodBase method) { + return method.getResponseContentLength(); } /** @@ -390,8 +405,8 @@ private HttpClientHelper2x() { } - public long getResponseContentLength(HeadMethod head) { - Header header = head.getResponseHeader("Content-Length"); + public long getResponseContentLength(HttpMethodBase method) { + Header header = method.getResponseHeader("Content-Length"); if (header != null) { try { return Integer.parseInt(header.getValue()); @@ -411,8 +426,16 @@ } public interface HttpClientHelper { - long getResponseContentLength(HeadMethod head); + long getResponseContentLength(HttpMethodBase method); int getHttpClientMajorVersion(); } + + public int getRequestMethod() { + return this.requestMethod; + } + + public void setRequestMethod(int requestMethod) { + this.requestMethod = requestMethod; + } }