/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.awssdk.protocols.xml.internal.unmarshall;

import java.util.Optional;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkPojo;
import software.amazon.awssdk.core.protocol.MarshallLocation;
import software.amazon.awssdk.http.AbortableInputStream;
import software.amazon.awssdk.http.SdkHttpFullResponse;
import software.amazon.awssdk.protocols.query.unmarshall.XmlDomParser;
import software.amazon.awssdk.protocols.query.unmarshall.XmlElement;

/**
 * Static methods to assist with parsing the response of AWS XML requests.
 */
@SdkInternalApi
public final class XmlResponseParserUtils {
    private XmlResponseParserUtils() {
    }

    /**
     * Parse an XML response if one is expected and available. If we are not expecting a payload, but the HTTP response
     * code shows an error then we will parse it anyway, as it should contain a serialized error.
     * @param sdkPojo the SDK builder object associated with the final response
     * @param response the HTTP response
     * @return A parsed XML document or an empty XML document if no payload/contents were found in the response.
     */
    public static XmlElement parse(SdkPojo sdkPojo, SdkHttpFullResponse response) {

        try {
            Optional<AbortableInputStream> responseContent = response.content();

            // In some cases the responseContent is present but empty, so when we are not expecting a body we should
            // not attempt to parse it even if the body appears to be present.
            if ((!response.isSuccessful() || hasPayloadMembers(sdkPojo)) && responseContent.isPresent()) {
                return XmlDomParser.parse(responseContent.get());
            } else {
                return XmlElement.empty();
            }
        } catch (RuntimeException e) {
            if (response.isSuccessful()) {
                throw e;
            }

            return XmlElement.empty();
        }
    }

    private static boolean hasPayloadMembers(SdkPojo sdkPojo) {
        return sdkPojo.sdkFields().stream()
                      .anyMatch(f -> f.location() == MarshallLocation.PAYLOAD);
    }
}

@@ -1224,16 +1224,83 @@
 Utils %7B%0A
+    private static final String CONTENT_LENGTH = %22Content-Length%22;%0A
     priv
@@ -2285,16 +2285,66 @@
 resent()
+ &&%0A                contentLengthNotZero(response)
 ) %7B%0A    
@@ -2854,11 +2854,206 @@
 );%0A    %7D
+%0A%0A    private static boolean contentLengthNotZero(SdkHttpFullResponse response) %7B%0A        return response.firstMatchingHeader(CONTENT_LENGTH).filter(h -%3E Long.parseLong(h) %3E 0).isPresent();%0A    %7D
 %0A%7D%0A

Fix an issue where the SDK attempted to parse an empty xml content returned from the service, throwing unable to unmarshall error