|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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.support; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.net.URI; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import javax.servlet.ServletException; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | +import javax.websocket.DeploymentException; |
| 29 | +import javax.websocket.Endpoint; |
| 30 | + |
| 31 | +import org.glassfish.tyrus.core.ComponentProviderService; |
| 32 | +import org.glassfish.tyrus.core.EndpointWrapper; |
| 33 | +import org.glassfish.tyrus.core.ErrorCollector; |
| 34 | +import org.glassfish.tyrus.core.RequestContext; |
| 35 | +import org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler; |
| 36 | +import org.glassfish.tyrus.websockets.Connection; |
| 37 | +import org.glassfish.tyrus.websockets.Version; |
| 38 | +import org.glassfish.tyrus.websockets.WebSocketApplication; |
| 39 | +import org.glassfish.tyrus.websockets.WebSocketEngine; |
| 40 | +import org.glassfish.tyrus.websockets.WebSocketEngine.WebSocketHolderListener; |
| 41 | +import org.springframework.http.HttpHeaders; |
| 42 | +import org.springframework.http.server.ServerHttpRequest; |
| 43 | +import org.springframework.http.server.ServerHttpResponse; |
| 44 | +import org.springframework.http.server.ServletServerHttpRequest; |
| 45 | +import org.springframework.http.server.ServletServerHttpResponse; |
| 46 | +import org.springframework.util.Assert; |
| 47 | +import org.springframework.util.ClassUtils; |
| 48 | +import org.springframework.util.ReflectionUtils; |
| 49 | +import org.springframework.util.StringUtils; |
| 50 | +import org.springframework.web.socket.server.HandshakeFailureException; |
| 51 | +import org.springframework.web.socket.server.endpoint.ServerEndpointRegistration; |
| 52 | +import org.springframework.web.socket.server.endpoint.ServletServerContainerFactoryBean; |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * GlassFish support for upgrading a request during a WebSocket handshake. To modify |
| 57 | + * properties of the underlying {@link javax.websocket.server.ServerContainer} you can use |
| 58 | + * {@link ServletServerContainerFactoryBean} in XML configuration or if using Java |
| 59 | + * configuration, access the container instance through the |
| 60 | + * "javax.websocket.server.ServerContainer" ServletContext attribute. |
| 61 | + * |
| 62 | + * @author Rossen Stoyanchev |
| 63 | + * @since 4.0 |
| 64 | + */ |
| 65 | +public abstract class AbstractGlassFishRequestUpgradeStrategy extends AbstractStandardUpgradeStrategy { |
| 66 | + |
| 67 | + private final static Random random = new Random(); |
| 68 | + |
| 69 | + @Override |
| 70 | + public String[] getSupportedVersions() { |
| 71 | + return StringUtils.commaDelimitedListToStringArray(Version.getSupportedWireProtocolVersions()); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, |
| 76 | + String selectedProtocol, Endpoint endpoint) throws HandshakeFailureException { |
| 77 | + |
| 78 | + Assert.isTrue(request instanceof ServletServerHttpRequest); |
| 79 | + HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); |
| 80 | + |
| 81 | + Assert.isTrue(response instanceof ServletServerHttpResponse); |
| 82 | + HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse(); |
| 83 | + |
| 84 | + WebSocketApplication webSocketApplication = createTyrusEndpoint(servletRequest, endpoint, selectedProtocol); |
| 85 | + WebSocketEngine webSocketEngine = WebSocketEngine.getEngine(); |
| 86 | + |
| 87 | + try { |
| 88 | + webSocketEngine.register(webSocketApplication); |
| 89 | + } |
| 90 | + catch (DeploymentException ex) { |
| 91 | + throw new HandshakeFailureException("Failed to configure endpoint in GlassFish", ex); |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + performUpgrade(servletRequest, servletResponse, request.getHeaders(), webSocketApplication); |
| 96 | + } |
| 97 | + catch (IOException ex) { |
| 98 | + throw new HandshakeFailureException( |
| 99 | + "Response update failed during upgrade to WebSocket, uri=" + request.getURI(), ex); |
| 100 | + } |
| 101 | + finally { |
| 102 | + webSocketEngine.unregister(webSocketApplication); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private boolean performUpgrade(HttpServletRequest request, HttpServletResponse response, |
| 107 | + HttpHeaders headers, WebSocketApplication wsApp) throws IOException { |
| 108 | + |
| 109 | + final TyrusHttpUpgradeHandler upgradeHandler; |
| 110 | + try { |
| 111 | + upgradeHandler = request.upgrade(TyrusHttpUpgradeHandler.class); |
| 112 | + } |
| 113 | + catch (ServletException e) { |
| 114 | + throw new HandshakeFailureException("Unable to create UpgardeHandler", e); |
| 115 | + } |
| 116 | + |
| 117 | + Connection connection = createConnection(upgradeHandler, response); |
| 118 | + |
| 119 | + RequestContext wsRequest = RequestContext.Builder.create() |
| 120 | + .requestURI(URI.create(wsApp.getPath())).requestPath(wsApp.getPath()) |
| 121 | + .connection(connection).secure(request.isSecure()).build(); |
| 122 | + |
| 123 | + for (String header : headers.keySet()) { |
| 124 | + wsRequest.getHeaders().put(header, headers.get(header)); |
| 125 | + } |
| 126 | + |
| 127 | + return WebSocketEngine.getEngine().upgrade(connection, wsRequest, new WebSocketHolderListener() { |
| 128 | + @Override |
| 129 | + public void onWebSocketHolder(WebSocketEngine.WebSocketHolder webSocketHolder) { |
| 130 | + upgradeHandler.setWebSocketHolder(webSocketHolder); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private WebSocketApplication createTyrusEndpoint(HttpServletRequest request, |
| 136 | + Endpoint endpoint, String selectedProtocol) { |
| 137 | + |
| 138 | + // shouldn't matter for processing but must be unique |
| 139 | + String endpointPath = "/" + random.nextLong(); |
| 140 | + ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(endpointPath, endpoint); |
| 141 | + endpointConfig.setSubprotocols(Arrays.asList(selectedProtocol)); |
| 142 | + return createTyrusEndpoint(new EndpointWrapper(endpoint, endpointConfig, |
| 143 | + ComponentProviderService.create(), null, "/", new ErrorCollector(), |
| 144 | + endpointConfig.getConfigurator())); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Create the actual TyrusEndpoint |
| 149 | + * @param endpoint The WebSocket endpoint |
| 150 | + * @return The configured WebSocketApplication, most likely {@code TyrusEndpoint} |
| 151 | + */ |
| 152 | + abstract protected WebSocketApplication createTyrusEndpoint(EndpointWrapper endpoint); |
| 153 | + |
| 154 | + private Connection createConnection(TyrusHttpUpgradeHandler handler, HttpServletResponse response) { |
| 155 | + try { |
| 156 | + String name = "org.glassfish.tyrus.servlet.ConnectionImpl"; |
| 157 | + Class<?> clazz = ClassUtils.forName(name, GlassFishRequestUpgradeStrategy.class.getClassLoader()); |
| 158 | + Constructor<?> constructor = clazz.getDeclaredConstructor(TyrusHttpUpgradeHandler.class, HttpServletResponse.class); |
| 159 | + ReflectionUtils.makeAccessible(constructor); |
| 160 | + return (Connection) constructor.newInstance(handler, response); |
| 161 | + } |
| 162 | + catch (Exception ex) { |
| 163 | + throw new IllegalStateException("Failed to instantiate GlassFish connection", ex); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments