Search in sources :

Example 1 with RemotingService

use of org.apache.activemq.artemis.core.remoting.server.RemotingService in project wildfly by wildfly.

the class HTTPUpgradeService method start.

@Override
public void start(StartContext context) throws StartException {
    ListenerRegistry listenerRegistry = listenerRegistrySupplier.get();
    ListenerRegistry.Listener listenerInfo = listenerRegistry.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());
    upgradeSupplier.get().addProtocol(getProtocol(), httpUpgradeListener, new HttpUpgradeHandshake() {

        /**
         * 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 {
            String secretKey = exchange.getRequestHeaders().getFirst(getSecKeyHeader());
            if (secretKey == null) {
                throw MessagingLogger.ROOT_LOGGER.upgradeRequestMissingKey();
            }
            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();
            if (!server.isActive() || !remotingService.isStarted()) {
                return false;
            }
            final String endpoint = exchange.getRequestHeaders().getFirst(getHttpUpgradeEndpointKey());
            if (endpoint == null || endpoint.equals(acceptorName)) {
                String response = createExpectedResponse(MAGIC_NUMBER, secretKey);
                exchange.getResponseHeaders().put(HttpString.tryFromString(getSecAcceptHeader()), response);
                return true;
            }
            return false;
        }

        private String createExpectedResponse(final String magicNumber, final String secretKey) throws IOException {
            try {
                final String concat = secretKey + magicNumber;
                final MessageDigest digest = MessageDigest.getInstance("SHA1");
                digest.update(concat.getBytes(StandardCharsets.UTF_8));
                final byte[] bytes = digest.digest();
                return FlexBase64.encodeString(bytes, false);
            } catch (NoSuchAlgorithmException e) {
                throw new IOException(e);
            }
        }
    });
}
Also used : ListenerRegistry(io.undertow.server.ListenerRegistry) RemotingService(org.apache.activemq.artemis.core.remoting.server.RemotingService) IOException(java.io.IOException) HttpString(io.undertow.util.HttpString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpUpgradeHandshake(io.undertow.server.handlers.HttpUpgradeHandshake) HttpServerExchange(io.undertow.server.HttpServerExchange) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) MessageDigest(java.security.MessageDigest)

Example 2 with RemotingService

use of org.apache.activemq.artemis.core.remoting.server.RemotingService in project wildfly by wildfly.

the class HTTPUpgradeService method switchToMessagingProtocol.

private static HttpUpgradeListener switchToMessagingProtocol(final ActiveMQServer activemqServer, final String acceptorName, final String protocolName) {
    return new HttpUpgradeListener() {

        @Override
        public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
            ChannelListener<StreamConnection> listener = new ChannelListener<StreamConnection>() {

                @Override
                public void handleEvent(StreamConnection connection) {
                    MessagingLogger.ROOT_LOGGER.debugf("Switching to %s protocol for %s http-acceptor", protocolName, acceptorName);
                    ActiveMQServer server = selectServer(exchange, activemqServer);
                    if (server == null) {
                        IoUtils.safeClose(connection);
                        return;
                    }
                    RemotingService remotingService = server.getRemotingService();
                    if (!server.isActive() || !remotingService.isStarted()) {
                        // ActiveMQ does not accept connection
                        IoUtils.safeClose(connection);
                        return;
                    }
                    NettyAcceptor acceptor = (NettyAcceptor) remotingService.getAcceptor(acceptorName);
                    SocketChannel channel = new WrappingXnioSocketChannel(connection);
                    try {
                        acceptor.transfer(channel);
                        connection.getSourceChannel().resumeReads();
                    } catch (IllegalStateException e) {
                        IoUtils.safeClose(connection);
                    }
                }
            };
            ChannelListeners.invokeChannelListener(streamConnection, listener);
        }
    };
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) WrappingXnioSocketChannel(org.xnio.netty.transport.WrappingXnioSocketChannel) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) SocketChannel(io.netty.channel.socket.SocketChannel) WrappingXnioSocketChannel(org.xnio.netty.transport.WrappingXnioSocketChannel) ChannelListener(org.xnio.ChannelListener) RemotingService(org.apache.activemq.artemis.core.remoting.server.RemotingService) NettyAcceptor(org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)2 RemotingService (org.apache.activemq.artemis.core.remoting.server.RemotingService)2 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)2 SocketChannel (io.netty.channel.socket.SocketChannel)1 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)1 ListenerRegistry (io.undertow.server.ListenerRegistry)1 HttpUpgradeHandshake (io.undertow.server.handlers.HttpUpgradeHandshake)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NettyAcceptor (org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor)1 ChannelListener (org.xnio.ChannelListener)1 StreamConnection (org.xnio.StreamConnection)1 WrappingXnioSocketChannel (org.xnio.netty.transport.WrappingXnioSocketChannel)1