Issue 403: add httpresponse to auth exception

This commit is contained in:
Adrian Cole 2010-11-11 10:57:15 +01:00
parent 5b40df46c9
commit ef0ac61104
11 changed files with 58 additions and 73 deletions

View File

@ -64,44 +64,51 @@ public class ParseAtmosStorageErrorFromXmlContent implements HttpErrorHandler {
} }
public static final Pattern DIRECTORY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/$"); public static final Pattern DIRECTORY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/$");
public static final Pattern DIRECTORY_KEY_PATH = Pattern public static final Pattern DIRECTORY_KEY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/(.*)");
.compile("^/rest/namespace/?([^/]+)/(.*)");
public void handleError(HttpCommand command, HttpResponse response) { public void handleError(HttpCommand command, HttpResponse response) {
Exception exception = new HttpResponseException(command, response); Exception exception = new HttpResponseException(command, response);
try { try {
AtmosStorageError error = parseErrorFromContentOrNull(command, response); AtmosStorageError error = null;
if (response.getPayload() != null) {
try {
String content = Utils.toStringAndClose(response.getPayload().getInput());
if (content != null && content.indexOf('<') >= 0) {
error = utils.parseAtmosStorageErrorFromContent(command, response, Utils.toInputStream(content));
} else {
exception = content != null ? new HttpResponseException(command, response, content) : exception;
}
} catch (IOException e) {
logger.warn(e, "exception reading error from response", response);
}
}
if (error != null && error.getCode() == 1016) { if (error != null && error.getCode() == 1016) {
File file = new File(command.getRequest().getEndpoint().getPath()); File file = new File(command.getRequest().getEndpoint().getPath());
exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file.getName());
.getName());
} else { } else {
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), exception = new AuthorizationException(exception.getMessage(), exception);
error != null ? error.getMessage() : response.getStatusLine()); break;
break; case 404:
case 404: if (!command.getRequest().getMethod().equals("DELETE")) {
if (!command.getRequest().getMethod().equals("DELETE")) { String message = error != null ? error.getMessage() : String.format("%s -> %s", command.getRequest()
String message = error != null ? error.getMessage() : String.format( .getRequestLine(), response.getStatusLine());
"%s -> %s", command.getRequest().getRequestLine(), response String path = command.getRequest().getEndpoint().getPath();
.getStatusLine()); Matcher matcher = DIRECTORY_PATH.matcher(path);
String path = command.getRequest().getEndpoint().getPath(); if (matcher.find()) {
Matcher matcher = DIRECTORY_PATH.matcher(path); exception = new ContainerNotFoundException(matcher.group(1), message);
} else {
matcher = DIRECTORY_KEY_PATH.matcher(path);
if (matcher.find()) { if (matcher.find()) {
exception = new ContainerNotFoundException(matcher.group(1), message); exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
} else {
matcher = DIRECTORY_KEY_PATH.matcher(path);
if (matcher.find()) {
exception = new KeyNotFoundException(matcher.group(1), matcher.group(2),
message);
}
} }
} }
break; }
default: break;
exception = error != null ? new AtmosStorageResponseException(command, response, default:
error) : new HttpResponseException(command, response); exception = error != null ? new AtmosStorageResponseException(command, response, error)
: new HttpResponseException(command, response);
} }
} }
@ -111,17 +118,4 @@ public class ParseAtmosStorageErrorFromXmlContent implements HttpErrorHandler {
} }
} }
AtmosStorageError parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) {
if (response.getPayload() != null) {
try {
String content = Utils.toStringAndClose(response.getPayload().getInput());
if (content != null && content.indexOf('<') >= 0)
return utils.parseAtmosStorageErrorFromContent(command, response, Utils
.toInputStream(content));
} catch (IOException e) {
logger.warn(e, "exception reading error from response", response);
}
}
return null;
}
} }

View File

