001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.socket.server.standard; 018 019import java.io.IOException; 020import java.util.Collections; 021import java.util.List; 022import java.util.Map; 023 024import javax.servlet.ServletException; 025import javax.servlet.http.HttpServletRequest; 026import javax.servlet.http.HttpServletResponse; 027import javax.websocket.Endpoint; 028import javax.websocket.Extension; 029 030import io.undertow.websockets.core.WebSocketVersion; 031import io.undertow.websockets.jsr.ServerWebSocketContainer; 032 033import org.springframework.http.server.ServerHttpRequest; 034import org.springframework.http.server.ServerHttpResponse; 035import org.springframework.lang.Nullable; 036import org.springframework.web.socket.server.HandshakeFailureException; 037 038/** 039 * A WebSocket {@code RequestUpgradeStrategy} for WildFly and its underlying 040 * Undertow web server. Also compatible with embedded Undertow usage. 041 * 042 * <p>Requires Undertow 1.3.5+ as of Spring Framework 5.0. 043 * 044 * @author Rossen Stoyanchev 045 * @since 4.0.1 046 */ 047public class UndertowRequestUpgradeStrategy extends AbstractStandardUpgradeStrategy { 048 049 private static final String[] VERSIONS = new String[] { 050 WebSocketVersion.V13.toHttpHeaderValue(), 051 WebSocketVersion.V08.toHttpHeaderValue(), 052 WebSocketVersion.V07.toHttpHeaderValue() 053 }; 054 055 056 @Override 057 public String[] getSupportedVersions() { 058 return VERSIONS; 059 } 060 061 @Override 062 protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, 063 @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) 064 throws HandshakeFailureException { 065 066 HttpServletRequest servletRequest = getHttpServletRequest(request); 067 HttpServletResponse servletResponse = getHttpServletResponse(response); 068 069 StringBuffer requestUrl = servletRequest.getRequestURL(); 070 String path = servletRequest.getRequestURI(); // shouldn't matter 071 Map<String, String> pathParams = Collections.emptyMap(); 072 073 ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint); 074 endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol)); 075 endpointConfig.setExtensions(selectedExtensions); 076 077 try { 078 getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams); 079 } 080 catch (ServletException ex) { 081 throw new HandshakeFailureException( 082 "Servlet request failed to upgrade to WebSocket: " + requestUrl, ex); 083 } 084 catch (IOException ex) { 085 throw new HandshakeFailureException( 086 "Response update failed during upgrade to WebSocket: " + requestUrl, ex); 087 } 088 } 089 090 @Override 091 public ServerWebSocketContainer getContainer(HttpServletRequest request) { 092 return (ServerWebSocketContainer) super.getContainer(request); 093 } 094 095}