NettyChannelBuilder extends AbstractManagedChannelImplBuilder<NettyChannelBuilder> { @VisibleForTesting static ProtocolNegotiator createProtocolNegotiator( String authority, NegotiationType negotiationType, SslContext sslContext) { switch (negotiationType) { case PLAINTEXT: return ProtocolNegotiators.plaintext(); case PLAINTEXT_UPGRADE: return ProtocolNegotiators.plaintextUpgrade(); case TLS: if (sslContext == null) { try { sslContext = GrpcSslContexts.forClient().build(); } catch (SSLException ex) { throw new RuntimeException(ex); } } return ProtocolNegotiators.tls(sslContext, authority); default: throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType); } } protected  NettyChannelBuilder(String host, int port); protected  NettyChannelBuilder(String target); protected  NettyChannelBuilder(SocketAddress address); static NettyChannelBuilder forAddress(SocketAddress serverAddress); static NettyChannelBuilder forAddress(String host, int port); static NettyChannelBuilder forTarget(String target); final NettyChannelBuilder channelType(Class<? extends Channel> channelType); final NettyChannelBuilder negotiationType(NegotiationType type); @Internal final NettyChannelBuilder protocolNegotiator(
          @Nullable ProtocolNegotiator protocolNegotiator); final NettyChannelBuilder eventLoopGroup(@Nullable EventLoopGroup eventLoopGroup); final NettyChannelBuilder sslContext(SslContext sslContext); final NettyChannelBuilder flowControlWindow(int flowControlWindow); final NettyChannelBuilder maxMessageSize(int maxMessageSize); final NettyChannelBuilder maxHeaderListSize(int maxHeaderListSize); @Override final NettyChannelBuilder usePlaintext(boolean skipNegotiation); static final int DEFAULT_FLOW_CONTROL_WINDOW; }
@Test public void createProtocolNegotiator_plaintext() { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority", NegotiationType.PLAINTEXT, noSslContext); assertTrue(negotiator instanceof ProtocolNegotiators.PlaintextNegotiator); }
@Test public void createProtocolNegotiator_plaintextUpgrade() { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority", NegotiationType.PLAINTEXT_UPGRADE, noSslContext); assertTrue(negotiator instanceof ProtocolNegotiators.PlaintextUpgradeNegotiator); }
@Test public void createProtocolNegotiator_tlsWithNoContext() { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority:1234", NegotiationType.TLS, noSslContext); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; assertEquals("authority", n.getHost()); assertEquals(1234, n.getPort()); }
@Test public void createProtocolNegotiator_tlsWithClientContext() throws SSLException { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority:1234", NegotiationType.TLS, GrpcSslContexts.forClient().build()); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; assertEquals("authority", n.getHost()); assertEquals(1234, n.getPort()); }
@Test public void createProtocolNegotiator_tlsWithAuthorityFallback() { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "bad_authority", NegotiationType.TLS, noSslContext); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; assertEquals("bad_authority", n.getHost()); assertEquals(-1, n.getPort()); }