Skip to content

Commit

Permalink
[java] simplify reading http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 9, 2024
1 parent e202389 commit 305280b
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions java/src/org/openqa/selenium/remote/http/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.openqa.selenium.internal.Require;

abstract class HttpMessage<M extends HttpMessage<M>> {
Expand Down Expand Up @@ -92,11 +90,8 @@ public Iterable<String> getHeaderNames() {
* @return an iterable view of the values
*/
public Iterable<String> getHeaders(String name) {
return headers.entrySet().stream()
.filter(e -> Objects.nonNull(e.getKey()))
.filter(e -> e.getKey().equalsIgnoreCase(name.toLowerCase()))
.flatMap((e) -> e.getValue().stream())
.collect(Collectors.toList());
return Collections.unmodifiableCollection(
headers.getOrDefault(name.toLowerCase(), Collections.emptyList()));
}

/**
Expand All @@ -106,12 +101,8 @@ public Iterable<String> getHeaders(String name) {
* @return the value
*/
public String getHeader(String name) {
return headers.entrySet().stream()
.filter(e -> Objects.nonNull(e.getKey()))
.filter(e -> e.getKey().equalsIgnoreCase(name.toLowerCase()))
.flatMap((e) -> e.getValue().stream())
.findFirst()
.orElse(null);
List<String> values = headers.getOrDefault(name.toLowerCase(), Collections.emptyList());
return !values.isEmpty() ? values.get(0) : null;
}

/**
Expand Down Expand Up @@ -146,7 +137,7 @@ public M addHeader(String name, String value) {
* @return self
*/
public M removeHeader(String name) {
headers.keySet().removeIf(header -> header.equalsIgnoreCase(name));
headers.remove(name.toLowerCase());
return self();
}

Expand Down

0 comments on commit 305280b

Please sign in to comment.