Skip to content

Commit 4616b5f

Browse files
committed
feat(gax): add getFirstHeader to HttpHeadersUtils
1 parent 9ea126e commit 4616b5f

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpHeadersUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.api.client.util.FieldInfo;
3535
import com.google.api.core.InternalApi;
3636
import com.google.common.collect.ImmutableList;
37+
import com.google.common.collect.Iterables;
3738
import java.util.List;
3839
import java.util.Map;
3940
import org.jspecify.annotations.NullMarked;
@@ -52,6 +53,33 @@ public class HttpHeadersUtils {
5253
return null;
5354
}
5455

56+
/**
57+
* Retrieves the first string value of a header by case-insensitive name from a headers map.
58+
*
59+
* @param headers the map of header names to header values
60+
* @param name the case-insensitive header name to look up
61+
* @return the first string value of the header, or {@code null} if not found
62+
*/
63+
public static @Nullable String getFirstHeader(Map<String, Object> headers, String name) {
64+
for (Map.Entry<String, Object> entry : headers.entrySet()) {
65+
if (entry.getKey().equalsIgnoreCase(name)) {
66+
return extractFirstString(entry.getValue());
67+
}
68+
}
69+
return null;
70+
}
71+
72+
private static @Nullable String extractFirstString(@Nullable Object headerValue) {
73+
if (headerValue == null) {
74+
return null;
75+
}
76+
if (headerValue instanceof Iterable) {
77+
Object firstElement = Iterables.getFirst((Iterable<?>) headerValue, null);
78+
return firstElement != null ? firstElement.toString() : null;
79+
}
80+
return headerValue.toString();
81+
}
82+
5583
public static HttpHeaders setHeaders(HttpHeaders headers, Map<String, String> headersMap) {
5684
for (Map.Entry<String, String> entry : headersMap.entrySet()) {
5785
setHeader(headers, entry.getKey(), entry.getValue());

sdk-platform-java/gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpHeadersUtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.api.client.http.HttpHeaders;
3636
import com.google.common.collect.ImmutableList;
3737
import com.google.common.collect.ImmutableMap;
38+
import java.util.Collections;
3839
import java.util.Map;
3940
import org.junit.jupiter.api.Test;
4041

@@ -130,4 +131,35 @@ void testGetUserAgentValue() {
130131
headersMap = ImmutableMap.of("Custom-Header", "CustomHeader", "accept", "Accept");
131132
assertNull(HttpHeadersUtils.getUserAgentValue(headersMap));
132133
}
134+
135+
@Test
136+
void testGetFirstHeader() {
137+
Map<String, Object> headersMap =
138+
ImmutableMap.<String, Object>builder()
139+
.put("X-Goog-Upload-URL", "https://test.googleapis.com/upload")
140+
.put("Location", ImmutableList.of("https://redirect.url", "https://secondary.url"))
141+
.put("Content-Length", 1024L)
142+
.put("Empty-List-Header", ImmutableList.of())
143+
.put("Null-First-Header", java.util.Collections.singletonList(null))
144+
.build();
145+
146+
assertEquals(
147+
"https://test.googleapis.com/upload",
148+
HttpHeadersUtils.getFirstHeader(headersMap, "X-Goog-Upload-URL"));
149+
assertEquals(
150+
"https://test.googleapis.com/upload",
151+
HttpHeadersUtils.getFirstHeader(headersMap, "x-goog-upload-url"));
152+
assertEquals(
153+
"https://redirect.url", HttpHeadersUtils.getFirstHeader(headersMap, "location"));
154+
assertEquals("1024", HttpHeadersUtils.getFirstHeader(headersMap, "content-length"));
155+
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "empty-list-header"));
156+
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "null-first-header"));
157+
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "non-existent-header"));
158+
}
159+
160+
@Test
161+
void testGetFirstHeader_nullValueInMap_returnsNull() {
162+
Map<String, Object> headersMap = Collections.singletonMap("Null-Value-Header", null);
163+
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "null-value-header"));
164+
}
133165
}

0 commit comments

Comments
 (0)