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.core; 17 18 import java.util.Objects; 19 import java.util.Optional; 20 import software.amazon.awssdk.annotations.Immutable; 21 import software.amazon.awssdk.annotations.SdkPublicApi; 22 import software.amazon.awssdk.http.SdkHttpResponse; 23 24 /** 25 * The base class for all SDK responses. 26 * 27 * @see SdkRequest 28 */ 29 @Immutable 30 @SdkPublicApi 31 public abstract class SdkResponse implements SdkPojo { 32 33 private final SdkHttpResponse sdkHttpResponse; 34 SdkResponse(Builder builder)35 protected SdkResponse(Builder builder) { 36 this.sdkHttpResponse = builder.sdkHttpResponse(); 37 } 38 39 /** 40 * @return HTTP response data returned from the service. 41 * 42 * @see SdkHttpResponse 43 */ sdkHttpResponse()44 public SdkHttpResponse sdkHttpResponse() { 45 return sdkHttpResponse; 46 } 47 48 /** 49 * Used to retrieve the value of a field from any class that extends {@link SdkResponse}. The field name 50 * specified should match the member name from the corresponding service-2.json model specified in the 51 * codegen-resources folder for a given service. The class specifies what class to cast the returned value to. 52 * If the returned value is also a modeled class, the {@link #getValueForField(String, Class)} method will 53 * again be available. 54 * 55 * @param fieldName The name of the member to be retrieved. 56 * @param clazz The class to cast the returned object to. 57 * @return Optional containing the casted return value 58 */ getValueForField(String fieldName, Class<T> clazz)59 public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { 60 return Optional.empty(); 61 } 62 toBuilder()63 public abstract Builder toBuilder(); 64 65 @Override equals(Object o)66 public boolean equals(Object o) { 67 if (this == o) { 68 return true; 69 } 70 if (o == null || getClass() != o.getClass()) { 71 return false; 72 } 73 SdkResponse that = (SdkResponse) o; 74 return Objects.equals(sdkHttpResponse, that.sdkHttpResponse); 75 } 76 77 @Override hashCode()78 public int hashCode() { 79 return Objects.hashCode(sdkHttpResponse); 80 } 81 82 public interface Builder { 83 sdkHttpResponse(SdkHttpResponse sdkHttpResponse)84 Builder sdkHttpResponse(SdkHttpResponse sdkHttpResponse); 85 sdkHttpResponse()86 SdkHttpResponse sdkHttpResponse(); 87 build()88 SdkResponse build(); 89 } 90 91 protected abstract static class BuilderImpl implements Builder { 92 93 private SdkHttpResponse sdkHttpResponse; 94 BuilderImpl()95 protected BuilderImpl() { 96 } 97 BuilderImpl(SdkResponse response)98 protected BuilderImpl(SdkResponse response) { 99 this.sdkHttpResponse = response.sdkHttpResponse(); 100 } 101 102 @Override sdkHttpResponse(SdkHttpResponse sdkHttpResponse)103 public Builder sdkHttpResponse(SdkHttpResponse sdkHttpResponse) { 104 this.sdkHttpResponse = sdkHttpResponse; 105 return this; 106 } 107 108 @Override sdkHttpResponse()109 public SdkHttpResponse sdkHttpResponse() { 110 return sdkHttpResponse; 111 } 112 } 113 } 114