Skip to content

Commit 392c249

Browse files
Remove useless annotation, make a new private method for authority.
1 parent 45dc82b commit 392c249

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

brave/brave6/src/main/java/com/linecorp/armeria/client/brave/ArmeriaHttpClientParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.linecorp.armeria.client.brave;
1818

19-
import static com.linecorp.armeria.client.ClientRequestContextUtil.firstNonNullException;
19+
import static com.linecorp.armeria.client.ObjectUtil.firstNonNullException;
2020

2121
import java.net.InetSocketAddress;
2222

core/src/main/java/com/linecorp/armeria/client/ClientRequestContextUtil.java core/src/main/java/com/linecorp/armeria/client/ObjectUtil.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717

1818
import java.util.function.Supplier;
1919

20-
import javax.annotation.CheckForNull;
21-
2220
/**
23-
* The utility class for ClientRequestContext.
21+
* The utility class for Object.
2422
*/
25-
public final class ClientRequestContextUtil {
23+
public final class ObjectUtil {
2624

2725
/**
2826
* Returns a non-null value. If an unexpected exception occurs while retrieving the first value,
2927
* or the first value is null, this function will return the second value.
3028
* Otherwise, it returns the first value.
3129
*/
32-
public static <T> T firstNonNullException(@CheckForNull Supplier<T> firstSupplier, @CheckForNull T second) {
30+
public static <T> T firstNonNullException(Supplier<T> firstSupplier, T second) {
3331
try {
3432
if (firstSupplier != null) {
3533
final T first = firstSupplier.get();
@@ -45,9 +43,9 @@ public static <T> T firstNonNullException(@CheckForNull Supplier<T> firstSupplie
4543
return second;
4644
}
4745

48-
throw new NullPointerException("Both parameters are null");
46+
throw new NullPointerException("Both parameters are null.");
4947
}
5048

51-
private ClientRequestContextUtil() {
49+
private ObjectUtil() {
5250
}
5351
}

core/src/main/java/com/linecorp/armeria/client/observation/DefaultHttpClientObservationConvention.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.linecorp.armeria.client.observation;
1818

19-
import static com.linecorp.armeria.client.ClientRequestContextUtil.firstNonNullException;
19+
import static com.linecorp.armeria.client.ObjectUtil.firstNonNullException;
2020

2121
import java.net.InetSocketAddress;
2222

core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java

+23-21
Original file line numberDiff line numberDiff line change
@@ -483,20 +483,15 @@ private void failEarly(Throwable cause) {
483483

484484
// TODO(ikhoon): Consider moving the logic for filling authority to `HttpClientDelegate.exceute()`.
485485
private void autoFillSchemeAuthorityAndOrigin() {
486-
487-
try {
488-
final String authority = authority();
489-
if (endpoint != null && endpoint.isIpAddrOnly()) {
490-
// The connection will be established with the IP address but `host` set to the `Endpoint`
491-
// could be used for SNI. It would make users send HTTPS requests
492-
// with CSLB or configure a reverse proxy based on an authority.
493-
final String host = SchemeAndAuthority.of(null, authority).host();
494-
if (!NetUtil.isValidIpV4Address(host) && !NetUtil.isValidIpV6Address(host)) {
495-
endpoint = endpoint.withHost(host);
496-
}
486+
final String authority = authorityOrNull();
487+
if (authority != null && endpoint != null && endpoint.isIpAddrOnly()) {
488+
// The connection will be established with the IP address but `host` set to the `Endpoint`
489+
// could be used for SNI. It would make users send HTTPS requests with CSLB or configure a reverse
490+
// proxy based on an authority.
491+
final String host = SchemeAndAuthority.of(null, authority).host();
492+
if (!NetUtil.isValidIpV4Address(host) && !NetUtil.isValidIpV6Address(host)) {
493+
endpoint = endpoint.withHost(host);
497494
}
498-
} catch (IllegalStateException e) {
499-
// Just pass, because it's normal condition.
500495
}
501496

502497
final HttpHeadersBuilder headersBuilder = internalRequestHeaders.toBuilder();
@@ -757,6 +752,17 @@ public String fragment() {
757752

758753
@Override
759754
public String authority() {
755+
final String authority = authorityOrNull();
756+
if (authority == null) {
757+
throw new IllegalStateException(
758+
"ClientRequestContext may be in the process of initialization." +
759+
"In this case, host() or authority() could be null");
760+
}
761+
return authority;
762+
}
763+
764+
@Nullable
765+
private String authorityOrNull() {
760766
final HttpHeaders additionalRequestHeaders = this.additionalRequestHeaders;
761767
String authority = additionalRequestHeaders.get(HttpHeaderNames.AUTHORITY);
762768
if (authority == null) {
@@ -778,11 +784,7 @@ public String authority() {
778784
if (authority == null) {
779785
authority = internalRequestHeaders.get(HttpHeaderNames.HOST);
780786
}
781-
if (authority == null) {
782-
throw new IllegalStateException(
783-
"ClientRequestContext may be in the process of initialization." +
784-
"In this case, host() or authority() could be null");
785-
}
787+
786788
return authority;
787789
}
788790

@@ -812,16 +814,16 @@ public String host() {
812814
@Override
813815
public URI uri() {
814816
final String scheme = getScheme(sessionProtocol());
817+
final String authority = authorityOrNull();
815818
final String path = path();
816819
final String query = query();
817820
final String fragment = fragment();
818821
try (TemporaryThreadLocals tmp = TemporaryThreadLocals.acquire()) {
819822
final StringBuilder buf = tmp.stringBuilder();
820823
buf.append(scheme);
821-
try {
822-
final String authority = authority();
824+
if (authority != null) {
823825
buf.append("://").append(authority);
824-
} catch (IllegalStateException e) {
826+
} else {
825827
buf.append(':');
826828
}
827829
buf.append(path);

0 commit comments

Comments
 (0)