okhttp: make OkHttpChannelBuilder final

This commit is contained in:
Sergii Tkachenko 2020-09-11 18:42:00 -04:00 committed by Sergii Tkachenko
parent d5dcfa737a
commit af6fbf6b74
2 changed files with 22 additions and 23 deletions

View File

@ -58,7 +58,7 @@ import javax.net.ssl.SSLSocketFactory;
/** Convenience class for building channels with the OkHttp transport. */ /** Convenience class for building channels with the OkHttp transport. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785") @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannelBuilder> { public final class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannelBuilder> {
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535; public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;
private final ManagedChannelImplBuilder managedChannelImplBuilder; private final ManagedChannelImplBuilder managedChannelImplBuilder;
@ -142,7 +142,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
*/ */
private final boolean useGetForSafeMethods = false; private final boolean useGetForSafeMethods = false;
protected OkHttpChannelBuilder(String host, int port) { private OkHttpChannelBuilder(String host, int port) {
this(GrpcUtil.authorityFromHostAndPort(host, port)); this(GrpcUtil.authorityFromHostAndPort(host, port));
} }
@ -168,13 +168,12 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
@Internal @Internal
@Override @Override
protected final ManagedChannelBuilder<?> delegate() { protected ManagedChannelBuilder<?> delegate() {
return managedChannelImplBuilder; return managedChannelImplBuilder;
} }
@VisibleForTesting @VisibleForTesting
final OkHttpChannelBuilder setTransportTracerFactory( OkHttpChannelBuilder setTransportTracerFactory(TransportTracer.Factory transportTracerFactory) {
TransportTracer.Factory transportTracerFactory) {
this.transportTracerFactory = transportTracerFactory; this.transportTracerFactory = transportTracerFactory;
return this; return this;
} }
@ -185,7 +184,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* <p>The channel does not take ownership of the given executor. It is the caller' responsibility * <p>The channel does not take ownership of the given executor. It is the caller' responsibility
* to shutdown the executor when appropriate. * to shutdown the executor when appropriate.
*/ */
public final OkHttpChannelBuilder transportExecutor(@Nullable Executor transportExecutor) { public OkHttpChannelBuilder transportExecutor(@Nullable Executor transportExecutor) {
this.transportExecutor = transportExecutor; this.transportExecutor = transportExecutor;
return this; return this;
} }
@ -196,7 +195,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* *
* @since 1.20.0 * @since 1.20.0
*/ */
public final OkHttpChannelBuilder socketFactory(@Nullable SocketFactory socketFactory) { public OkHttpChannelBuilder socketFactory(@Nullable SocketFactory socketFactory) {
this.socketFactory = socketFactory; this.socketFactory = socketFactory;
return this; return this;
} }
@ -214,7 +213,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* @deprecated use {@link #usePlaintext()} or {@link #useTransportSecurity()} instead. * @deprecated use {@link #usePlaintext()} or {@link #useTransportSecurity()} instead.
*/ */
@Deprecated @Deprecated
public final OkHttpChannelBuilder negotiationType(io.grpc.okhttp.NegotiationType type) { public OkHttpChannelBuilder negotiationType(io.grpc.okhttp.NegotiationType type) {
Preconditions.checkNotNull(type, "type"); Preconditions.checkNotNull(type, "type");
switch (type) { switch (type) {
case TLS: case TLS:
@ -284,7 +283,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
/** /**
* Override the default {@link SSLSocketFactory} and enable TLS negotiation. * Override the default {@link SSLSocketFactory} and enable TLS negotiation.
*/ */
public final OkHttpChannelBuilder sslSocketFactory(SSLSocketFactory factory) { public OkHttpChannelBuilder sslSocketFactory(SSLSocketFactory factory) {
this.sslSocketFactory = factory; this.sslSocketFactory = factory;
negotiationType = NegotiationType.TLS; negotiationType = NegotiationType.TLS;
return this; return this;
@ -310,7 +309,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* @return this * @return this
* *
*/ */
public final OkHttpChannelBuilder hostnameVerifier(@Nullable HostnameVerifier hostnameVerifier) { public OkHttpChannelBuilder hostnameVerifier(@Nullable HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier; this.hostnameVerifier = hostnameVerifier;
return this; return this;
} }
@ -327,7 +326,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If {@code connectionSpec} is not with TLS * If {@code connectionSpec} is not with TLS
*/ */
public final OkHttpChannelBuilder connectionSpec( public OkHttpChannelBuilder connectionSpec(
com.squareup.okhttp.ConnectionSpec connectionSpec) { com.squareup.okhttp.ConnectionSpec connectionSpec) {
Preconditions.checkArgument(connectionSpec.isTls(), "plaintext ConnectionSpec is not accepted"); Preconditions.checkArgument(connectionSpec.isTls(), "plaintext ConnectionSpec is not accepted");
this.connectionSpec = Utils.convertSpec(connectionSpec); this.connectionSpec = Utils.convertSpec(connectionSpec);
@ -336,7 +335,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
/** Sets the negotiation type for the HTTP/2 connection to plaintext. */ /** Sets the negotiation type for the HTTP/2 connection to plaintext. */
@Override @Override
public final OkHttpChannelBuilder usePlaintext() { public OkHttpChannelBuilder usePlaintext() {
negotiationType = NegotiationType.PLAINTEXT; negotiationType = NegotiationType.PLAINTEXT;
return this; return this;
} }
@ -350,7 +349,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* socket factory used. * socket factory used.
*/ */
@Override @Override
public final OkHttpChannelBuilder useTransportSecurity() { public OkHttpChannelBuilder useTransportSecurity() {
negotiationType = NegotiationType.TLS; negotiationType = NegotiationType.TLS;
return this; return this;
} }
@ -365,7 +364,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* *
* @since 1.11.0 * @since 1.11.0
*/ */
public final OkHttpChannelBuilder scheduledExecutorService( public OkHttpChannelBuilder scheduledExecutorService(
ScheduledExecutorService scheduledExecutorService) { ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService = this.scheduledExecutorService =
checkNotNull(scheduledExecutorService, "scheduledExecutorService"); checkNotNull(scheduledExecutorService, "scheduledExecutorService");
@ -398,13 +397,13 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
* RESOURCE_EXHAUSTED. * RESOURCE_EXHAUSTED.
*/ */
@Override @Override
public final OkHttpChannelBuilder maxInboundMessageSize(int max) { public OkHttpChannelBuilder maxInboundMessageSize(int max) {
Preconditions.checkArgument(max >= 0, "negative max"); Preconditions.checkArgument(max >= 0, "negative max");
maxInboundMessageSize = max; maxInboundMessageSize = max;
return this; return this;
} }
final ClientTransportFactory buildTransportFactory() { ClientTransportFactory buildTransportFactory() {
boolean enableKeepAlive = keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED; boolean enableKeepAlive = keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED;
return new OkHttpTransportFactory( return new OkHttpTransportFactory(
transportExecutor, transportExecutor,
@ -424,17 +423,17 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
useGetForSafeMethods); useGetForSafeMethods);
} }
final OkHttpChannelBuilder disableCheckAuthority() { OkHttpChannelBuilder disableCheckAuthority() {
this.managedChannelImplBuilder.disableCheckAuthority(); this.managedChannelImplBuilder.disableCheckAuthority();
return this; return this;
} }
final OkHttpChannelBuilder enableCheckAuthority() { OkHttpChannelBuilder enableCheckAuthority() {
this.managedChannelImplBuilder.enableCheckAuthority(); this.managedChannelImplBuilder.enableCheckAuthority();
return this; return this;
} }
final int getDefaultPort() { int getDefaultPort() {
switch (negotiationType) { switch (negotiationType) {
case PLAINTEXT: case PLAINTEXT:
return GrpcUtil.DEFAULT_PORT_PLAINTEXT; return GrpcUtil.DEFAULT_PORT_PLAINTEXT;
@ -445,7 +444,7 @@ public class OkHttpChannelBuilder extends ForwardingChannelBuilder<OkHttpChannel
} }
} }
final void setStatsEnabled(boolean value) { void setStatsEnabled(boolean value) {
this.managedChannelImplBuilder.setStatsEnabled(value); this.managedChannelImplBuilder.setStatsEnabled(value);
} }

View File

@ -73,7 +73,7 @@ public class OkHttpChannelBuilderTest {
@Test @Test
public void failOverrideInvalidAuthority() { public void failOverrideInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234); OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("good", 1234);
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:"); thrown.expectMessage("Invalid authority:");
@ -82,14 +82,14 @@ public class OkHttpChannelBuilderTest {
@Test @Test
public void disableCheckAuthorityAllowsInvalidAuthority() { public void disableCheckAuthorityAllowsInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234) OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("good", 1234)
.disableCheckAuthority(); .disableCheckAuthority();
builder.overrideAuthority("[invalidauthority").usePlaintext().buildTransportFactory(); builder.overrideAuthority("[invalidauthority").usePlaintext().buildTransportFactory();
} }
@Test @Test
public void enableCheckAuthorityFailOverrideInvalidAuthority() { public void enableCheckAuthorityFailOverrideInvalidAuthority() {
OkHttpChannelBuilder builder = new OkHttpChannelBuilder("good", 1234) OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("good", 1234)
.disableCheckAuthority() .disableCheckAuthority()
.enableCheckAuthority(); .enableCheckAuthority();