From 392c249c36ab1bab6d6b5d12fc11accc91ba872d Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Wed, 13 Nov 2024 00:36:07 +0900 Subject: [PATCH] Remove useless annotation, make a new private method for authority. --- .../client/brave/ArmeriaHttpClientParser.java | 2 +- ...equestContextUtil.java => ObjectUtil.java} | 12 +++-- ...efaultHttpClientObservationConvention.java | 2 +- .../client/DefaultClientRequestContext.java | 44 ++++++++++--------- 4 files changed, 30 insertions(+), 30 deletions(-) rename core/src/main/java/com/linecorp/armeria/client/{ClientRequestContextUtil.java => ObjectUtil.java} (82%) diff --git a/brave/brave6/src/main/java/com/linecorp/armeria/client/brave/ArmeriaHttpClientParser.java b/brave/brave6/src/main/java/com/linecorp/armeria/client/brave/ArmeriaHttpClientParser.java index d96df0ab828..96d8789d8ff 100644 --- a/brave/brave6/src/main/java/com/linecorp/armeria/client/brave/ArmeriaHttpClientParser.java +++ b/brave/brave6/src/main/java/com/linecorp/armeria/client/brave/ArmeriaHttpClientParser.java @@ -16,7 +16,7 @@ package com.linecorp.armeria.client.brave; -import static com.linecorp.armeria.client.ClientRequestContextUtil.firstNonNullException; +import static com.linecorp.armeria.client.ObjectUtil.firstNonNullException; import java.net.InetSocketAddress; diff --git a/core/src/main/java/com/linecorp/armeria/client/ClientRequestContextUtil.java b/core/src/main/java/com/linecorp/armeria/client/ObjectUtil.java similarity index 82% rename from core/src/main/java/com/linecorp/armeria/client/ClientRequestContextUtil.java rename to core/src/main/java/com/linecorp/armeria/client/ObjectUtil.java index 221ccefbbdf..bb0a412c614 100644 --- a/core/src/main/java/com/linecorp/armeria/client/ClientRequestContextUtil.java +++ b/core/src/main/java/com/linecorp/armeria/client/ObjectUtil.java @@ -17,19 +17,17 @@ import java.util.function.Supplier; -import javax.annotation.CheckForNull; - /** - * The utility class for ClientRequestContext. + * The utility class for Object. */ -public final class ClientRequestContextUtil { +public final class ObjectUtil { /** * Returns a non-null value. If an unexpected exception occurs while retrieving the first value, * or the first value is null, this function will return the second value. * Otherwise, it returns the first value. */ - public static T firstNonNullException(@CheckForNull Supplier firstSupplier, @CheckForNull T second) { + public static T firstNonNullException(Supplier firstSupplier, T second) { try { if (firstSupplier != null) { final T first = firstSupplier.get(); @@ -45,9 +43,9 @@ public static T firstNonNullException(@CheckForNull Supplier firstSupplie return second; } - throw new NullPointerException("Both parameters are null"); + throw new NullPointerException("Both parameters are null."); } - private ClientRequestContextUtil() { + private ObjectUtil() { } } diff --git a/core/src/main/java/com/linecorp/armeria/client/observation/DefaultHttpClientObservationConvention.java b/core/src/main/java/com/linecorp/armeria/client/observation/DefaultHttpClientObservationConvention.java index 30e7b74aa67..830a5102e63 100644 --- a/core/src/main/java/com/linecorp/armeria/client/observation/DefaultHttpClientObservationConvention.java +++ b/core/src/main/java/com/linecorp/armeria/client/observation/DefaultHttpClientObservationConvention.java @@ -16,7 +16,7 @@ package com.linecorp.armeria.client.observation; -import static com.linecorp.armeria.client.ClientRequestContextUtil.firstNonNullException; +import static com.linecorp.armeria.client.ObjectUtil.firstNonNullException; import java.net.InetSocketAddress; diff --git a/core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java b/core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java index 05a84a92a5c..0b9deabd3bf 100644 --- a/core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java +++ b/core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java @@ -483,20 +483,15 @@ private void failEarly(Throwable cause) { // TODO(ikhoon): Consider moving the logic for filling authority to `HttpClientDelegate.exceute()`. private void autoFillSchemeAuthorityAndOrigin() { - - try { - final String authority = authority(); - if (endpoint != null && endpoint.isIpAddrOnly()) { - // The connection will be established with the IP address but `host` set to the `Endpoint` - // could be used for SNI. It would make users send HTTPS requests - // with CSLB or configure a reverse proxy based on an authority. - final String host = SchemeAndAuthority.of(null, authority).host(); - if (!NetUtil.isValidIpV4Address(host) && !NetUtil.isValidIpV6Address(host)) { - endpoint = endpoint.withHost(host); - } + final String authority = authorityOrNull(); + if (authority != null && endpoint != null && endpoint.isIpAddrOnly()) { + // The connection will be established with the IP address but `host` set to the `Endpoint` + // could be used for SNI. It would make users send HTTPS requests with CSLB or configure a reverse + // proxy based on an authority. + final String host = SchemeAndAuthority.of(null, authority).host(); + if (!NetUtil.isValidIpV4Address(host) && !NetUtil.isValidIpV6Address(host)) { + endpoint = endpoint.withHost(host); } - } catch (IllegalStateException e) { - // Just pass, because it's normal condition. } final HttpHeadersBuilder headersBuilder = internalRequestHeaders.toBuilder(); @@ -757,6 +752,17 @@ public String fragment() { @Override public String authority() { + final String authority = authorityOrNull(); + if (authority == null) { + throw new IllegalStateException( + "ClientRequestContext may be in the process of initialization." + + "In this case, host() or authority() could be null"); + } + return authority; + } + + @Nullable + private String authorityOrNull() { final HttpHeaders additionalRequestHeaders = this.additionalRequestHeaders; String authority = additionalRequestHeaders.get(HttpHeaderNames.AUTHORITY); if (authority == null) { @@ -778,11 +784,7 @@ public String authority() { if (authority == null) { authority = internalRequestHeaders.get(HttpHeaderNames.HOST); } - if (authority == null) { - throw new IllegalStateException( - "ClientRequestContext may be in the process of initialization." + - "In this case, host() or authority() could be null"); - } + return authority; } @@ -812,16 +814,16 @@ public String host() { @Override public URI uri() { final String scheme = getScheme(sessionProtocol()); + final String authority = authorityOrNull(); final String path = path(); final String query = query(); final String fragment = fragment(); try (TemporaryThreadLocals tmp = TemporaryThreadLocals.acquire()) { final StringBuilder buf = tmp.stringBuilder(); buf.append(scheme); - try { - final String authority = authority(); + if (authority != null) { buf.append("://").append(authority); - } catch (IllegalStateException e) { + } else { buf.append(':'); } buf.append(path);