Skip to content

Commit 6e1567b

Browse files
committed
Add WebSphereRequestUpgradeStrategy
Tested with Aug 2015 WAS Liberty Beta for the upcoming 8.5.5.7 release. Issue: SPR-12367
1 parent a369fc8 commit 6e1567b

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,25 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
6565
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
6666

6767

68+
private static final ClassLoader classLoader = AbstractHandshakeHandler.class.getClassLoader();
69+
6870
private static final boolean jettyWsPresent = ClassUtils.isPresent(
69-
"org.eclipse.jetty.websocket.server.WebSocketServerFactory", AbstractHandshakeHandler.class.getClassLoader());
71+
"org.eclipse.jetty.websocket.server.WebSocketServerFactory", classLoader);
7072

7173
private static final boolean tomcatWsPresent = ClassUtils.isPresent(
72-
"org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", AbstractHandshakeHandler.class.getClassLoader());
74+
"org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", classLoader);
7375

7476
private static final boolean undertowWsPresent = ClassUtils.isPresent(
75-
"io.undertow.websockets.jsr.ServerWebSocketContainer", AbstractHandshakeHandler.class.getClassLoader());
77+
"io.undertow.websockets.jsr.ServerWebSocketContainer", classLoader);
7678

7779
private static final boolean glassFishWsPresent = ClassUtils.isPresent(
78-
"org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler", AbstractHandshakeHandler.class.getClassLoader());
80+
"org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler", classLoader);
7981

8082
private static final boolean webLogicWsPresent = ClassUtils.isPresent(
81-
"weblogic.websocket.tyrus.TyrusServletWriter", AbstractHandshakeHandler.class.getClassLoader());
83+
"weblogic.websocket.tyrus.TyrusServletWriter", classLoader);
84+
85+
private static final boolean webSphereWsPresent = ClassUtils.isPresent(
86+
"com.ibm.websphere.wsoc.WsWsocServerContainer", classLoader);
8287

8388

8489
protected final Log logger = LogFactory.getLog(getClass());
@@ -126,15 +131,19 @@ else if (glassFishWsPresent) {
126131
else if (webLogicWsPresent) {
127132
className = "org.springframework.web.socket.server.standard.WebLogicRequestUpgradeStrategy";
128133
}
134+
else if (webSphereWsPresent) {
135+
className = "org.springframework.web.socket.server.standard.WebSphereRequestUpgradeStrategy";
136+
}
129137
else {
130138
throw new IllegalStateException("No suitable default RequestUpgradeStrategy found");
131139
}
132140
try {
133-
Class<?> clazz = ClassUtils.forName(className, AbstractHandshakeHandler.class.getClassLoader());
141+
Class<?> clazz = ClassUtils.forName(className, classLoader);
134142
return (RequestUpgradeStrategy) clazz.newInstance();
135143
}
136144
catch (Throwable ex) {
137-
throw new IllegalStateException("Failed to instantiate RequestUpgradeStrategy: " + className, ex);
145+
throw new IllegalStateException(
146+
"Failed to instantiate RequestUpgradeStrategy: " + className, ex);
138147
}
139148
}
140149

0 commit comments

Comments
 (0)