Search in sources :

Example 1 with SimpleHttpUpgradeHandshake

use of org.jboss.as.remoting.SimpleHttpUpgradeHandshake in project wildfly by wildfly.

the class HTTPUpgradeService method start.

@Override
public void start(StartContext context) throws StartException {
    ListenerRegistry.Listener listenerInfo = listenerRegistry.getValue().getListener(httpListenerName);
    assert listenerInfo != null;
    httpUpgradeMetadata = new ListenerRegistry.HttpUpgradeMetadata(getProtocol(), CORE);
    listenerInfo.addHttpUpgradeMetadata(httpUpgradeMetadata);
    MessagingLogger.ROOT_LOGGER.registeredHTTPUpgradeHandler(ACTIVEMQ_REMOTING, acceptorName);
    ServiceController<?> activeMQService = context.getController().getServiceContainer().getService(MessagingServices.getActiveMQServiceName(activeMQServerName));
    ActiveMQServer activeMQServer = ActiveMQServer.class.cast(activeMQService.getValue());
    httpUpgradeListener = switchToMessagingProtocol(activeMQServer, acceptorName, getProtocol());
    injectedRegistry.getValue().addProtocol(getProtocol(), httpUpgradeListener, new SimpleHttpUpgradeHandshake(MAGIC_NUMBER, getSecKeyHeader(), getSecAcceptHeader()) {

        /**
                     * override the default upgrade handshake to take into account the {@code TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME} header
                     * to select the correct acceptors among all that are configured in ActiveMQ.
                     *
                     * If the request does not have this header, the first acceptor will be used.
                     */
        @Override
        public boolean handleUpgrade(HttpServerExchange exchange) throws IOException {
            if (super.handleUpgrade(exchange)) {
                ActiveMQServer server = selectServer(exchange, activeMQServer);
                if (server == null) {
                    return false;
                }
                // If ActiveMQ remoting service is stopped (eg during shutdown), refuse
                // the handshake so that the ActiveMQ client will detect the connection has failed
                RemotingService remotingService = server.getRemotingService();
                final String endpoint = exchange.getRequestHeaders().getFirst(getHttpUpgradeEndpointKey());
                if (!server.isActive() || !remotingService.isStarted()) {
                    return false;
                }
                if (endpoint == null) {
                    return true;
                } else {
                    return acceptorName.equals(endpoint);
                }
            } else {
                return false;
            }
        }
    });
}
Also used : ListenerRegistry(io.undertow.server.ListenerRegistry) HttpServerExchange(io.undertow.server.HttpServerExchange) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) SimpleHttpUpgradeHandshake(org.jboss.as.remoting.SimpleHttpUpgradeHandshake) RemotingService(org.apache.activemq.artemis.core.remoting.server.RemotingService) IOException(java.io.IOException)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)1 ListenerRegistry (io.undertow.server.ListenerRegistry)1 IOException (java.io.IOException)1 RemotingService (org.apache.activemq.artemis.core.remoting.server.RemotingService)1 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)1 SimpleHttpUpgradeHandshake (org.jboss.as.remoting.SimpleHttpUpgradeHandshake)1