public HttpResponse doWs()

in core/src/main/java/hudson/cli/CLIAction.java [117:175]


    public HttpResponse doWs() {
        if (!WebSockets.isSupported()) {
            return HttpResponses.notFound();
        }
        Authentication authentication = Jenkins.getAuthentication();
        return WebSockets.upgrade(new WebSocketSession() {
            ServerSideImpl connection;
            class OutputImpl implements PlainCLIProtocol.Output {
                @Override
                public void send(byte[] data) throws IOException {
                    sendBinary(ByteBuffer.wrap(data));
                }
                @Override
                public void close() throws IOException {
                    doClose();
                }
            }
            private void doClose() {
                close();
            }
            @Override
            protected void opened() {
                try {
                    connection = new ServerSideImpl(new OutputImpl(), authentication);
                } catch (IOException x) {
                    error(x);
                    return;
                }
                new Thread(() -> {
                    try {
                        try {
                            connection.run();
                        } finally {
                            connection.close();
                        }
                    } catch (Exception x) {
                        error(x);
                    }
                }, "CLI handler for " + authentication.getName()).start();
            }
            @Override
            protected void binary(byte[] payload, int offset, int len) {
                try {
                    connection.handle(new DataInputStream(new ByteArrayInputStream(payload, offset, len)));
                } catch (IOException x) {
                    error(x);
                }
            }
            @Override
            protected void error(Throwable cause) {
                LOGGER.log(Level.WARNING, null, cause);
            }
            @Override
            protected void closed(int statusCode, String reason) {
                LOGGER.fine(() -> "closed: " + statusCode + ": " + reason);
                connection.handleClose();
            }
        });
    }