8277120: Use Optional.isEmpty instead of !Optional.isPresent in java.net.http

Reviewed-by: dfuchs
This commit is contained in:
Andrey Turbanov 2022-01-21 08:53:37 +00:00
parent 19f877981e
commit 47b1c51bbd
5 changed files with 9 additions and 11 deletions

View File

@ -305,7 +305,7 @@ class AuthenticationFilter implements HeaderFilter {
AuthInfo au = proxy ? exchange.proxyauth : exchange.serverauth;
if (au == null) {
// if no authenticator, let the user deal with 407/401
if (!exchange.client().authenticator().isPresent()) return null;
if (exchange.client().authenticator().isEmpty()) return null;
PasswordAuthentication pw = getCredentials(authval, proxy, req);
if (pw == null) {
@ -331,7 +331,7 @@ class AuthenticationFilter implements HeaderFilter {
}
// if no authenticator, let the user deal with 407/401
if (!exchange.client().authenticator().isPresent()) return null;
if (exchange.client().authenticator().isEmpty()) return null;
// try again
PasswordAuthentication pw = getCredentials(authval, proxy, req);

View File

@ -124,7 +124,7 @@ public class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
checkTimeout(timeout);
this.systemHeadersBuilder = new HttpHeadersBuilder();
}
if (!userHeaders.firstValue("User-Agent").isPresent()) {
if (userHeaders.firstValue("User-Agent").isEmpty()) {
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
}
this.uri = requestURI;
@ -182,7 +182,7 @@ public class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
this.userHeaders = other.userHeaders;
this.isWebSocket = other.isWebSocket;
this.systemHeadersBuilder = new HttpHeadersBuilder();
if (!userHeaders.firstValue("User-Agent").isPresent()) {
if (userHeaders.firstValue("User-Agent").isEmpty()) {
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
}
this.uri = uri;

View File

@ -175,7 +175,7 @@ public final class Utils {
// used by caller.
public static final BiPredicate<String, String> CONTEXT_RESTRICTED(HttpClient client) {
return (k, v) -> !client.authenticator().isPresent() ||
return (k, v) -> client.authenticator().isEmpty() ||
(!k.equalsIgnoreCase("Authorization")
&& !k.equalsIgnoreCase("Proxy-Authorization"));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -72,7 +72,7 @@ public final class BuilderImpl implements Builder {
this.proxySelector = proxySelector;
// If a proxy selector was supplied by the user, it should be present
// on the client and should be the same that what we got as an argument
assert !client.proxy().isPresent()
assert client.proxy().isEmpty()
|| client.proxy().equals(proxySelector);
this.headers = headers;
this.subprotocols = subprotocols;

View File

@ -28,7 +28,6 @@ package jdk.internal.net.http.websocket;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.WebSocketHandshakeException;
@ -62,7 +61,6 @@ import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.String.format;
@ -281,7 +279,7 @@ public class OpeningHandshake {
throws CheckFailedException
{
Optional<String> opt = responseHeaders.firstValue(HEADER_PROTOCOL);
if (!opt.isPresent()) {
if (opt.isEmpty()) {
// If there is no such header in the response, then the server
// doesn't want to use any subprotocol
return "";
@ -363,7 +361,7 @@ public class OpeningHandshake {
* or {@code null} if none is required or applicable.
*/
private static Proxy proxyFor(Optional<ProxySelector> selector, URI uri) {
if (!selector.isPresent()) {
if (selector.isEmpty()) {
return null;
}
URI requestURI = createRequestURI(uri); // Based on the HTTP scheme