Home / Class/ AutobahnServerHandler Class — netty Architecture

AutobahnServerHandler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  ae745e27_4df1_b3ea_fa42_b6885b919f66["AutobahnServerHandler"]
  09279f01_66ef_8d62_413b_208067a3bcdb["AutobahnServerHandler.java"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|defined in| 09279f01_66ef_8d62_413b_208067a3bcdb
  b028e31f_a8e1_153c_dee2_72c016dcbcdb["channelRead()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| b028e31f_a8e1_153c_dee2_72c016dcbcdb
  12154d1b_b4cb_55e1_5cfe_9140493dfb19["channelReadComplete()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| 12154d1b_b4cb_55e1_5cfe_9140493dfb19
  9582854a_5889_5658_7efd_36d66fcb998f["handleHttpRequest()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| 9582854a_5889_5658_7efd_36d66fcb998f
  639f9ea2_204f_c050_5473_0a33f951e3dc["handleWebSocketFrame()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| 639f9ea2_204f_c050_5473_0a33f951e3dc
  ae872491_ad06_d7be_5add_fde8a3b97c5f["sendHttpResponse()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| ae872491_ad06_d7be_5add_fde8a3b97c5f
  cb2f107e_6485_844f_227e_eed8602f1712["exceptionCaught()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| cb2f107e_6485_844f_227e_eed8602f1712
  9d107630_428d_f1e8_0eff_87ab20ba11cd["String()"]
  ae745e27_4df1_b3ea_fa42_b6885b919f66 -->|method| 9d107630_428d_f1e8_0eff_87ab20ba11cd

Relationship Graph

Source Code

testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java lines 51–145

public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
    private static final Logger logger = Logger.getLogger(AutobahnServerHandler.class.getName());

    private WebSocketServerHandshaker handshaker;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof HttpRequest) {
            handleHttpRequest(ctx, (HttpRequest) msg);
        } else if (msg instanceof WebSocketFrame) {
            handleWebSocketFrame(ctx, (WebSocketFrame) msg);
        } else {
            throw new IllegalStateException("unknown message: " + msg);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req)
            throws Exception {
        // Handle a bad request.
        if (!req.decoderResult().isSuccess()) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, ctx.alloc().buffer(0)));
            return;
        }

        // Allow only GET methods.
        if (!GET.equals(req.method())) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN, ctx.alloc().buffer(0)));
            return;
        }

        // Handshake
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                getWebSocketLocation(req), null, false, Integer.MAX_VALUE);
        handshaker = wsFactory.newHandshaker(req);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), req);
        }
    }

    private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(String.format(
                    "Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame)));
        }

        if (frame instanceof CloseWebSocketFrame) {
            handshaker.close(ctx, (CloseWebSocketFrame) frame);
        } else if (frame instanceof PingWebSocketFrame) {
            ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
        } else if (frame instanceof TextWebSocketFrame ||
                frame instanceof BinaryWebSocketFrame ||
                frame instanceof ContinuationWebSocketFrame) {
            ctx.write(frame);
        } else if (frame instanceof PongWebSocketFrame) {
            frame.release();
            // Ignore
        } else {
            throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
                    .getName()));
        }
    }

    private static void sendHttpResponse(
            ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) {
        // Generate an error page if response status code is not OK (200).
        if (res.status().code() != 200) {
            ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
            res.content().writeBytes(buf);
            buf.release();
            setContentLength(res, res.content().readableBytes());
        }

        // Send the response and close the connection if necessary.
        ChannelFuture f = ctx.channel().writeAndFlush(res);

Frequently Asked Questions

What is the AutobahnServerHandler class?
AutobahnServerHandler is a class in the netty codebase, defined in testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java.
Where is AutobahnServerHandler defined?
AutobahnServerHandler is defined in testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java at line 51.

Analyze Your Own Codebase

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

Try Supermodel Free