anand patel
anand patel

Reputation: 25

Where did the Connection object in the Connectors in Jetty 9.x go?

When I am trying to migrate to Jetty 9.4.x from Jetty 8.1.12, I am getting errors because of following issue.

We use connector.getConnection() method. And I did not find any replacement in Jetty 9.4.x.

This is followup question on Jetty upgrade 8 to 9

We use connector.getConnection() to get inet socket address.

    private InetSocketAddress findFirstInetConnector(Server server) {
        Connector[] connectors = server.getConnectors();
        if (connectors != null) {
            for (Connector connector : connectors) {
                Object connection = connector.getConnection();
                if (connection instanceof ServerSocketChannel) {
                    SocketAddress address = ((ServerSocketChannel) connector.getConnection()).socket().getLocalSocketAddress();
                    if (address instanceof InetSocketAddress) {
                        return (InetSocketAddress) address;
                    }
                } else if (connection instanceof ServerSocket) {
                    SocketAddress address = ((ServerSocket) connector.getConnection()).getLocalSocketAddress();
                    if (address instanceof InetSocketAddress) {
                        return (InetSocketAddress) address;
                    }
                }
            }
        }
        return null;
    }

Can anyone help me find replacement for the same.

Upvotes: 1

Views: 313

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49525

A Connector has no Connection anymore.

You cannot just arbitrarily get a SocketAddress or InetSocketAddress out of it anymore.

Why?

Well, we have logical connectors, physical connectors, non-network connectors, network connectors, server connectors, local connectors, pipe based connectors, jni based connectors, unixsocket based connectors, etc...

You'll have to create the InetSocketAddress you want from the information on a started and running connector (such as the NetworkConnector localPort and host).

To walk the connector information you can ...

for (Connector connector : server.getConnectors())
{
    // What is the connector name (can be null or unset)
    String connectorName = connector.getName();

    // What is the declared protocol default for this connector?
    String defaultProtocol = connector.getDefaultConnectionFactory().getProtocol();
    // What other features does this connector handle?
    for (ConnectionFactory connectionFactory : connector.getConnectionFactories())
    {
        // List of protocols handled by this specific connection factory for this specific connector
        connectionFactory.getProtocols();

        if (connectionFactory instanceof SslConnectionFactory)
        {
            // this can handle TLS/SSL based connections
        }

        if (connectionFactory instanceof HttpConnectionFactory)
        {
            // this can handle http protocols

            // get the http specific configuration
            HttpConfiguration httpConfig = ((HttpConnectionFactory) connectionFactory).getHttpConfiguration();
            // what port is recognized as secure
            httpConfig.getSecurePort();
            // what scheme is recognized as secure
            httpConfig.getSecureScheme();
        }

        if (connectionFactory instanceof HTTP2ServerConnectionFactory)
        {
            // can handle encrypted http/2 protocols (and alpn features)
        }

        if (connectionFactory instanceof HTTP2CServerConnectionFactory)
        {
            // this can handle http/2's special clear-text "h2c" protocol (no alpn features)
        }
    }

    if (!connector.isRunning())
    {
        // no information below can be trusted unless the connector is started
        continue;
    }

    if (connector instanceof NetworkConnector)
    {
        // we have a network capable connector
        NetworkConnector networkConnector = (NetworkConnector) connector;
        // What interface is it listening on?
        String interfaceName = networkConnector.getHost();
        // What local port is it bound to?
        int localPort = networkConnector.getLocalPort();
    }
}

Upvotes: 1

Related Questions