Home / Function/ testReplaceHandler() — netty Function Reference

testReplaceHandler() — netty Function Reference

Architecture documentation for the testReplaceHandler() function in SniHandlerTest.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  43ec0116_7195_0a12_237d_6e2484e5347d["testReplaceHandler()"]
  00b45f27_cbf5_af92_430f_7f0426117fa5["SniHandlerTest"]
  43ec0116_7195_0a12_237d_6e2484e5347d -->|defined in| 00b45f27_cbf5_af92_430f_7f0426117fa5
  698f639c_6a11_d253_15c7_9dca0d17d480["handlerRemoved0()"]
  43ec0116_7195_0a12_237d_6e2484e5347d -->|calls| 698f639c_6a11_d253_15c7_9dca0d17d480
  b2c4304e_7e24_d36d_6cc5_2a994bce5582["CustomSslHandler()"]
  43ec0116_7195_0a12_237d_6e2484e5347d -->|calls| b2c4304e_7e24_d36d_6cc5_2a994bce5582
  style 43ec0116_7195_0a12_237d_6e2484e5347d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java lines 485–608

    @ParameterizedTest(name = "{index}: sslProvider={0}")
    @MethodSource("data")
    @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
    public void testReplaceHandler(SslProvider provider) throws Exception {
        switch (provider) {
            case OPENSSL:
            case OPENSSL_REFCNT:
                final String sniHost = "sni.netty.io";
                LocalAddress address = new LocalAddress("testReplaceHandler-" + Math.random());
                EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory());
                Channel sc = null;
                Channel cc = null;
                SslContext sslContext = null;

                SelfSignedCertificate cert = CachedSelfSignedCertificate.getCachedCertificate();

                try {
                    final SslContext sslServerContext = SslContextBuilder
                            .forServer(cert.key(), cert.cert())
                            .sslProvider(provider)
                            .build();

                    final Mapping<String, SslContext> mapping = new Mapping<String, SslContext>() {
                        @Override
                        public SslContext map(String input) {
                            return sslServerContext;
                        }
                    };

                    final Promise<Void> releasePromise = group.next().newPromise();

                    final SniHandler handler = new SniHandler(mapping) {
                        @Override
                        protected void replaceHandler(ChannelHandlerContext ctx,
                                                      String hostname, final SslContext sslContext)
                                throws Exception {

                            boolean success = false;
                            try {
                                assertEquals(1, ((ReferenceCountedOpenSslContext) sslContext).refCnt());
                                // The SniHandler's replaceHandler() method allows us to implement custom behavior.
                                // As an example, we want to release() the SslContext upon channelInactive() or rather
                                // when the SslHandler closes it's SslEngine. If you take a close look at SslHandler
                                // you'll see that it's doing it in the #handlerRemoved0() method.

                                SSLEngine sslEngine = sslContext.newEngine(ctx.alloc());
                                try {
                                    assertEquals(2, ((ReferenceCountedOpenSslContext) sslContext).refCnt());
                                    SslHandler customSslHandler = new CustomSslHandler(sslContext, sslEngine) {
                                        @Override
                                        public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
                                            try {
                                                super.handlerRemoved0(ctx);
                                            } finally {
                                                releasePromise.trySuccess(null);
                                            }
                                        }
                                    };
                                    ctx.pipeline().replace(this, CustomSslHandler.class.getName(), customSslHandler);
                                    success = true;
                                } finally {
                                    if (!success) {
                                        ReferenceCountUtil.safeRelease(sslEngine);
                                    }
                                }
                            } finally {
                                if (!success) {
                                    ReferenceCountUtil.safeRelease(sslContext);
                                    releasePromise.cancel(true);
                                }
                            }
                        }
                    };

                    ServerBootstrap sb = new ServerBootstrap();
                    sc = sb.group(group).channel(LocalServerChannel.class)
                            .childHandler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel ch) throws Exception {
                            ch.pipeline().addFirst(handler);
                        }

Domain

Subdomains

Frequently Asked Questions

What does testReplaceHandler() do?
testReplaceHandler() is a function in the netty codebase, defined in handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java.
Where is testReplaceHandler() defined?
testReplaceHandler() is defined in handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java at line 485.
What does testReplaceHandler() call?
testReplaceHandler() calls 2 function(s): CustomSslHandler, handlerRemoved0.

Analyze Your Own Codebase

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

Try Supermodel Free