use of org.springframework.web.socket.server.HandshakeFailureException 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.server.HandshakeFailureException in project spring-framework by spring-projects.
the class AbstractTyrusRequestUpgradeStrategy method createEndpoint.
private Object createEndpoint(ServerEndpointRegistration registration, ComponentProviderService provider, WebSocketContainer container, TyrusWebSocketEngine engine) throws DeploymentException {
DirectFieldAccessor accessor = new DirectFieldAccessor(engine);
Object sessionListener = accessor.getPropertyValue("sessionListener");
Object clusterContext = accessor.getPropertyValue("clusterContext");
try {
if (constructorWithBooleanArgument) {
// Tyrus 1.11+
return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null, Boolean.TRUE);
} else {
return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null);
}
} catch (Exception ex) {
throw new HandshakeFailureException("Failed to register " + registration, ex);
}
}
use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.
the class UndertowRequestUpgradeStrategy method upgradeInternal.
@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
HttpServletRequest servletRequest = getHttpServletRequest(request);
HttpServletResponse servletResponse = getHttpServletResponse(response);
StringBuffer requestUrl = servletRequest.getRequestURL();
// shouldn't matter
String path = servletRequest.getRequestURI();
Map<String, String> pathParams = Collections.emptyMap();
ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
endpointConfig.setExtensions(selectedExtensions);
try {
getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
} catch (ServletException ex) {
throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
} catch (IOException ex) {
throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + requestUrl, ex);
}
}
use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.
the class TransportHandlingSockJsService method handleRawWebSocketRequest.
@Override
protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler) throws IOException {
TransportHandler transportHandler = this.handlers.get(TransportType.WEBSOCKET);
if (!(transportHandler instanceof HandshakeHandler)) {
logger.error("No handler configured for raw WebSocket messages");
response.setStatusCode(HttpStatus.NOT_FOUND);
return;
}
HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
HandshakeFailureException failure = null;
try {
Map<String, Object> attributes = new HashMap<>();
if (!chain.applyBeforeHandshake(request, response, attributes)) {
return;
}
((HandshakeHandler) transportHandler).doHandshake(request, response, handler, attributes);
chain.applyAfterHandshake(request, response, null);
} catch (HandshakeFailureException ex) {
failure = ex;
} catch (Exception ex) {
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
} finally {
if (failure != null) {
chain.applyAfterHandshake(request, response, failure);
throw failure;
}
}
}
use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.
the class AbstractTyrusRequestUpgradeStrategy method upgradeInternal.
@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException {
HttpServletRequest servletRequest = getHttpServletRequest(request);
HttpServletResponse servletResponse = getHttpServletResponse(response);
TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest);
TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine();
Object tyrusEndpoint = null;
boolean success;
try {
// Shouldn't matter for processing but must be unique
String path = "/" + random.nextLong();
tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine);
register(engine, tyrusEndpoint);
HttpHeaders headers = request.getHeaders();
RequestContext requestContext = createRequestContext(servletRequest, path, headers);
TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse();
UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse);
success = SUCCESS.equals(upgradeInfo.getStatus());
if (success) {
if (logger.isTraceEnabled()) {
logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders());
}
handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse);
}
} catch (Exception ex) {
unregisterTyrusEndpoint(engine, tyrusEndpoint);
throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex);
}
unregisterTyrusEndpoint(engine, tyrusEndpoint);
if (!success) {
throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI());
}
}
Aggregations