1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.http; 17 18 import java.io.InputStream; 19 import java.util.Optional; 20 import software.amazon.awssdk.annotations.SdkPublicApi; 21 22 @SdkPublicApi 23 public class HttpExecuteResponse { 24 25 private final SdkHttpResponse response; 26 private final Optional<AbortableInputStream> responseBody; 27 HttpExecuteResponse(BuilderImpl builder)28 private HttpExecuteResponse(BuilderImpl builder) { 29 this.response = builder.response; 30 this.responseBody = builder.responseBody; 31 } 32 33 /** 34 * @return The HTTP response. 35 */ httpResponse()36 public SdkHttpResponse httpResponse() { 37 return response; 38 } 39 40 /** 41 * Get the {@link AbortableInputStream} associated with this response. 42 * 43 * <p>Always close the "responseBody" input stream to release the underlying HTTP connection. 44 * Even for error responses, the SDK creates an input stream for reading error data. It is essential to close the input stream 45 * in the "responseBody" attribute for both success and error cases. 46 * 47 * @return An {@link Optional} containing the {@link AbortableInputStream} if available. 48 */ responseBody()49 public Optional<AbortableInputStream> responseBody() { 50 return responseBody; 51 } 52 builder()53 public static Builder builder() { 54 return new BuilderImpl(); 55 } 56 57 public interface Builder { 58 /** 59 * Set the HTTP response to be executed by the client. 60 * 61 * @param response The response. 62 * @return This builder for method chaining. 63 */ response(SdkHttpResponse response)64 Builder response(SdkHttpResponse response); 65 66 /** 67 * Set the {@link InputStream} to be returned by the client. 68 * @param inputStream The {@link InputStream} 69 * @return This builder for method chaining 70 */ responseBody(AbortableInputStream inputStream)71 Builder responseBody(AbortableInputStream inputStream); 72 build()73 HttpExecuteResponse build(); 74 } 75 76 private static class BuilderImpl implements Builder { 77 78 private SdkHttpResponse response; 79 private Optional<AbortableInputStream> responseBody = Optional.empty(); 80 81 @Override response(SdkHttpResponse response)82 public Builder response(SdkHttpResponse response) { 83 this.response = response; 84 return this; 85 } 86 87 @Override responseBody(AbortableInputStream responseBody)88 public Builder responseBody(AbortableInputStream responseBody) { 89 this.responseBody = Optional.ofNullable(responseBody); 90 return this; 91 } 92 93 @Override build()94 public HttpExecuteResponse build() { 95 return new HttpExecuteResponse(this); 96 } 97 } 98 } 99