Skip to content
Permalink
Browse files
JCLOUDS-1552: Do not attempt to parse empty payload (#82)
  • Loading branch information
cstamas committed Sep 21, 2020
1 parent 3ea2cce commit d65047c87b46717605742057b59847673339e8fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
@@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream;

import java.util.Collection;
import java.util.Map;
@@ -82,7 +83,8 @@ public String apply(Location location) {
}

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;
if ("text/plain".equals(response.getPayload().getContentMetadata().getContentType()))
return null;
@@ -17,6 +17,7 @@
package org.jclouds.aws.util;
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.NOT_FOUND;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
@@ -26,12 +27,17 @@
import java.io.IOException;
import java.io.InputStream;

import javax.inject.Provider;

import org.jclouds.aws.domain.AWSError;
import org.jclouds.aws.filters.FormSignerV2Test;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest;
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.BeforeTest;
import org.testng.annotations.Test;
@@ -88,6 +94,29 @@ public void testNoExceptionParsingTextPlain() {
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
public void testParseAWSErrorFromContentHttpCommandHttpResponseInputStream() {
AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response(getClass()
@@ -121,8 +121,9 @@ public int getMaxConnectionsPerHost() {

public static byte[] toByteArrayOrNull(PayloadEnclosing response) {
if (response.getPayload() != null) {
InputStream input = response.getPayload().getInput();
InputStream input = null;
try {
input = response.getPayload().openStream();
return toByteArray(input);
} catch (IOException e) {
propagate(e);

0 comments on commit d65047c

Please sign in to comment.