mirror of https://github.com/apache/jclouds.git
Issue 403: add httpresponse to auth exception
This commit is contained in:
parent
5b40df46c9
commit
ef0ac61104
|
@ -64,44 +64,51 @@ public class ParseAtmosStorageErrorFromXmlContent implements HttpErrorHandler {
|
|||
}
|
||||
|
||||
public static final Pattern DIRECTORY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/$");
|
||||
public static final Pattern DIRECTORY_KEY_PATH = Pattern
|
||||
.compile("^/rest/namespace/?([^/]+)/(.*)");
|
||||
public static final Pattern DIRECTORY_KEY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/(.*)");
|
||||
|
||||
public void handleError(HttpCommand command, HttpResponse response) {
|
||||
Exception exception = new HttpResponseException(command, response);
|
||||
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) {
|
||||
File file = new File(command.getRequest().getEndpoint().getPath());
|
||||
exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file
|
||||
.getName());
|
||||
exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file.getName());
|
||||
} else {
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(),
|
||||
error != null ? error.getMessage() : response.getStatusLine());
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
String message = error != null ? error.getMessage() : String.format(
|
||||
"%s -> %s", command.getRequest().getRequestLine(), response
|
||||
.getStatusLine());
|
||||
String path = command.getRequest().getEndpoint().getPath();
|
||||
Matcher matcher = DIRECTORY_PATH.matcher(path);
|
||||
case 401:
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
String message = error != null ? error.getMessage() : String.format("%s -> %s", command.getRequest()
|
||||
.getRequestLine(), response.getStatusLine());
|
||||
String path = command.getRequest().getEndpoint().getPath();
|
||||
Matcher matcher = DIRECTORY_PATH.matcher(path);
|
||||
if (matcher.find()) {
|
||||
exception = new ContainerNotFoundException(matcher.group(1), message);
|
||||
} else {
|
||||
matcher = DIRECTORY_KEY_PATH.matcher(path);
|
||||
if (matcher.find()) {
|
||||
exception = new ContainerNotFoundException(matcher.group(1), message);
|
||||
} else {
|
||||
matcher = DIRECTORY_KEY_PATH.matcher(path);
|
||||
if (matcher.find()) {
|
||||
exception = new KeyNotFoundException(matcher.group(1), matcher.group(2),
|
||||
message);
|
||||
}
|
||||
exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
exception = error != null ? new AtmosStorageResponseException(command, response,
|
||||
error) : new HttpResponseException(command, response);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -66,7 +66,7 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
|
|||
|
||||
public void handleError(HttpCommand command, HttpResponse response) {
|
||||
HttpRequest request = command.getRequest();
|
||||
Exception exception = null;
|
||||
Exception exception = new HttpResponseException(command, response);
|
||||
try {
|
||||
AWSError error = null;
|
||||
String message = null;
|
||||
|
@ -81,14 +81,13 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
|
|||
} else {
|
||||
try {
|
||||
message = Utils.toStringAndClose(response.getPayload().getInput());
|
||||
exception = new HttpResponseException(command, response, message);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
message = message != null ? message : String.format("%s -> %s", request.getRequestLine(),
|
||||
response.getStatusLine());
|
||||
if (exception == null)
|
||||
exception = new HttpResponseException(command, response, message);
|
||||
switch (response.getStatusCode()) {
|
||||
case 400:
|
||||
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))
|
||||
exception = new IllegalStateException(message, exception);
|
||||
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
|
||||
exception = new IllegalArgumentException(message, exception);
|
||||
break;
|
||||
case 401:
|
||||
case 403:
|
||||
exception = new AuthorizationException(command.getRequest(), message);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
|
|
|
@ -80,12 +80,14 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
|
|||
} catch (RuntimeException e) {
|
||||
try {
|
||||
message = Utils.toStringAndClose(response.getPayload().getInput());
|
||||
exception = new HttpResponseException(command, response, message);
|
||||
} catch (IOException e1) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
message = Utils.toStringAndClose(response.getPayload().getInput());
|
||||
exception = new HttpResponseException(command, response, message);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +96,7 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
|
|||
response.getStatusLine());
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(), message);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.jclouds.rest;
|
||||
|
||||
import org.jclouds.http.HttpRequest;
|
||||
|
||||
/**
|
||||
* Thrown when there is an authorization error.
|
||||
|
@ -39,14 +38,6 @@ public class AuthorizationException extends RuntimeException {
|
|||
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) {
|
||||
super(arg0);
|
||||
}
|
||||
|
|
|
@ -53,23 +53,17 @@ public class GoGridErrorHandler implements HttpErrorHandler {
|
|||
try {
|
||||
Exception exception = new HttpResponseException(command, response);
|
||||
Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response);
|
||||
if (errors != null)
|
||||
exception = new GoGridResponseException(command, response, errors);
|
||||
switch (response.getStatusCode()) {
|
||||
case 400:
|
||||
if (Iterables.get(errors, 0).getMessage()
|
||||
.indexOf("No object found") != -1) {
|
||||
exception = new ResourceNotFoundException(Iterables.get(errors,
|
||||
0).getMessage(), exception);
|
||||
if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) {
|
||||
exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception);
|
||||
break;
|
||||
}
|
||||
case 403:
|
||||
|
||||
exception = new AuthorizationException(command.getRequest(),
|
||||
errors != null ? errors.toString() : response.getStatusLine());
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
default:
|
||||
exception = errors != null ? new GoGridResponseException(command,
|
||||
response, errors) : new HttpResponseException(command,
|
||||
response);
|
||||
}
|
||||
command.setException(exception);
|
||||
} finally {
|
||||
|
|
|
@ -57,7 +57,7 @@ public class ParseCloudFilesErrorFromHttpResponse implements HttpErrorHandler {
|
|||
exception = content != null ? new HttpResponseException(command, response, content) : exception;
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(), content, exception);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
|
|
|
@ -52,9 +52,10 @@ public class ParseCloudServersErrorFromHttpResponse implements HttpErrorHandler
|
|||
Exception exception = new HttpResponseException(command, response);
|
||||
try {
|
||||
String content = parseErrorFromContentOrNull(command, response);
|
||||
exception = content != null ? new HttpResponseException(command, response, content) : exception;
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(), content);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
|
|
|
@ -64,8 +64,8 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
|
|||
RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
|
||||
String errorClass = firstResponse.getErrorInfo().getErrorClass();
|
||||
if (errorClass.equals("PermissionException"))
|
||||
throw new AuthorizationException(responseException.getCommand().getRequest(),
|
||||
firstResponse.getErrorInfo().getErrorMessage());
|
||||
throw new AuthorizationException(
|
||||
firstResponse.getErrorInfo().getErrorMessage(), responseException);
|
||||
throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,9 +65,10 @@ public class ParseSlicehostErrorFromHttpResponse implements HttpErrorHandler {
|
|||
Exception exception = new HttpResponseException(command, response);
|
||||
try {
|
||||
String content = response.getStatusCode() != 401 ? parseErrorFromContentOrNull(command, response) : null;
|
||||
exception = content != null ? new HttpResponseException(command, response, content) : exception;
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(), content);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 403:
|
||||
case 404:
|
||||
|
|
|
@ -79,6 +79,7 @@ public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
|
|||
} else {
|
||||
try {
|
||||
message = Utils.toStringAndClose(response.getPayload().getInput());
|
||||
exception = message != null ? new HttpResponseException(command, response, message) : exception;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +97,7 @@ public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
|
|||
break;
|
||||
case 401:
|
||||
case 403:
|
||||
exception = new AuthorizationException(command.getRequest(), message);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||
|
|
|
@ -54,6 +54,8 @@ public class ParseTerremarkVCloudErrorFromHttpResponse implements HttpErrorHandl
|
|||
|
||||
try {
|
||||
String content = parseErrorFromContentOrNull(command, response);
|
||||
if (content != null)
|
||||
exception = new HttpResponseException(command, response, content);
|
||||
if (response.getMessage() != null
|
||||
&& ((response.getMessage().indexOf("because there is a pending task running") != -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);
|
||||
break;
|
||||
case 401:
|
||||
exception = new AuthorizationException(command.getRequest(), content);
|
||||
exception = new AuthorizationException(exception.getMessage(), exception);
|
||||
break;
|
||||
case 403: // TODO temporary as terremark mistakenly uses this for vApp
|
||||
// not found.
|
||||
|
|
Loading…
Reference in New Issue