use of org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method upgrade.
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
ServletContext servletContext = servletRequest.getServletContext();
Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
if (selectedProtocol != null) {
upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
}
return handlerAdapter;
};
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
try {
container.upgrade(webSocketCreator, servletRequest, servletResponse);
} catch (UndeclaredThrowableException ex) {
throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
} catch (Exception ex) {
throw new HandshakeFailureException("Failed to upgrade", ex);
}
}
use of org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.
the class JettyWebSocketClient method doHandshakeInternal.
@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
final ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols(protocols);
for (WebSocketExtension extension : extensions) {
request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(extension));
}
request.setHeaders(headers);
Principal user = getUser();
JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
Callable<WebSocketSession> connectTask = () -> {
JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
Future<Session> future = this.client.connect(adapter, uri, request);
future.get(this.client.getConnectTimeout() + 2000, TimeUnit.MILLISECONDS);
return wsSession;
};
if (this.taskExecutor != null) {
return this.taskExecutor.submitListenable(connectTask);
} else {
ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
task.run();
return task;
}
}
Aggregations