Search in sources :

Example 16 with WebSocketServletFactory

use of org.eclipse.jetty.websocket.servlet.WebSocketServletFactory in project qpid-broker-j by apache.

the class WebSocketProvider method start.

@Override
public void start() {
    _idleTimeoutChecker.start();
    _server = new Server(new QBBTrackingThreadPool());
    final ServerConnector connector;
    HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory();
    httpConnectionFactory.getHttpConfiguration().setSendServerVersion(false);
    httpConnectionFactory.getHttpConfiguration().setSendXPoweredBy(false);
    if (_transport == Transport.WS) {
        connector = new ServerConnector(_server, httpConnectionFactory);
    } else if (_transport == Transport.WSS) {
        connector = new ServerConnector(_server, _sslContextFactory, httpConnectionFactory);
        connector.addBean(new SslHandshakeListener() {

            @Override
            public void handshakeFailed(final Event event, final Throwable failure) {
                SSLEngine sslEngine = event.getSSLEngine();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.info("TLS handshake failed: host='{}', port={}", sslEngine.getPeerHost(), sslEngine.getPeerPort(), failure);
                } else {
                    LOGGER.info("TLS handshake failed: host='{}', port={}: {}", sslEngine.getPeerHost(), sslEngine.getPeerPort(), String.valueOf(failure));
                }
            }
        });
    } else {
        throw new IllegalArgumentException("Unexpected transport on port " + _port.getName() + ":" + _transport);
    }
    String bindingAddress = _port.getBindingAddress();
    if (bindingAddress != null && !bindingAddress.trim().equals("") && !bindingAddress.trim().equals("*")) {
        connector.setHost(bindingAddress.trim());
    }
    connector.setPort(_port.getPort());
    _server.addConnector(connector);
    WebSocketHandler wshandler = new WebSocketHandler() {

        @Override
        public void configure(final WebSocketServletFactory factory) {
            factory.setCreator((req, resp) -> {
                resp.setAcceptedSubProtocol(AMQP_WEBSOCKET_SUBPROTOCOL);
                return new AmqpWebSocket();
            });
        }

        @Override
        public void configurePolicy(final WebSocketPolicy policy) {
            super.configurePolicy(policy);
            // See https://github.com/eclipse/jetty.project/issues/488
            try {
                Field maxBinaryMessageSize = policy.getClass().getDeclaredField("maxBinaryMessageSize");
                maxBinaryMessageSize.setAccessible(true);
                maxBinaryMessageSize.set(policy, 0);
            } catch (IllegalAccessException | NoSuchFieldException e) {
                LOGGER.warn("Could not override maxBinaryMessageSize", e);
            }
        }
    };
    _server.setHandler(wshandler);
    wshandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(final String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
            if (response.isCommitted() || baseRequest.isHandled()) {
                return;
            }
            baseRequest.setHandled(true);
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        }
    });
    try {
        _server.start();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new ServerScopedRuntimeException(e);
    }
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) SslHandshakeListener(org.eclipse.jetty.io.ssl.SslHandshakeListener) SSLEngine(javax.net.ssl.SSLEngine) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Field(java.lang.reflect.Field) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) IOException(java.io.IOException) WebSocketHandler(org.eclipse.jetty.websocket.server.WebSocketHandler)

Aggregations

WebSocketServletFactory (org.eclipse.jetty.websocket.servlet.WebSocketServletFactory)16 Server (org.eclipse.jetty.server.Server)7 WebSocketHandler (org.eclipse.jetty.websocket.server.WebSocketHandler)6 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)5 IOException (java.io.IOException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Consumer (org.apache.camel.Consumer)3 ConnectorRef (org.apache.camel.component.websocket.WebsocketComponent.ConnectorRef)3 WebSocketServlet (org.eclipse.jetty.websocket.servlet.WebSocketServlet)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Request (org.eclipse.jetty.server.Request)2 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)2 Test (org.junit.Test)2 InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)1 BuckEventExternalInterface (com.facebook.buck.event.external.events.BuckEventExternalInterface)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1