Skip to content

Commit

Permalink
Remove useless annotation, make a new private method for authority.
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenchickenlove committed Nov 12, 2024
1 parent 45dc82b commit 392c249
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> T firstNonNullException(@CheckForNull Supplier<T> firstSupplier, @CheckForNull T second) {
public static <T> T firstNonNullException(Supplier<T> firstSupplier, T second) {
try {
if (firstSupplier != null) {
final T first = firstSupplier.get();
Expand All @@ -45,9 +43,9 @@ public static <T> T firstNonNullException(@CheckForNull Supplier<T> firstSupplie
return second;
}

throw new NullPointerException("Both parameters are null");
throw new NullPointerException("Both parameters are null.");
}

private ClientRequestContextUtil() {
private ObjectUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 392c249

Please sign in to comment.