Hi all,
Wolfgang Baer wrote:
> Hi,
>
> David Daney wrote:
>>
>>Perhaps make isError() a member of Response
>
> Yes, I also thought of this. Its in HTTPURLConnection because there is
> already a method isRedirect(Response r). Both should be moved to Response.
Committed as attached. Changes over the previous patch are the move of the
isRedirect() and isError() methods to Response.java.
2006-02-09 Wolfgang Baer <[EMAIL PROTECTED]>
* gnu/java/net/protocol/http/HTTPURLConnection.java:
(isRedirect): Removed, moved to Response.java.
(connect): If error condition redirect responseSink to errorSink.
(getInputStream): If error condition throw IOException, for the error
codes 404 and 410 throw a FileNotFoundException.
* gnu/java/net/protocol/http/Response.java (isError): New method.
(isRedirect): New method, moved from HTTPURLConnection.java.
Wolfgang
Index: gnu/java/net/protocol/http/HTTPURLConnection.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java,v
retrieving revision 1.18
diff -u -r1.18 HTTPURLConnection.java
--- gnu/java/net/protocol/http/HTTPURLConnection.java 9 Feb 2006 09:37:08 -0000 1.18
+++ gnu/java/net/protocol/http/HTTPURLConnection.java 9 Feb 2006 20:39:42 -0000
@@ -254,7 +254,7 @@
}
}
- if (isRedirect(response) && getInstanceFollowRedirects())
+ if (response.isRedirect() && getInstanceFollowRedirects())
{
// Read the response body, if there is one. If the
// redirect points us back at the same server, we will use
@@ -349,22 +349,13 @@
{
responseSink = response.getBody();
- if (response.getCode() == 404)
- {
- errorSink = responseSink;
- throw new FileNotFoundException(url.toString());
- }
+ if (response.isError())
+ errorSink = responseSink;
}
}
while (retry);
connected = true;
- }
-
- private static boolean isRedirect(Response response)
- {
- int sc = response.getCode();
- return (sc != 304 && (sc / 100) == 3);
- }
+ }
/**
* Returns a connection, from the pool if necessary.
@@ -525,6 +516,17 @@
{
throw new ProtocolException("doInput is false");
}
+
+ if (response.isError())
+ {
+ int code = response.getCode();
+ if (code == 404 || code == 410)
+ throw new FileNotFoundException(url.toString());
+
+ throw new IOException("Server returned HTTP response code " + code
+ + " for URL " + url.toString());
+ }
+
return responseSink;
}
Index: gnu/java/net/protocol/http/Response.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/net/protocol/http/Response.java,v
retrieving revision 1.3
diff -u -r1.3 Response.java
--- gnu/java/net/protocol/http/Response.java 12 Oct 2005 19:48:25 -0000 1.3
+++ gnu/java/net/protocol/http/Response.java 9 Feb 2006 20:39:42 -0000
@@ -1,5 +1,5 @@
/* Response.java --
- Copyright (C) 2004 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -188,6 +188,28 @@
{
return headers.getDateValue(name);
}
+
+ /**
+ * Tests whether this response indicates a redirection.
+ *
+ * @return <code>true</code> if, <code>false</code> otherwise.
+ */
+ public boolean isRedirect()
+ {
+ return (code != 304 && getCodeClass() == 3);
+ }
+
+ /**
+ * Tests whether this response indicates an error.
+ * Errors are the response codes <code>4xx</code> - Client error and
+ * <code>5xx</code> - Server error.
+ *
+ * @return <code>true</code> if, <code>false</code> otherwise.
+ */
+ public boolean isError()
+ {
+ return (getCodeClass() == 4 || getCodeClass() == 5);
+ }
/**
* Returns an InputStream that returns the body of the response.