Home / Class/ OcspClientExample Class — netty Architecture

OcspClientExample Class — netty Architecture

Architecture documentation for the OcspClientExample class in OcspClientExample.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  b9432e46_513a_9655_4561_4a9c7c3a3848["OcspClientExample"]
  78bd9b38_0697_2858_7e74_e153357ddaa0["OcspClientExample.java"]
  b9432e46_513a_9655_4561_4a9c7c3a3848 -->|defined in| 78bd9b38_0697_2858_7e74_e153357ddaa0
  d4458b73_b004_4daf_0b0c_59a3db2c8882["main()"]
  b9432e46_513a_9655_4561_4a9c7c3a3848 -->|method| d4458b73_b004_4daf_0b0c_59a3db2c8882
  447e7dc1_343c_edfd_88f0_01d4df3f2d14["newClientHandler()"]
  b9432e46_513a_9655_4561_4a9c7c3a3848 -->|method| 447e7dc1_343c_edfd_88f0_01d4df3f2d14

Relationship Graph

Source Code

example/src/main/java/io/netty/example/ocsp/OcspClientExample.java lines 66–249

public class OcspClientExample {
    public static void main(String[] args) throws Exception {
        if (!OpenSsl.isAvailable()) {
            throw new IllegalStateException("OpenSSL is not available!");
        }

        if (!OpenSsl.isOcspSupported()) {
            throw new IllegalStateException("OCSP is not supported!");
        }

        // Using Wikipedia as an example. I'd rather use Netty's own website
        // but the server (Cloudflare) doesn't support OCSP stapling. A few
        // other examples could be Microsoft or Squarespace. Use OpenSSL's
        // CLI client to assess if a server supports OCSP stapling. E.g.:
        //
        // openssl s_client -tlsextdebug -status -connect www.squarespace.com:443
        //
        String host = "www.wikipedia.org";

        ReferenceCountedOpenSslContext context
            = (ReferenceCountedOpenSslContext) SslContextBuilder.forClient()
                .sslProvider(SslProvider.OPENSSL)
                .enableOcsp(true)
                .build();

        try {
            EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
            try {
                Promise<FullHttpResponse> promise = group.next().newPromise();

                Bootstrap bootstrap = new Bootstrap()
                        .channel(NioSocketChannel.class)
                        .group(group)
                        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5 * 1000)
                        .handler(newClientHandler(context, host, promise));

                Channel channel = bootstrap.connect(host, 443)
                        .syncUninterruptibly()
                        .channel();

                try {
                    FullHttpResponse response = promise.get();
                    ReferenceCountUtil.release(response);
                } finally {
                    channel.close();
                }
            } finally {
                group.shutdownGracefully();
            }
        } finally {
            context.release();
        }
    }

    private static ChannelInitializer<Channel> newClientHandler(final ReferenceCountedOpenSslContext context,
            final String host, final Promise<FullHttpResponse> promise) {

        return new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                SslHandler sslHandler = context.newHandler(ch.alloc());
                ReferenceCountedOpenSslEngine engine
                    = (ReferenceCountedOpenSslEngine) sslHandler.engine();

                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(sslHandler);
                pipeline.addLast(new ExampleOcspClientHandler(engine));

                pipeline.addLast(new HttpClientCodec());
                pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
                pipeline.addLast(new HttpClientHandler(host, promise));
            }

            @Override
            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                if (!promise.isDone()) {
                    promise.tryFailure(new IllegalStateException("Connection closed and Promise was not done."));
                }
                ctx.fireChannelInactive();
            }

Frequently Asked Questions

What is the OcspClientExample class?
OcspClientExample is a class in the netty codebase, defined in example/src/main/java/io/netty/example/ocsp/OcspClientExample.java.
Where is OcspClientExample defined?
OcspClientExample is defined in example/src/main/java/io/netty/example/ocsp/OcspClientExample.java at line 66.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free