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);
}
}
Aggregations