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.util.Collection; 19 import java.util.List; 20 import java.util.Map; 21 import java.util.Optional; 22 import java.util.function.BiConsumer; 23 import java.util.stream.Collectors; 24 import software.amazon.awssdk.annotations.Immutable; 25 import software.amazon.awssdk.annotations.SdkPublicApi; 26 import software.amazon.awssdk.utils.http.SdkHttpUtils; 27 28 /** 29 * An immutable set of HTTP headers. {@link SdkHttpRequest} should be used for requests, and {@link SdkHttpResponse} should be 30 * used for responses. 31 */ 32 @SdkPublicApi 33 @Immutable 34 public interface SdkHttpHeaders { 35 /** 36 * Returns a map of all HTTP headers in this message, sorted in case-insensitive order by their header name. 37 * 38 * <p>This will never be null. If there are no headers an empty map is returned.</p> 39 * 40 * @return An unmodifiable map of all headers in this message. 41 */ headers()42 Map<String, List<String>> headers(); 43 44 /** 45 * Perform a case-insensitive search for a particular header in this request, returning the first matching header, if one is 46 * found. 47 * 48 * <p>This is useful for headers like 'Content-Type' or 'Content-Length' of which there is expected to be only one value 49 * present.</p> 50 * 51 * <p>This is equivalent to invoking {@link SdkHttpUtils#firstMatchingHeader(Map, String)}</p>. 52 * 53 * @param header The header to search for (case insensitively). 54 * @return The first header that matched the requested one, or empty if one was not found. 55 */ firstMatchingHeader(String header)56 default Optional<String> firstMatchingHeader(String header) { 57 return SdkHttpUtils.firstMatchingHeader(headers(), header); 58 } 59 firstMatchingHeader(Collection<String> headersToFind)60 default Optional<String> firstMatchingHeader(Collection<String> headersToFind) { 61 return SdkHttpUtils.firstMatchingHeaderFromCollection(headers(), headersToFind); 62 } 63 matchingHeaders(String header)64 default List<String> matchingHeaders(String header) { 65 return SdkHttpUtils.allMatchingHeaders(headers(), header).collect(Collectors.toList()); 66 } 67 forEachHeader(BiConsumer<? super String, ? super List<String>> consumer)68 default void forEachHeader(BiConsumer<? super String, ? super List<String>> consumer) { 69 headers().forEach(consumer); 70 } 71 numHeaders()72 default int numHeaders() { 73 return headers().size(); 74 } 75 } 76