Author: maartenc
Date: Mon Aug 25 14:56:56 2008
New Revision: 688894

URL: http://svn.apache.org/viewvc?rev=688894&view=rev
Log:
FIX: HTTP Handlers ignore unsuccessful response codes (IVY-864) (thanks to 
James P. White)

Modified:
    ant/ivy/core/trunk/CHANGES.txt
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java

Modified: ant/ivy/core/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=688894&r1=688893&r2=688894&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Mon Aug 25 14:56:56 2008
@@ -108,6 +108,7 @@
 - IMPROVEMENT: Add a memory cache for the module descriptor that are parsed 
from the cache (IVY-883)
 - IMPROVEMENT: Improve performance (IVY-872)
 
+- FIX: HTTP Handlers ignore unsuccessful response codes (IVY-864) (thanks to 
James P. White)
 - FIX: Deliver delivers the wrong version when a dynamic revision is evicted 
before being resolved (IVY-707)
 - FIX: Inconsistency with "multi-project" tutorial (IVY-667)
 - FIX: URLRepository does not allow some valid file scheme uri's (IVY-884)

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java?rev=688894&r1=688893&r2=688894&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java 
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java 
Mon Aug 25 14:56:56 2008
@@ -61,22 +61,11 @@
             con = url.openConnection();
             con.setRequestProperty("User-Agent", "Apache Ivy/" + 
Ivy.getIvyVersion());
             if (con instanceof HttpURLConnection) {
-                ((HttpURLConnection) con).setRequestMethod("HEAD");
-                int status = ((HttpURLConnection) con).getResponseCode();
-                if (status == HttpStatus.SC_OK) {
-                    return new URLInfo(true, ((HttpURLConnection) 
con).getContentLength(), con
-                            .getLastModified());
+                HttpURLConnection httpCon = (HttpURLConnection) con;
+                httpCon.setRequestMethod("HEAD");
+                if (checkStatusCode(url, httpCon)) {
+                    return new URLInfo(true, httpCon.getContentLength(), 
con.getLastModified());
                 }
-                if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
-                    Message.warn("Your proxy requires authentication.");
-                } else if (String.valueOf(status).startsWith("4")) {
-                    Message.verbose("CLIENT ERROR: "
-                            + ((HttpURLConnection) con).getResponseMessage() + 
" url=" + url);
-                } else if (String.valueOf(status).startsWith("5")) {
-                    Message.error("SERVER ERROR: " + ((HttpURLConnection) 
con).getResponseMessage()
-                            + " url=" + url);
-                }
-                Message.debug("HTTP response status: " + status + " url=" + 
url);
             } else {
                 int contentLength = con.getContentLength();
                 if (contentLength <= 0) {
@@ -97,12 +86,38 @@
         return UNAVAILABLE;
     }
 
+    private boolean checkStatusCode(URL url, HttpURLConnection con) throws 
IOException {
+        int status = con.getResponseCode();
+        if (status == HttpStatus.SC_OK) {
+            return true;
+        }
+        Message.debug("HTTP response status: " + status + " url=" + url);
+        if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
+            Message.warn("Your proxy requires authentication.");
+        } else if (String.valueOf(status).startsWith("4")) {
+            Message.verbose("CLIENT ERROR: "
+                    + ((HttpURLConnection) con).getResponseMessage() + " url=" 
+ url);
+        } else if (String.valueOf(status).startsWith("5")) {
+            Message.error("SERVER ERROR: " + ((HttpURLConnection) 
con).getResponseMessage()
+                    + " url=" + url);
+        }
+        return false;
+    }
+
     public InputStream openStream(URL url) throws IOException {
         URLConnection conn = null;
         InputStream inStream = null;
         try {
             conn = url.openConnection();
             conn.setRequestProperty("User-Agent", "Apache Ivy/" + 
Ivy.getIvyVersion());
+            if (conn instanceof HttpURLConnection) {
+                HttpURLConnection httpCon = (HttpURLConnection) conn;
+                if (!checkStatusCode(url, httpCon)) {
+                    throw new IOException(
+                        "The HTTP response code for " + url + " did not 
indicate a success."
+                                + " See log for more detail.");
+                }
+            }
             inStream = conn.getInputStream();
             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 
@@ -126,6 +141,14 @@
         try {
             srcConn = src.openConnection();
             srcConn.setRequestProperty("User-Agent", "Apache Ivy/" + 
Ivy.getIvyVersion());
+            if (srcConn instanceof HttpURLConnection) {
+                HttpURLConnection httpCon = (HttpURLConnection) srcConn;
+                if (!checkStatusCode(src, httpCon)) {
+                    throw new IOException(
+                        "The HTTP response code for " + src + " did not 
indicate a success."
+                                + " See log for more detail.");
+                }
+            }
             int contentLength = srcConn.getContentLength();
             FileUtil.copy(srcConn.getInputStream(), dest, l);
             if (dest.length() != contentLength && contentLength != -1) {

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java?rev=688894&r1=688893&r2=688894&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java 
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java 
Mon Aug 25 14:56:56 2008
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.FileInputStream;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.text.ParseException;
@@ -94,11 +95,22 @@
 
     public InputStream openStream(URL url) throws IOException {
         GetMethod get = doGet(url);
+        if (!checkStatusCode(url, get)) {
+            throw new IOException(
+                    "The HTTP response code for " + url + " did not indicate a 
success."
+                            + " See log for more detail.");
+        }
         return new GETInputStream(get);
     }
 
     public void download(URL src, File dest, CopyProgressListener l) throws 
IOException {
         GetMethod get = doGet(src);
+        // We can only figure the content we got is want we want if the status 
is success.
+        if (!checkStatusCode(src, get)) {
+            throw new IOException(
+                    "The HTTP response code for " + src + " did not indicate a 
success."
+                            + " See log for more detail.");
+        }
         FileUtil.copy(get.getResponseBodyAsStream(), dest, l);
         dest.setLastModified(getLastModified(get));
         get.releaseConnection();
@@ -134,20 +146,9 @@
         HeadMethod head = null;
         try {
             head = doHead(url, timeout);
-            int status = head.getStatusCode();
-            head.releaseConnection();
-            if (status == HttpStatus.SC_OK) {
+            if (checkStatusCode(url, head)) {
                 return new URLInfo(true, getResponseContentLength(head), 
getLastModified(head));
             }
-            if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
-                Message.error("Your proxy requires authentication.");
-            } else if (String.valueOf(status).startsWith("4")) {
-                Message.verbose("CLIENT ERROR: " + head.getStatusText() + " 
url=" + url);
-            } else if (String.valueOf(status).startsWith("5")) {
-                Message.warn("SERVER ERROR: " + head.getStatusText() + " url=" 
+ url);
-            }
-            Message.debug("HTTP response status: " + status + "=" + 
head.getStatusText() + " url="
-                    + url);
         } catch (HttpException e) {
             Message.error("HttpClientHandler: " + e.getMessage() + ":" + 
e.getReasonCode() + "="
                     + e.getReason() + " url=" + url);
@@ -167,6 +168,23 @@
         }
         return UNAVAILABLE;
     }
+    
+    private boolean checkStatusCode(URL url, HttpMethodBase method) throws 
IOException {
+        int status = method.getStatusCode();
+        if (status == HttpStatus.SC_OK) {
+            return true;
+        }
+        Message.debug("HTTP response status: " + status + " url=" + url);
+        if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
+            Message.warn("Your proxy requires authentication.");
+        } else if (String.valueOf(status).startsWith("4")) {
+            Message.verbose("CLIENT ERROR: " + method.getStatusText() + " 
url=" + url);
+        } else if (String.valueOf(status).startsWith("5")) {
+            Message.error("SERVER ERROR: " + method.getStatusText() + " url=" 
+ url);
+        }
+        
+        return false;
+    }
 
     private long getLastModified(HttpMethodBase method) {
         Header header = method.getResponseHeader("last-modified");


Reply via email to