Permalink
Browse files

Even better handling of 40x error codes

  • Loading branch information...
1 parent 6b7a543 commit 8c66e2738c37e99e5fd0d6e371d72bf00225ad95 @timroes timroes committed Apr 4, 2012
Showing with 16 additions and 37 deletions.
  1. +0 −3 README.md
  2. +0 −27 src/de/timroes/axmlrpc/XMLRPCAuthorizationException.java
  3. +16 −7 src/de/timroes/axmlrpc/XMLRPCClient.java
View
@@ -161,9 +161,6 @@ With this flag enabled, the XML-RPC client will ignore the HTTP status
code of the response from the server. According to specification the
status code must be 200. This flag is only needed for the use with
not standard compliant servers.
-40X return codes won't be ignored, and will still lead to an exception.
-In case of 401 and 403 a XMLRPCAuthorizationException will be thrown
-(also without this flag).
#### FLAGS_FORWARD
@@ -1,27 +0,0 @@
-package de.timroes.axmlrpc;
-
-/**
- * This exception will be thrown if the server returned an UNAUTHORIZED or
- * FORBIDDEN response code.
- *
- * @author Tim Roes <mail@timroes.de>
- */
-public class XMLRPCAuthorizationException extends XMLRPCException {
-
- private final int statusCode;
-
- public XMLRPCAuthorizationException(int statusCode) {
- super();
- this.statusCode = statusCode;
- }
-
- /**
- * Get the status code, that has been returned by the server.
- *
- * @return The status code, that caused this exception.
- */
- public final int getStatusCode() {
- return statusCode;
- }
-
-}
@@ -92,9 +92,6 @@
* code of the response from the server. According to specification the
* status code must be 200. This flag is only needed for the use with
* not standard compliant servers.
- * 40X return codes won't be ignored, and will still lead to an exception.
- * In case of 401 and 403 a XMLRPCAuthorizationException will be thrown
- * (also without this flag).
*/
public static final int FLAGS_IGNORE_STATUSCODE = 0x10;
@@ -466,14 +463,26 @@ public Object call(String methodName, Object[] params) throws XMLRPCException {
statusCode = http.getResponseCode();
}
- // If status code was 401 or 403, throw an XMLRPCAuthorizationException
+ InputStream istream;
+
+ // If status code was 401 or 403 throw exception or if appropriate
+ // flag is set, ignore error code.
if(statusCode == HttpURLConnection.HTTP_FORBIDDEN
|| statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
- throw new XMLRPCAuthorizationException(statusCode);
+
+ if(isFlagSet(FLAGS_IGNORE_STATUSCODE)) {
+ // getInputStream will fail if server returned above
+ // error code, use getErrorStream instead
+ istream = http.getErrorStream();
+ } else {
+ throw new XMLRPCException("Invalid status code '"
+ + statusCode + "' returned from server.");
+ }
+
+ } else {
+ istream = http.getInputStream();
}
- InputStream istream = http.getInputStream();
-
// If status code is 301 Moved Permanently or 302 Found ...
if(statusCode == HttpURLConnection.HTTP_MOVED_PERM
|| statusCode == HttpURLConnection.HTTP_MOVED_TEMP) {

0 comments on commit 8c66e27

Please sign in to comment.