|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.socket.server.standard; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | +import javax.websocket.Endpoint; |
| 26 | +import javax.websocket.Extension; |
| 27 | +import javax.websocket.server.ServerContainer; |
| 28 | +import javax.websocket.server.ServerEndpointConfig; |
| 29 | + |
| 30 | +import org.springframework.http.server.ServerHttpRequest; |
| 31 | +import org.springframework.http.server.ServerHttpResponse; |
| 32 | +import org.springframework.web.socket.server.HandshakeFailureException; |
| 33 | + |
| 34 | +/** |
| 35 | + * WebSphere support for upgrading an {@link HttpServletRequest} during a |
| 36 | + * WebSocket handshake. To modify properties of the underlying |
| 37 | + * {@link javax.websocket.server.ServerContainer} you can use |
| 38 | + * {@link ServletServerContainerFactoryBean} in XML configuration or, when using |
| 39 | + * Java configuration, access the container instance through the |
| 40 | + * "javax.websocket.server.ServerContainer" ServletContext attribute. |
| 41 | + * |
| 42 | + * <p>Tested with WAS Liberty beta (August 2015) for the upcoming 8.5.5.7 release. |
| 43 | + * |
| 44 | + * @author Rossen Stoyanchev |
| 45 | + * @since 4.2.1 |
| 46 | + */ |
| 47 | +public class WebSphereRequestUpgradeStrategy extends AbstractStandardUpgradeStrategy { |
| 48 | + |
| 49 | + private final static Method upgradeMethod; |
| 50 | + |
| 51 | + static { |
| 52 | + ClassLoader loader = WebSphereRequestUpgradeStrategy.class.getClassLoader(); |
| 53 | + try { |
| 54 | + Class<?> type = loader.loadClass("com.ibm.websphere.wsoc.WsWsocServerContainer"); |
| 55 | + upgradeMethod = type.getMethod("doUpgrade", HttpServletRequest.class, |
| 56 | + HttpServletResponse.class, ServerEndpointConfig.class, Map.class); |
| 57 | + } |
| 58 | + catch (Exception ex) { |
| 59 | + throw new IllegalStateException("No compatible WebSphere version found", ex); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Override |
| 65 | + public String[] getSupportedVersions() { |
| 66 | + return new String[] {"13"}; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void upgradeInternal(ServerHttpRequest httpRequest, ServerHttpResponse httpResponse, |
| 71 | + String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) |
| 72 | + throws HandshakeFailureException { |
| 73 | + |
| 74 | + HttpServletRequest request = getHttpServletRequest(httpRequest); |
| 75 | + HttpServletResponse response = getHttpServletResponse(httpResponse); |
| 76 | + |
| 77 | + StringBuffer requestUrl = request.getRequestURL(); |
| 78 | + String path = request.getRequestURI(); // shouldn't matter |
| 79 | + Map<String, String> pathParams = Collections.<String, String> emptyMap(); |
| 80 | + |
| 81 | + ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint); |
| 82 | + endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol)); |
| 83 | + endpointConfig.setExtensions(selectedExtensions); |
| 84 | + |
| 85 | + try { |
| 86 | + ServerContainer container = getContainer(request); |
| 87 | + upgradeMethod.invoke(container, request, response, endpointConfig, pathParams); |
| 88 | + } |
| 89 | + catch (Exception ex) { |
| 90 | + throw new HandshakeFailureException( |
| 91 | + "Servlet request failed to upgrade to WebSocket, uri=" + requestUrl, ex); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments