mirror of https://github.com/apache/jclouds.git
fixed azure blob error parsing when no content is returned
This commit is contained in:
parent
2f00ab22e4
commit
a0fbeb96e7
|
@ -68,12 +68,20 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
|
||||||
try {
|
try {
|
||||||
if (response.getPayload() != null) {
|
if (response.getPayload() != null) {
|
||||||
String contentType = response.getPayload().getContentMetadata().getContentType();
|
String contentType = response.getPayload().getContentMetadata().getContentType();
|
||||||
if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) {
|
if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)
|
||||||
AzureStorageError error = utils.parseAzureStorageErrorFromContent(command, response, response
|
&& !new Long(0).equals(response.getPayload().getContentMetadata().getContentLength())) {
|
||||||
|
try {
|
||||||
|
AzureStorageError error = utils.parseAzureStorageErrorFromContent(command, response, response
|
||||||
.getPayload().getInput());
|
.getPayload().getInput());
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
message = error.getMessage();
|
message = error.getMessage();
|
||||||
exception = new AzureStorageResponseException(command, response, error);
|
exception = new AzureStorageResponseException(command, response, error);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
try {
|
||||||
|
message = Utils.toStringAndClose(response.getPayload().getInput());
|
||||||
|
} catch (IOException e1) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
@ -83,28 +91,28 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
message = message != null ? message : String.format("%s -> %s", command.getRequest().getRequestLine(),
|
message = message != null ? message : String.format("%s -> %s", command.getRequest().getRequestLine(),
|
||||||
response.getStatusLine());
|
response.getStatusLine());
|
||||||
switch (response.getStatusCode()) {
|
switch (response.getStatusCode()) {
|
||||||
case 401:
|
case 401:
|
||||||
exception = new AuthorizationException(command.getRequest(), message);
|
exception = new AuthorizationException(command.getRequest(), message);
|
||||||
break;
|
break;
|
||||||
case 404:
|
case 404:
|
||||||
if (!command.getRequest().getMethod().equals("DELETE")) {
|
if (!command.getRequest().getMethod().equals("DELETE")) {
|
||||||
String path = command.getRequest().getEndpoint().getPath();
|
String path = command.getRequest().getEndpoint().getPath();
|
||||||
Matcher matcher = CONTAINER_PATH.matcher(path);
|
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()) {
|
if (matcher.find()) {
|
||||||
exception = new ContainerNotFoundException(matcher.group(1), message);
|
exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
|
||||||
} else {
|
|
||||||
matcher = CONTAINER_KEY_PATH.matcher(path);
|
|
||||||
if (matcher.find()) {
|
|
||||||
exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
case 411:
|
break;
|
||||||
exception = new IllegalArgumentException(message);
|
case 411:
|
||||||
break;
|
exception = new IllegalArgumentException(message);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
releasePayload(response);
|
releasePayload(response);
|
||||||
|
|
|
@ -50,22 +50,33 @@ public class ParseAzureErrorFromXmlContentTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test411WithTextHtmlIllegalArgumentException() {
|
public void test411WithTextHtmlIllegalArgumentException() {
|
||||||
assertCodeMakes("PUT", URI
|
assertCodeMakes("PUT",
|
||||||
.create("https://jclouds.blob.core.windows.net/adriancole-azureblob-413790770?restype=container"), 411,
|
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",
|
"Length Required", "text/html; charset=us-ascii", "<HTML><HEAD><TITLE>Length Required</TITLE>\r\n",
|
||||||
IllegalArgumentException.class);
|
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
|
@Test
|
||||||
public void test412WithTextHtmlHttpResponseException() {
|
public void test412WithTextHtmlHttpResponseException() {
|
||||||
assertCodeMakes("GET", URI
|
assertCodeMakes(
|
||||||
.create("https://jclouds.blob.core.windows.net/adriancole-blobstore2?restype=container&comp=list&prefix=apps/apps/apps/&include=metadata"), 412,
|
"GET",
|
||||||
"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>",
|
URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore2?restype=container&comp=list&prefix=apps/apps/apps/&include=metadata"),
|
||||||
AzureStorageResponseException.class);
|
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,
|
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() {
|
ParseAzureStorageErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
|
||||||
|
|
||||||
|
@ -79,7 +90,7 @@ public class ParseAzureErrorFromXmlContentTest {
|
||||||
HttpCommand command = createMock(HttpCommand.class);
|
HttpCommand command = createMock(HttpCommand.class);
|
||||||
HttpRequest request = new HttpRequest(method, uri);
|
HttpRequest request = new HttpRequest(method, uri);
|
||||||
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Utils
|
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Utils
|
||||||
.toInputStream(content)));
|
.toInputStream(content)));
|
||||||
response.getPayload().getContentMetadata().setContentType(contentType);
|
response.getPayload().getContentMetadata().setContentType(contentType);
|
||||||
|
|
||||||
expect(command.getRequest()).andReturn(request).atLeastOnce();
|
expect(command.getRequest()).andReturn(request).atLeastOnce();
|
||||||
|
|
Loading…
Reference in New Issue