@ -66,7 +66,7 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
public void handleError(HttpCommand command, HttpResponse response) { public void handleError(HttpCommand command, HttpResponse response) {
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();
Exception exception = null; Exception exception = new HttpResponseException(command, response);
try { try {
AWSError error = null; AWSError error = null;
String message = null; String message = null;
@ -81,14 +81,13 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
} else { } else {
try { try {
message = Utils.toStringAndClose(response.getPayload().getInput()); message = Utils.toStringAndClose(response.getPayload().getInput());
exception = new HttpResponseException(command, response, message);
} catch (IOException e) { } catch (IOException e) {
} }
} }
} }
message = message != null ? message : String.format("%s -> %s", request.getRequestLine(), message = message != null ? message : String.format("%s -> %s", request.getRequestLine(),
response.getStatusLine()); response.getStatusLine());
if (exception == null)
exception = new HttpResponseException(command, response, message);
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 400: case 400:
if (error != null && error.getCode() != null if (error != null && error.getCode() != null
@ -98,13 +97,13 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
.getCode().endsWith(".Duplicate"))) || (message != null && message.indexOf("already exists") != -1)) .getCode().endsWith(".Duplicate"))) || (message != null && message.indexOf("already exists") != -1))
exception = new IllegalStateException(message, exception); exception = new IllegalStateException(message, exception);
else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure")) else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
exception = new AuthorizationException(command.getRequest(), message); exception = new AuthorizationException(exception.getMessage(), exception);
else if (message != null && message.indexOf("Failed to bind the following fields") != -1)// Nova else if (message != null && message.indexOf("Failed to bind the following fields") != -1)// Nova
exception = new IllegalArgumentException(message, exception); exception = new IllegalArgumentException(message, exception);
break; break;
case 401: case 401:
case 403: case 403:
exception = new AuthorizationException(command.getRequest(), message); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 404: case 404:
if (!command.getRequest().getMethod().equals("DELETE")) { if (!command.getRequest().getMethod().equals("DELETE")) {

View File

@ -80,12 +80,14 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
} catch (RuntimeException e) { } catch (RuntimeException e) {
try { try {
message = Utils.toStringAndClose(response.getPayload().getInput()); message = Utils.toStringAndClose(response.getPayload().getInput());
exception = new HttpResponseException(command, response, message);
} catch (IOException e1) { } catch (IOException e1) {
} }
} }
} else { } else {
try { try {
message = Utils.toStringAndClose(response.getPayload().getInput()); message = Utils.toStringAndClose(response.getPayload().getInput());
exception = new HttpResponseException(command, response, message);
} catch (IOException e) { } catch (IOException e) {
} }
} }
@ -94,7 +96,7 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
response.getStatusLine()); response.getStatusLine());
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), message); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 404: case 404:
if (!command.getRequest().getMethod().equals("DELETE")) { if (!command.getRequest().getMethod().equals("DELETE")) {

View File

@ -19,7 +19,6 @@
package org.jclouds.rest; package org.jclouds.rest;
import org.jclouds.http.HttpRequest;
/** /**
* Thrown when there is an authorization error. * Thrown when there is an authorization error.
@ -39,14 +38,6 @@ public class AuthorizationException extends RuntimeException {
super(arg0, arg1); super(arg0, arg1);
} }
public AuthorizationException(HttpRequest resource, String error) {
super(String.format("%s -> %s", resource.getRequestLine(), error));
}
public AuthorizationException(HttpRequest resource, String error, Throwable arg1) {
super(String.format("%s -> %s", resource.getRequestLine(), error), arg1);
}
public AuthorizationException(Throwable arg0) { public AuthorizationException(Throwable arg0) {
super(arg0); super(arg0);
} }

View File

@ -53,23 +53,17 @@ public class GoGridErrorHandler implements HttpErrorHandler {
try { try {
Exception exception = new HttpResponseException(command, response); Exception exception = new HttpResponseException(command, response);
Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response); Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response);
if (errors != null)
exception = new GoGridResponseException(command, response, errors);
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 400: case 400:
if (Iterables.get(errors, 0).getMessage() if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) {
.indexOf("No object found") != -1) { exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception);
exception = new ResourceNotFoundException(Iterables.get(errors,
0).getMessage(), exception);
break; break;
} }
case 403: case 403:
exception = new AuthorizationException(exception.getMessage(), exception);
exception = new AuthorizationException(command.getRequest(),
errors != null ? errors.toString() : response.getStatusLine());
break; break;
default:
exception = errors != null ? new GoGridResponseException(command,
response, errors) : new HttpResponseException(command,
response);
} }
command.setException(exception); command.setException(exception);
} finally { } finally {

View File

@ -57,7 +57,7 @@ public class ParseCloudFilesErrorFromHttpResponse implements HttpErrorHandler {
exception = content != null ? new HttpResponseException(command, response, content) : exception; exception = content != null ? new HttpResponseException(command, response, content) : exception;
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), content, exception); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 404: case 404:
if (!command.getRequest().getMethod().equals("DELETE")) { if (!command.getRequest().getMethod().equals("DELETE")) {

View File

@ -52,9 +52,10 @@ public class ParseCloudServersErrorFromHttpResponse implements HttpErrorHandler
Exception exception = new HttpResponseException(command, response); Exception exception = new HttpResponseException(command, response);
try { try {
String content = parseErrorFromContentOrNull(command, response); String content = parseErrorFromContentOrNull(command, response);
exception = content != null ? new HttpResponseException(command, response, content) : exception;
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), content); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 404: case 404:
if (!command.getRequest().getMethod().equals("DELETE")) { if (!command.getRequest().getMethod().equals("DELETE")) {

View File

@ -64,8 +64,8 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0); RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
String errorClass = firstResponse.getErrorInfo().getErrorClass(); String errorClass = firstResponse.getErrorInfo().getErrorClass();
if (errorClass.equals("PermissionException")) if (errorClass.equals("PermissionException"))
throw new AuthorizationException(responseException.getCommand().getRequest(), throw new AuthorizationException(
firstResponse.getErrorInfo().getErrorMessage()); firstResponse.getErrorInfo().getErrorMessage(), responseException);
throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e); throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e);
} }
} }

View File

@ -65,9 +65,10 @@ public class ParseSlicehostErrorFromHttpResponse implements HttpErrorHandler {
Exception exception = new HttpResponseException(command, response); Exception exception = new HttpResponseException(command, response);
try { try {
String content = response.getStatusCode() != 401 ? parseErrorFromContentOrNull(command, response) : null; String content = response.getStatusCode() != 401 ? parseErrorFromContentOrNull(command, response) : null;
exception = content != null ? new HttpResponseException(command, response, content) : exception;
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), content); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 403: case 403:
case 404: case 404:

View File

@ -79,6 +79,7 @@ public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
} else { } else {
try { try {
message = Utils.toStringAndClose(response.getPayload().getInput()); message = Utils.toStringAndClose(response.getPayload().getInput());
exception = message != null ? new HttpResponseException(command, response, message) : exception;
} catch (IOException e) { } catch (IOException e) {
} }
} }
@ -96,7 +97,7 @@ public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
break; break;
case 401: case 401:
case 403: case 403:
exception = new AuthorizationException(command.getRequest(), message); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 404: case 404:
if (!command.getRequest().getMethod().equals("DELETE")) { if (!command.getRequest().getMethod().equals("DELETE")) {

View File

@ -54,6 +54,8 @@ public class ParseTerremarkVCloudErrorFromHttpResponse implements HttpErrorHandl
try { try {
String content = parseErrorFromContentOrNull(command, response); String content = parseErrorFromContentOrNull(command, response);
if (content != null)
exception = new HttpResponseException(command, response, content);
if (response.getMessage() != null if (response.getMessage() != null
&& ((response.getMessage().indexOf("because there is a pending task running") != -1) && ((response.getMessage().indexOf("because there is a pending task running") != -1)
|| (response.getMessage().indexOf("because it is already powered off") != -1) || (response.getMessage().indexOf("because it is already powered off") != -1)
@ -65,7 +67,7 @@ public class ParseTerremarkVCloudErrorFromHttpResponse implements HttpErrorHandl
exception = new IllegalArgumentException(response.getMessage(), exception); exception = new IllegalArgumentException(response.getMessage(), exception);
break; break;
case 401: case 401:
exception = new AuthorizationException(command.getRequest(), content); exception = new AuthorizationException(exception.getMessage(), exception);
break; break;
case 403: // TODO temporary as terremark mistakenly uses this for vApp case 403: // TODO temporary as terremark mistakenly uses this for vApp
// not found. // not found.