|
| 1 | +package com.stripe.net; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | + |
| 5 | +import com.stripe.util.CaseInsensitiveMap; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Optional; |
| 12 | +import lombok.EqualsAndHashCode; |
| 13 | + |
| 14 | +/** |
| 15 | + * A read-only view of a set of HTTP headers. |
| 16 | + * |
| 17 | + * <p>This class mimics the {@code java.net.http.HttpHeaders} added in Java 11. |
| 18 | + */ |
| 19 | +@EqualsAndHashCode |
| 20 | +public class HttpHeaders { |
| 21 | + private CaseInsensitiveMap<List<String>> headerMap; |
| 22 | + |
| 23 | + private HttpHeaders(CaseInsensitiveMap<List<String>> headerMap) { |
| 24 | + this.headerMap = headerMap; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns an {@link HttpHeaders} instance initialized from the given map. |
| 29 | + * |
| 30 | + * @param headerMap the map containing the header names and values |
| 31 | + * @return an {@link HttpHeaders} instance containing the given headers |
| 32 | + * @throws NullPointerException if {@code headerMap} is {@code null} |
| 33 | + */ |
| 34 | + public static HttpHeaders of(Map<String, List<String>> headerMap) { |
| 35 | + requireNonNull(headerMap); |
| 36 | + return new HttpHeaders(CaseInsensitiveMap.of(headerMap)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus |
| 41 | + * the provided header. |
| 42 | + * |
| 43 | + * @param name the name of the header to add |
| 44 | + * @param value the value of the header to add |
| 45 | + * @return the new {@link HttpHeaders} instance |
| 46 | + * @throws NullPointerException if {@code name} or {@code value} is {@code null} |
| 47 | + */ |
| 48 | + public HttpHeaders withAdditionalHeader(String name, String value) { |
| 49 | + requireNonNull(name); |
| 50 | + requireNonNull(value); |
| 51 | + return this.withAdditionalHeader(name, Arrays.asList(value)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus |
| 56 | + * the provided header. |
| 57 | + * |
| 58 | + * @param name the name of the header to add |
| 59 | + * @param values the values of the header to add |
| 60 | + * @return the new {@link HttpHeaders} instance |
| 61 | + * @throws NullPointerException if {@code name} or {@code values} is {@code null} |
| 62 | + */ |
| 63 | + public HttpHeaders withAdditionalHeader(String name, List<String> values) { |
| 64 | + requireNonNull(name); |
| 65 | + requireNonNull(values); |
| 66 | + Map<String, List<String>> headerMap = new HashMap<>(); |
| 67 | + headerMap.put(name, values); |
| 68 | + return this.withAdditionalHeaders(headerMap); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus |
| 73 | + * the provided headers. |
| 74 | + * |
| 75 | + * @param headerMap the map containing the headers to add |
| 76 | + * @return the new {@link HttpHeaders} instance |
| 77 | + * @throws NullPointerException if {@code headerMap} is {@code null} |
| 78 | + */ |
| 79 | + public HttpHeaders withAdditionalHeaders(Map<String, List<String>> headerMap) { |
| 80 | + requireNonNull(headerMap); |
| 81 | + Map<String, List<String>> newHeaderMap = new HashMap<>(this.map()); |
| 82 | + newHeaderMap.putAll(headerMap); |
| 83 | + return HttpHeaders.of(newHeaderMap); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns an unmodifiable List of all of the header string values of the given named header. |
| 88 | + * Always returns a List, which may be empty if the header is not present. |
| 89 | + * |
| 90 | + * @param name the header name |
| 91 | + * @return a List of headers string values |
| 92 | + */ |
| 93 | + public List<String> allValues(String name) { |
| 94 | + if (this.headerMap.containsKey(name)) { |
| 95 | + List<String> values = this.headerMap.get(name); |
| 96 | + if ((values != null) && (values.size() > 0)) { |
| 97 | + return Collections.unmodifiableList(values); |
| 98 | + } |
| 99 | + } |
| 100 | + return Collections.emptyList(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns an {@link Optional} containing the first header string value of the given named (and |
| 105 | + * possibly multi-valued) header. If the header is not present, then the returned {@code Optional} |
| 106 | + * is empty. |
| 107 | + * |
| 108 | + * @param name the header name |
| 109 | + * @return an {@code Optional<String>} containing the first named header string value, if present |
| 110 | + */ |
| 111 | + public Optional<String> firstValue(String name) { |
| 112 | + if (this.headerMap.containsKey(name)) { |
| 113 | + List<String> values = this.headerMap.get(name); |
| 114 | + if ((values != null) && (values.size() > 0)) { |
| 115 | + return Optional.of(values.get(0)); |
| 116 | + } |
| 117 | + } |
| 118 | + return Optional.empty(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns an unmodifiable Map view of this HttpHeaders. |
| 123 | + * |
| 124 | + * @return the Map |
| 125 | + */ |
| 126 | + public Map<String, List<String>> map() { |
| 127 | + return Collections.unmodifiableMap(this.headerMap); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns this {@link HttpHeaders} as a string. |
| 132 | + * |
| 133 | + * @return a string describing the HTTP headers |
| 134 | + */ |
| 135 | + @Override |
| 136 | + public String toString() { |
| 137 | + StringBuilder sb = new StringBuilder(); |
| 138 | + sb.append(super.toString()); |
| 139 | + sb.append(" { "); |
| 140 | + sb.append(map()); |
| 141 | + sb.append(" }"); |
| 142 | + return sb.toString(); |
| 143 | + } |
| 144 | +} |
0 commit comments