Browse Source
Now there is just one EndpointHandshakeRequestHandler that works on different runtimes.pull/292/head
8 changed files with 197 additions and 94 deletions
@ -0,0 +1,83 @@
@@ -0,0 +1,83 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.websocket.server.endpoint; |
||||
|
||||
import javax.websocket.Endpoint; |
||||
|
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.websocket.WebSocketHandler; |
||||
import org.springframework.websocket.endpoint.StandardWebSocketHandlerAdapter; |
||||
import org.springframework.websocket.server.AbstractHandshakeRequestHandler; |
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class EndpointHandshakeRequestHandler extends AbstractHandshakeRequestHandler { |
||||
|
||||
private static final boolean tomcatWebSocketPresent = ClassUtils.isPresent( |
||||
"org.apache.tomcat.websocket.server.WsHandshakeRequest", EndpointHandshakeRequestHandler.class.getClassLoader()); |
||||
|
||||
private final EndpointRegistration endpointRegistration; |
||||
|
||||
private final EndpointRequestUpgradeStrategy upgradeStrategy; |
||||
|
||||
|
||||
public EndpointHandshakeRequestHandler(WebSocketHandler webSocketHandler) { |
||||
this(new StandardWebSocketHandlerAdapter(webSocketHandler)); |
||||
} |
||||
|
||||
public EndpointHandshakeRequestHandler(Endpoint endpoint) { |
||||
this.endpointRegistration = new EndpointRegistration("/dummy", endpoint); |
||||
this.upgradeStrategy = createRequestUpgradeStrategy(); |
||||
} |
||||
|
||||
private static EndpointRequestUpgradeStrategy createRequestUpgradeStrategy() { |
||||
String className; |
||||
if (tomcatWebSocketPresent) { |
||||
className = "org.springframework.websocket.server.endpoint.TomcatRequestUpgradeStrategy"; |
||||
} |
||||
else { |
||||
throw new IllegalStateException("No suitable EndpointRequestUpgradeStrategy"); |
||||
} |
||||
try { |
||||
Class<?> clazz = ClassUtils.forName(className, EndpointHandshakeRequestHandler.class.getClassLoader()); |
||||
return (EndpointRequestUpgradeStrategy) BeanUtils.instantiateClass(clazz.getConstructor()); |
||||
} |
||||
catch (Throwable t) { |
||||
throw new IllegalStateException("Failed to instantiate " + className, t); |
||||
} |
||||
} |
||||
|
||||
public EndpointRegistration getEndpointRegistration() { |
||||
return this.endpointRegistration; |
||||
} |
||||
|
||||
@Override |
||||
public void doHandshakeInternal(ServerHttpRequest request, ServerHttpResponse response, String protocol) |
||||
throws Exception { |
||||
|
||||
logger.debug("Upgrading HTTP request"); |
||||
this.upgradeStrategy.upgrade(request, response, protocol, this.endpointRegistration); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.websocket.server.endpoint; |
||||
|
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
|
||||
|
||||
/** |
||||
* A strategy for performing the actual request upgrade after the handshake checks have |
||||
* passed, encapsulating runtime-specific steps of the handshake. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public interface EndpointRequestUpgradeStrategy { |
||||
|
||||
void upgrade(ServerHttpRequest request, ServerHttpResponse response, String protocol, |
||||
EndpointRegistration registration) throws Exception; |
||||
|
||||
} |
||||
@ -1,79 +0,0 @@
@@ -1,79 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.websocket.server.endpoint; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.Collections; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.websocket.Endpoint; |
||||
import javax.websocket.server.ServerEndpointConfig; |
||||
|
||||
import org.apache.tomcat.websocket.server.WsHandshakeRequest; |
||||
import org.apache.tomcat.websocket.server.WsHttpUpgradeHandler; |
||||
import org.apache.tomcat.websocket.server.WsServerContainer; |
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.http.server.ServletServerHttpRequest; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ReflectionUtils; |
||||
import org.springframework.websocket.WebSocketHandler; |
||||
import org.springframework.websocket.endpoint.StandardWebSocketHandlerAdapter; |
||||
import org.springframework.websocket.server.AbstractHandshakeRequestHandler; |
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class TomcatHandshakeRequestHandler extends AbstractHandshakeRequestHandler { |
||||
|
||||
private final Endpoint endpoint; |
||||
|
||||
private final ServerEndpointConfig endpointConfig; |
||||
|
||||
|
||||
public TomcatHandshakeRequestHandler(WebSocketHandler webSocketHandler) { |
||||
this(new StandardWebSocketHandlerAdapter(webSocketHandler)); |
||||
} |
||||
|
||||
public TomcatHandshakeRequestHandler(Endpoint endpoint) { |
||||
this.endpoint = endpoint; |
||||
this.endpointConfig = new ServerEndpointRegistration("/dummy", this.endpoint); |
||||
} |
||||
|
||||
@Override |
||||
public void doHandshakeInternal(ServerHttpRequest request, ServerHttpResponse response, String protocol) throws Exception { |
||||
|
||||
logger.debug("Upgrading HTTP request"); |
||||
|
||||
Assert.isTrue(request instanceof ServletServerHttpRequest); |
||||
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); |
||||
|
||||
WsHandshakeRequest wsRequest = new WsHandshakeRequest(servletRequest); |
||||
Method method = ReflectionUtils.findMethod(WsHandshakeRequest.class, "finished"); |
||||
ReflectionUtils.makeAccessible(method); |
||||
method.invoke(wsRequest); |
||||
|
||||
WsHttpUpgradeHandler wsHandler = servletRequest.upgrade(WsHttpUpgradeHandler.class); |
||||
|
||||
wsHandler.preInit(this.endpoint, this.endpointConfig, WsServerContainer.getServerContainer(), |
||||
wsRequest, protocol, Collections.<String, String> emptyMap(), servletRequest.isSecure()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.websocket.server.endpoint; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.Collections; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.apache.tomcat.websocket.server.WsHandshakeRequest; |
||||
import org.apache.tomcat.websocket.server.WsHttpUpgradeHandler; |
||||
import org.apache.tomcat.websocket.server.WsServerContainer; |
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.http.server.ServletServerHttpRequest; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class TomcatRequestUpgradeStrategy implements EndpointRequestUpgradeStrategy { |
||||
|
||||
|
||||
@Override |
||||
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String protocol, |
||||
EndpointRegistration registration) throws Exception { |
||||
|
||||
Assert.isTrue(request instanceof ServletServerHttpRequest); |
||||
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); |
||||
|
||||
WsHttpUpgradeHandler wsHandler = servletRequest.upgrade(WsHttpUpgradeHandler.class); |
||||
|
||||
WsHandshakeRequest wsRequest = new WsHandshakeRequest(servletRequest); |
||||
Method method = ReflectionUtils.findMethod(WsHandshakeRequest.class, "finished"); |
||||
ReflectionUtils.makeAccessible(method); |
||||
method.invoke(wsRequest); |
||||
|
||||
wsHandler.preInit(registration.getEndpoint(), registration, |
||||
WsServerContainer.getServerContainer(), wsRequest, protocol, |
||||
Collections.<String, String> emptyMap(), servletRequest.isSecure()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue