class NettyServerInitializer extends ChannelInitializer<SocketChannel> {

    private static final Logger Log = LoggerFactory.getLogger(NettyServerInitializer.class);

    /**
     * Controls the write timeout time in seconds to handle stalled sessions and prevent DoS
     */
    public static final SystemProperty<Duration> WRITE_TIMEOUT_SECONDS = SystemProperty.Builder.ofType(Duration.class)
        .setKey("xmpp.socket.write-timeout-seconds")
        .setDefaultValue(Duration.ofSeconds(30))
        .setChronoUnit(ChronoUnit.SECONDS)
        .setDynamic(true)
        .build();

    public static final String TRAFFIC_HANDLER_NAME = "trafficShapingHandler";
    private final ChannelGroup allChannels; // This is a collection that is managed by the invoking entity.
    private final ConnectionConfiguration configuration;
    private final Set<NettyChannelHandlerFactory> channelHandlerFactories; // This is a collection that is managed by the invoking entity.

    public NettyServerInitializer(ConnectionConfiguration configuration, ChannelGroup allChannels, Set<NettyChannelHandlerFactory> channelHandlerFactories) {
        this.allChannels = allChannels;
        this.configuration = configuration;
        this.channelHandlerFactories = channelHandlerFactories;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {<FILL_FUNCTION_BODY>}

    private boolean isDirectTLSConfigured() {
        return this.configuration.getTlsPolicy() == Connection.TLSPolicy.directTLS;
    }
}


        boolean isClientConnection = configuration.getType() == ConnectionType.SOCKET_C2S;

        NettyConnectionHandler businessLogicHandler = NettyConnectionHandlerFactory.createConnectionHandler(configuration);
        Duration maxIdleTimeBeforeClosing = businessLogicHandler.getMaxIdleTime().isNegative() ? Duration.ZERO : businessLogicHandler.getMaxIdleTime();

        ch.pipeline()
            .addLast(TRAFFIC_HANDLER_NAME, new ChannelTrafficShapingHandler(0))
            .addLast("idleStateHandler", new IdleStateHandler(maxIdleTimeBeforeClosing.dividedBy(2).toMillis(), 0, 0, TimeUnit.MILLISECONDS))
            .addLast("keepAliveHandler", new NettyIdleStateKeepAliveHandler(isClientConnection))
            .addLast(new NettyXMPPDecoder())
            .addLast(new StringEncoder(StandardCharsets.UTF_8))
            .addLast("stalledSessionHandler", new WriteTimeoutHandler(Math.toIntExact(WRITE_TIMEOUT_SECONDS.getValue().getSeconds())))
            .addLast(businessLogicHandler);

        // Add ChannelHandler providers implemented by plugins, if any.
        channelHandlerFactories.forEach(factory -> {
            try {
                factory.addNewHandlerTo(ch.pipeline());
            } catch (Throwable t) {
                Log.warn("Unable to add ChannelHandler from '{}' to pipeline of new channel: {}", factory, ch, t);
            }
        });

        if (isDirectTLSConfigured()) {
            ch.attr(CONNECTION).get().startTLS(false, true);
        }

        allChannels.add(ch);
    
<no_super_class>