Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.api.client.util.FieldInfo;
import com.google.api.core.InternalApi;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.NullMarked;
Expand All @@ -52,6 +53,30 @@
return null;
}

/**
* Retrieves the first string value of a header by case-insensitive name from a headers map.
*
* @param headers the map of header names to header values
* @param name the case-insensitive header name to look up
* @return the first string value of the header, or {@code null} if not found
*/
public static @Nullable String getFirstHeader(Map<String, Object> headers, String name) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase(name)) {
return extractFirstString(entry.getValue());
}
}
return null;
}

private static @Nullable String extractFirstString(Object headerValue) {
if (headerValue instanceof Iterable) {
Object firstElement = Iterables.getFirst((Iterable<?>) headerValue, null);

Check warning on line 74 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpHeadersUtils.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Annotate the parameter with @javax.annotation.Nullable in method 'getFirst' declaration, or make sure that null can not be passed as argument.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaAW_BkQ5EuKHGL3DY6R&open=AaAW_BkQ5EuKHGL3DY6R&pullRequest=14115
return firstElement != null ? firstElement.toString() : null;

Check warning on line 75 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpHeadersUtils.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Change this condition so that it does not always evaluate to "true"

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaAW_BkQ5EuKHGL3DY6Q&open=AaAW_BkQ5EuKHGL3DY6Q&pullRequest=14115
}
return headerValue.toString();
}

public static HttpHeaders setHeaders(HttpHeaders headers, Map<String, String> headersMap) {
for (Map.Entry<String, String> entry : headersMap.entrySet()) {
setHeader(headers, entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,29 @@ void testGetUserAgentValue() {
headersMap = ImmutableMap.of("Custom-Header", "CustomHeader", "accept", "Accept");
assertNull(HttpHeadersUtils.getUserAgentValue(headersMap));
}

@Test
void testGetFirstHeader() {
Map<String, Object> headersMap =
ImmutableMap.<String, Object>builder()
.put("X-Goog-Upload-URL", "https://test.googleapis.com/upload")
.put("Location", ImmutableList.of("https://redirect.url", "https://secondary.url"))
.put("Content-Length", 1024L)
.put("Empty-List-Header", ImmutableList.of())
.put("Null-First-Header", java.util.Collections.singletonList(null))
.build();

assertEquals(
"https://test.googleapis.com/upload",
HttpHeadersUtils.getFirstHeader(headersMap, "X-Goog-Upload-URL"));
assertEquals(
"https://test.googleapis.com/upload",
HttpHeadersUtils.getFirstHeader(headersMap, "x-goog-upload-url"));
assertEquals(
"https://redirect.url", HttpHeadersUtils.getFirstHeader(headersMap, "location"));
assertEquals("1024", HttpHeadersUtils.getFirstHeader(headersMap, "content-length"));
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "empty-list-header"));
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "null-first-header"));
assertNull(HttpHeadersUtils.getFirstHeader(headersMap, "non-existent-header"));
}
}
Loading