Search in sources :

Example 1 with OkHttpChannelBuilder

use of io.grpc.okhttp.OkHttpChannelBuilder in project grpc-java by grpc.

the class Utils method newOkhttpClientChannel.

private static OkHttpChannelBuilder newOkhttpClientChannel(SocketAddress address, boolean tls, boolean testca, @Nullable String authorityOverride) {
    InetSocketAddress addr = (InetSocketAddress) address;
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress(addr.getHostName(), addr.getPort());
    if (tls) {
        builder.negotiationType(io.grpc.okhttp.NegotiationType.TLS);
        SSLSocketFactory factory;
        if (testca) {
            builder.overrideAuthority(GrpcUtil.authorityFromHostAndPort(authorityOverride, addr.getPort()));
            try {
                factory = TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        }
        builder.sslSocketFactory(factory);
    } else {
        builder.negotiationType(io.grpc.okhttp.NegotiationType.PLAINTEXT);
    }
    return builder;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) IOException(java.io.IOException) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Example 2 with OkHttpChannelBuilder

use of io.grpc.okhttp.OkHttpChannelBuilder in project grpc-java by grpc.

the class TesterOkHttpChannelBuilder method build.

public static ManagedChannel build(String host, int port, @Nullable String serverHostOverride, boolean useTls, @Nullable InputStream testCa) {
    ChannelCredentials credentials;
    if (useTls) {
        if (testCa == null) {
            credentials = TlsChannelCredentials.create();
        } else {
            try {
                credentials = TlsChannelCredentials.newBuilder().trustManager(testCa).build();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        credentials = InsecureChannelCredentials.create();
    }
    ManagedChannelBuilder<?> channelBuilder = Grpc.newChannelBuilderForAddress(host, port, credentials).maxInboundMessageSize(16 * 1024 * 1024);
    if (!(channelBuilder instanceof OkHttpChannelBuilder)) {
        throw new RuntimeException("Did not receive an OkHttpChannelBuilder");
    }
    if (serverHostOverride != null) {
        // Force the hostname to match the cert the server uses.
        channelBuilder.overrideAuthority(serverHostOverride);
    }
    return channelBuilder.build();
}
Also used : InsecureChannelCredentials(io.grpc.InsecureChannelCredentials) TlsChannelCredentials(io.grpc.TlsChannelCredentials) ChannelCredentials(io.grpc.ChannelCredentials) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Example 3 with OkHttpChannelBuilder

use of io.grpc.okhttp.OkHttpChannelBuilder in project grpc-java by grpc.

the class Http2OkHttpTest method createChannelBuilderPreCredentialsApi.

private OkHttpChannelBuilder createChannelBuilderPreCredentialsApi() {
    int port = ((InetSocketAddress) getListenAddress()).getPort();
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("localhost", port).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).connectionSpec(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).build()).overrideAuthority(GrpcUtil.authorityFromHostAndPort(TestUtils.TEST_SERVER_HOST, port));
    try {
        builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(), TestUtils.loadCert("ca.pem")));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // Disable the default census stats interceptor, use testing interceptor instead.
    InternalOkHttpChannelBuilder.setStatsEnabled(builder, false);
    return builder.intercept(createCensusStatsClientInterceptor());
}
Also used : ConnectionSpec(com.squareup.okhttp.ConnectionSpec) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) InternalOkHttpChannelBuilder(io.grpc.okhttp.InternalOkHttpChannelBuilder) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Example 4 with OkHttpChannelBuilder

use of io.grpc.okhttp.OkHttpChannelBuilder in project grpc-java by grpc.

the class Http2OkHttpTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("127.0.0.1", getPort()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).connectionSpec(new ConnectionSpec.Builder(OkHttpChannelBuilder.DEFAULT_CONNECTION_SPEC).cipherSuites(TestUtils.preferredTestCiphers().toArray(new String[0])).tlsVersions(ConnectionSpec.MODERN_TLS.tlsVersions().toArray(new TlsVersion[0])).build()).overrideAuthority(GrpcUtil.authorityFromHostAndPort(TestUtils.TEST_SERVER_HOST, getPort()));
    io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
    try {
        builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(), TestUtils.loadCert("ca.pem")));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return builder.build();
}
Also used : ConnectionSpec(com.squareup.okhttp.ConnectionSpec) IOException(java.io.IOException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Example 5 with OkHttpChannelBuilder

use of io.grpc.okhttp.OkHttpChannelBuilder in project grpc-java by grpc.

the class Http2OkHttpTest method createChannelBuilder.

@Override
protected OkHttpChannelBuilder createChannelBuilder() {
    int port = ((InetSocketAddress) getListenAddress()).getPort();
    ChannelCredentials channelCreds;
    try {
        channelCreds = TlsChannelCredentials.newBuilder().trustManager(TestUtils.loadCert("ca.pem")).build();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("localhost", port, channelCreds).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).overrideAuthority(GrpcUtil.authorityFromHostAndPort(TestUtils.TEST_SERVER_HOST, port));
    // Disable the default census stats interceptor, use testing interceptor instead.
    InternalOkHttpChannelBuilder.setStatsEnabled(builder, false);
    return builder.intercept(createCensusStatsClientInterceptor());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ChannelCredentials(io.grpc.ChannelCredentials) TlsChannelCredentials(io.grpc.TlsChannelCredentials) IOException(java.io.IOException) InternalOkHttpChannelBuilder(io.grpc.okhttp.InternalOkHttpChannelBuilder) OkHttpChannelBuilder(io.grpc.okhttp.OkHttpChannelBuilder)

Aggregations

OkHttpChannelBuilder (io.grpc.okhttp.OkHttpChannelBuilder)6 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)3 ConnectionSpec (com.squareup.okhttp.ConnectionSpec)2 ChannelCredentials (io.grpc.ChannelCredentials)2 TlsChannelCredentials (io.grpc.TlsChannelCredentials)2 InternalOkHttpChannelBuilder (io.grpc.okhttp.InternalOkHttpChannelBuilder)2 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)2 InsecureChannelCredentials (io.grpc.InsecureChannelCredentials)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Test (org.junit.Test)1