JCLOUDS-1552: Do not attempt to parse empty payload (#82)

This commit is contained in:
Tamas Cservenak 2020-09-21 15:46:25 +02:00 committed by Ignasi Barrera
parent 76f9a53247
commit 619466c66f
3 changed files with 34 additions and 2 deletions

View File

@ -19,6 +19,7 @@ package org.jclouds.aws.util;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG; import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -82,7 +83,8 @@ public class AWSUtils {
} }
public AWSError parseAWSErrorFromContent(HttpRequest request, HttpResponse response) { public AWSError parseAWSErrorFromContent(HttpRequest request, HttpResponse response) {
if (response.getPayload() == null) byte[] actualPayload = response.getPayload() != null ? closeClientButKeepContentStream(response) : null;
if (actualPayload == null || actualPayload.length == 0)
return null; return null;
if ("text/plain".equals(response.getPayload().getContentMetadata().getContentType())) if ("text/plain".equals(response.getPayload().getContentMetadata().getContentType()))
return null; return null;

View File

@ -17,6 +17,7 @@
package org.jclouds.aws.util; package org.jclouds.aws.util;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN; import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST; import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
@ -26,12 +27,17 @@ import static org.testng.Assert.assertNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.inject.Provider;
import org.jclouds.aws.domain.AWSError; import org.jclouds.aws.domain.AWSError;
import org.jclouds.aws.filters.FormSignerV2Test; import org.jclouds.aws.filters.FormSignerV2Test;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.io.payloads.StringPayload;
import org.jclouds.rest.RequestSigner;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -88,6 +94,29 @@ public class AWSUtilsTest {
assertNull(utils.parseAWSErrorFromContent(command.getCurrentRequest(), response)); assertNull(utils.parseAWSErrorFromContent(command.getCurrentRequest(), response));
} }
/**
* Do not attempt to parse empty payload.
*/
@Test
public void testNoExceptionEmptyPayload() {
RequestSigner requestSigner = createMock(RequestSigner.class);
ParseSax.Factory factory = createMock(ParseSax.Factory.class);
Provider provider = createMock(Provider.class);
// these all will throw UnexpectedInvocationEx if touched
replay(requestSigner, factory, provider);
AWSUtils riggedUtil = new AWSUtils(
"ignore",
requestSigner,
factory,
provider
);
HttpResponse response = HttpResponse.builder().statusCode(NOT_FOUND.getStatusCode()).payload(new StringPayload("")).build();
response.getPayload().getContentMetadata().setContentType("application/unknown");
assertNull(riggedUtil.parseAWSErrorFromContent(command.getCurrentRequest(), response));
}
@Test @Test
public void testParseAWSErrorFromContentHttpCommandHttpResponseInputStream() { public void testParseAWSErrorFromContentHttpCommandHttpResponseInputStream() {
AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response(getClass() AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response(getClass()

View File

@ -121,8 +121,9 @@ public class HttpUtils {
public static byte[] toByteArrayOrNull(PayloadEnclosing response) { public static byte[] toByteArrayOrNull(PayloadEnclosing response) {
if (response.getPayload() != null) { if (response.getPayload() != null) {
InputStream input = response.getPayload().getInput(); InputStream input = null;
try { try {
input = response.getPayload().openStream();
return toByteArray(input); return toByteArray(input);
} catch (IOException e) { } catch (IOException e) {
propagate(e); propagate(e);