fixed azure blob error parsing when no content is returned

This commit is contained in:
Adrian Cole 2010-11-08 09:02:48 +01:00
parent 2f00ab22e4
commit a0fbeb96e7
2 changed files with 53 additions and 34 deletions

View File

@ -68,12 +68,20 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
try {
if (response.getPayload() != null) {
String contentType = response.getPayload().getContentMetadata().getContentType();
if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) {
AzureStorageError error = utils.parseAzureStorageErrorFromContent(command, response, response
if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)
&& !new Long(0).equals(response.getPayload().getContentMetadata().getContentLength())) {
try {
AzureStorageError error = utils.parseAzureStorageErrorFromContent(command, response, response
.getPayload().getInput());
if (error != null) {
message = error.getMessage();
exception = new AzureStorageResponseException(command, response, error);
if (error != null) {
message = error.getMessage();
exception = new AzureStorageResponseException(command, response, error);
}
} catch (RuntimeException e) {
try {
message = Utils.toStringAndClose(response.getPayload().getInput());
} catch (IOException e1) {
}
}
} else {
try {
@ -83,28 +91,28 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
}
}
message = message != null ? message : String.format("%s -> %s", command.getRequest().getRequestLine(),
response.getStatusLine());
response.getStatusLine());
switch (response.getStatusCode()) {
case 401:
exception = new AuthorizationException(command.getRequest(), message);
break;
case 404:
if (!command.getRequest().getMethod().equals("DELETE")) {
String path = command.getRequest().getEndpoint().getPath();
Matcher matcher = CONTAINER_PATH.matcher(path);
case 401:
exception = new AuthorizationException(command.getRequest(), message);
break;
case 404:
if (!command.getRequest().getMethod().equals("DELETE")) {
String path = command.getRequest().getEndpoint().getPath();
Matcher matcher = CONTAINER_PATH.matcher(path);
if (matcher.find()) {
exception = new ContainerNotFoundException(matcher.group(1), message);
} else {
matcher = CONTAINER_KEY_PATH.matcher(path);
if (matcher.find()) {
exception = new ContainerNotFoundException(matcher.group(1), message);
} else {
matcher = CONTAINER_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;
case 411:
exception = new IllegalArgumentException(message);
break;
}
break;
case 411:
exception = new IllegalArgumentException(message);
break;
}
} finally {
releasePayload(response);

View File

@ -50,22 +50,33 @@ public class ParseAzureErrorFromXmlContentTest {
@Test
public void test411WithTextHtmlIllegalArgumentException() {
assertCodeMakes("PUT", URI
.create("https://jclouds.blob.core.windows.net/adriancole-azureblob-413790770?restype=container"), 411,
"Length Required", "text/html; charset=us-ascii", "<HTML><HEAD><TITLE>Length Required</TITLE>\r\n",
IllegalArgumentException.class);
assertCodeMakes("PUT",
URI.create("https://jclouds.blob.core.windows.net/adriancole-azureblob-413790770?restype=container"), 411,
"Length Required", "text/html; charset=us-ascii", "<HTML><HEAD><TITLE>Length Required</TITLE>\r\n",
IllegalArgumentException.class);
}
@Test
public void test304WithNoContentIllegalArgumentException() {
assertCodeMakes("GET", URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore0/apples"), 411,
"HTTP/1.1 304 The condition specified using HTTP conditional header(s) is not met.", "application/unknown",
"", IllegalArgumentException.class);
}
@Test
public void test412WithTextHtmlHttpResponseException() {
assertCodeMakes("GET", URI
.create("https://jclouds.blob.core.windows.net/adriancole-blobstore2?restype=container&comp=list&prefix=apps/apps/apps/&include=metadata"), 412,
"HTTP/1.1 412 The condition specified using HTTP conditional header(s) is not met.", "application/xml", "<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ConditionNotMet</Code><Message>The condition specified using HTTP conditional header(s) is not met.\nRequestId:921efcad-84bc-4e0a-863d-24810d1096e1\nTime:2010-11-04T15:03:07.8694513Z</Message></Error>",
AzureStorageResponseException.class);
assertCodeMakes(
"GET",
URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore2?restype=container&comp=list&prefix=apps/apps/apps/&include=metadata"),
412,
"HTTP/1.1 412 The condition specified using HTTP conditional header(s) is not met.",
"application/xml",
"<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ConditionNotMet</Code><Message>The condition specified using HTTP conditional header(s) is not met.\nRequestId:921efcad-84bc-4e0a-863d-24810d1096e1\nTime:2010-11-04T15:03:07.8694513Z</Message></Error>",
AzureStorageResponseException.class);
}
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
String content, Class<? extends Exception> expected) {
String content, Class<? extends Exception> expected) {
ParseAzureStorageErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
@ -79,7 +90,7 @@ public class ParseAzureErrorFromXmlContentTest {
HttpCommand command = createMock(HttpCommand.class);
HttpRequest request = new HttpRequest(method, uri);
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Utils
.toInputStream(content)));
.toInputStream(content)));
response.getPayload().getContentMetadata().setContentType(contentType);
expect(command.getRequest()).andReturn(request).atLeastOnce();