|
| 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 io.undertow.server.HttpServerExchange; |
| 20 | +import io.undertow.server.HttpUpgradeListener; |
| 21 | +import io.undertow.servlet.api.InstanceFactory; |
| 22 | +import io.undertow.servlet.api.InstanceHandle; |
| 23 | +import io.undertow.servlet.websockets.ServletWebSocketHttpExchange; |
| 24 | +import io.undertow.websockets.core.WebSocketChannel; |
| 25 | +import io.undertow.websockets.core.protocol.Handshake; |
| 26 | +import io.undertow.websockets.core.protocol.version07.Hybi07Handshake; |
| 27 | +import io.undertow.websockets.core.protocol.version08.Hybi08Handshake; |
| 28 | +import io.undertow.websockets.core.protocol.version13.Hybi13Handshake; |
| 29 | +import io.undertow.websockets.jsr.ConfiguredServerEndpoint; |
| 30 | +import io.undertow.websockets.jsr.EncodingFactory; |
| 31 | +import io.undertow.websockets.jsr.EndpointSessionHandler; |
| 32 | +import io.undertow.websockets.jsr.ServerWebSocketContainer; |
| 33 | +import io.undertow.websockets.jsr.handshake.HandshakeUtil; |
| 34 | +import org.springframework.http.server.ServerHttpRequest; |
| 35 | +import org.springframework.http.server.ServerHttpResponse; |
| 36 | +import org.springframework.web.socket.server.HandshakeFailureException; |
| 37 | +import org.xnio.StreamConnection; |
| 38 | + |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
| 41 | +import javax.websocket.Decoder; |
| 42 | +import javax.websocket.Encoder; |
| 43 | +import javax.websocket.Endpoint; |
| 44 | +import javax.websocket.Extension; |
| 45 | +import java.util.*; |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * A {@link org.springframework.web.socket.server.RequestUpgradeStrategy} for use |
| 50 | + * with WildFly and its underlying Undertow web server. |
| 51 | + * |
| 52 | + * @author Rossen Stoyanchev |
| 53 | + * @since 4.0.1 |
| 54 | + */ |
| 55 | +public class UndertowRequestUpgradeStrategy extends AbstractStandardUpgradeStrategy { |
| 56 | + |
| 57 | + private final Handshake[] handshakes; |
| 58 | + |
| 59 | + private final String[] supportedVersions; |
| 60 | + |
| 61 | + |
| 62 | + public UndertowRequestUpgradeStrategy() { |
| 63 | + this.handshakes = new Handshake[] { new Hybi13Handshake(), new Hybi08Handshake(), new Hybi07Handshake() }; |
| 64 | + this.supportedVersions = initSupportedVersions(this.handshakes); |
| 65 | + } |
| 66 | + |
| 67 | + private String[] initSupportedVersions(Handshake[] handshakes) { |
| 68 | + String[] versions = new String[handshakes.length]; |
| 69 | + for (int i=0; i < versions.length; i++) { |
| 70 | + versions[i] = handshakes[i].getVersion().toHttpHeaderValue(); |
| 71 | + } |
| 72 | + return versions; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String[] getSupportedVersions() { |
| 77 | + return this.supportedVersions; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, |
| 82 | + List<Extension> selectedExtensions, final Endpoint endpoint) throws HandshakeFailureException { |
| 83 | + |
| 84 | + HttpServletRequest servletRequest = getHttpServletRequest(request); |
| 85 | + HttpServletResponse servletResponse = getHttpServletResponse(response); |
| 86 | + |
| 87 | + final ServletWebSocketHttpExchange exchange = new ServletWebSocketHttpExchange(servletRequest, servletResponse); |
| 88 | + exchange.putAttachment(HandshakeUtil.PATH_PARAMS, Collections.<String, String>emptyMap()); |
| 89 | + |
| 90 | + ServerWebSocketContainer wsContainer = (ServerWebSocketContainer) getContainer(servletRequest); |
| 91 | + final EndpointSessionHandler endpointSessionHandler = new EndpointSessionHandler(wsContainer); |
| 92 | + |
| 93 | + final Handshake handshake = getHandshakeToUse(exchange); |
| 94 | + |
| 95 | + final ConfiguredServerEndpoint configuredServerEndpoint = createConfiguredServerEndpoint( |
| 96 | + selectedProtocol, selectedExtensions, endpoint, servletRequest); |
| 97 | + |
| 98 | + exchange.upgradeChannel(new HttpUpgradeListener() { |
| 99 | + @Override |
| 100 | + public void handleUpgrade(StreamConnection connection, HttpServerExchange serverExchange) { |
| 101 | + WebSocketChannel channel = handshake.createChannel(exchange, connection, exchange.getBufferPool()); |
| 102 | + HandshakeUtil.setConfig(channel, configuredServerEndpoint); |
| 103 | + endpointSessionHandler.onConnect(exchange, channel); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + handshake.handshake(exchange); |
| 108 | + } |
| 109 | + |
| 110 | + private Handshake getHandshakeToUse(ServletWebSocketHttpExchange exchange) { |
| 111 | + for (Handshake handshake : this.handshakes) { |
| 112 | + if (handshake.matches(exchange)) { |
| 113 | + return handshake; |
| 114 | + } |
| 115 | + } |
| 116 | + // Should never occur |
| 117 | + throw new HandshakeFailureException("No matching Undertow Handshake found: " + exchange.getRequestHeaders()); |
| 118 | + } |
| 119 | + |
| 120 | + private ConfiguredServerEndpoint createConfiguredServerEndpoint(String selectedProtocol, |
| 121 | + List<Extension> selectedExtensions, Endpoint endpoint, HttpServletRequest servletRequest) { |
| 122 | + |
| 123 | + String path = servletRequest.getRequestURI(); // shouldn't matter |
| 124 | + ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration(path, endpoint); |
| 125 | + endpointRegistration.setSubprotocols(Arrays.asList(selectedProtocol)); |
| 126 | + endpointRegistration.setExtensions(selectedExtensions); |
| 127 | + |
| 128 | + return new ConfiguredServerEndpoint(endpointRegistration, |
| 129 | + new EndpointInstanceFactory(endpoint), null, |
| 130 | + new EncodingFactory( |
| 131 | + Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(), |
| 132 | + Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap(), |
| 133 | + Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(), |
| 134 | + Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap())); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + private static class EndpointInstanceFactory implements InstanceFactory<Endpoint> { |
| 139 | + |
| 140 | + private final Endpoint endpoint; |
| 141 | + |
| 142 | + public EndpointInstanceFactory(Endpoint endpoint) { |
| 143 | + this.endpoint = endpoint; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public InstanceHandle<Endpoint> createInstance() throws InstantiationException { |
| 148 | + |
| 149 | + return new InstanceHandle<Endpoint>() { |
| 150 | + @Override |
| 151 | + public Endpoint getInstance() { |
| 152 | + return endpoint; |
| 153 | + } |
| 154 | + @Override |
| 155 | + public void release() { |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments