Skip to content

Commit b6fdcff

Browse files
committed
HttpSessionHandshakeInterceptor and related web.socket.server polishing
Issue: SPR-12352
1 parent 716916b commit b6fdcff

21 files changed

+158
-235
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -34,18 +34,12 @@
3434
@SuppressWarnings("serial")
3535
public class HandshakeFailureException extends NestedRuntimeException {
3636

37-
/**
38-
* Constructor with message and root cause.
39-
*/
40-
public HandshakeFailureException(String message, Throwable cause) {
41-
super(message, cause);
42-
}
43-
44-
/**
45-
* Constructor without a message.
46-
*/
4737
public HandshakeFailureException(String message) {
4838
super(message);
4939
}
5040

41+
public HandshakeFailureException(String message, Throwable cause) {
42+
super(message, cause);
43+
}
44+
5145
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -28,7 +28,6 @@
2828
*
2929
* @author Rossen Stoyanchev
3030
* @since 4.0
31-
*
3231
* @see HandshakeInterceptor
3332
* @see org.springframework.web.socket.server.support.WebSocketHttpRequestHandler
3433
* @see org.springframework.web.socket.sockjs.SockJsService
@@ -37,19 +36,16 @@ public interface HandshakeHandler {
3736

3837
/**
3938
* Initiate the handshake.
40-
*
4139
* @param request the current request
4240
* @param response the current response
4341
* @param wsHandler the handler to process WebSocket messages; see
4442
* {@link PerConnectionWebSocketHandler} for providing a handler with
4543
* per-connection lifecycle.
4644
* @param attributes attributes from the HTTP handshake to associate with the WebSocket
4745
* session; the provided attributes are copied, the original map is not used.
48-
*
4946
* @return whether the handshake negotiation was successful or not. In either case the
5047
* response status, headers, and body will have been updated to reflect the
5148
* result of the negotiation
52-
*
5349
* @throws HandshakeFailureException thrown when handshake processing failed to
5450
* complete due to an internal, unrecoverable error, i.e. a server error as
5551
* opposed to a failure to successfully negotiate the handshake.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -41,18 +41,18 @@ public interface HandshakeInterceptor {
4141
* @param wsHandler the target WebSocket handler
4242
* @param attributes attributes from the HTTP handshake to associate with the WebSocket
4343
* session; the provided attributes are copied, the original map is not used.
44-
* @return whether to proceed with the handshake {@code true} or abort {@code false}
44+
* @return whether to proceed with the handshake ({@code true}) or abort ({@code false})
4545
*/
4646
boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
4747
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception;
4848

4949
/**
50-
* Invoked after the handshake is done. The response status and headers indicate the
51-
* results of the handshake, i.e. whether it was successful or not.
50+
* Invoked after the handshake is done. The response status and headers indicate
51+
* the results of the handshake, i.e. whether it was successful or not.
5252
* @param request the current request
5353
* @param response the current response
5454
* @param wsHandler the target WebSocket handler
55-
* @param exception an exception raised during the handshake, or {@code null}
55+
* @param exception an exception raised during the handshake, or {@code null} if none
5656
*/
5757
void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
5858
WebSocketHandler wsHandler, Exception exception);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -39,7 +39,7 @@ public interface RequestUpgradeStrategy {
3939
String[] getSupportedVersions();
4040

4141
/**
42-
* @return the WebSocket protocol extensions supported by the underlying WebSocket server.
42+
* Return the WebSocket protocol extensions supported by the underlying WebSocket server.
4343
*/
4444
List<WebSocketExtension> getSupportedExtensions(ServerHttpRequest request);
4545

@@ -53,9 +53,8 @@ public interface RequestUpgradeStrategy {
5353
* @param user the user to associate with the WebSocket session
5454
* @param wsHandler the handler for WebSocket messages
5555
* @param attributes handshake request specific attributes to be set on the WebSocket
56-
* session via {@link org.springframework.web.socket.server.HandshakeInterceptor}
57-
* and thus made available to the
58-
* {@link org.springframework.web.socket.WebSocketHandler};
56+
* session via {@link org.springframework.web.socket.server.HandshakeInterceptor} and
57+
* thus made available to the {@link org.springframework.web.socket.WebSocketHandler}
5958
* @throws HandshakeFailureException thrown when handshake processing failed to
6059
* complete due to an internal, unrecoverable error, i.e. a server error as
6160
* opposed to a failure to successfully negotiate the requirements of the

spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -63,7 +63,7 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
6363
new NamedThreadLocal<WebSocketHandlerContainer>("WebSocket Handler Container");
6464

6565

66-
private WebSocketServerFactory factory;
66+
private final WebSocketServerFactory factory;
6767

6868
private volatile List<WebSocketExtension> supportedExtensions;
6969

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
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-
171
/**
18-
* Server-side support for the Jetty WebSocket API.
2+
* Server-side support for the Jetty 9+ WebSocket API.
193
*/
204
package org.springframework.web.socket.server.jetty;
21-
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
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-
171
/**
182
* Server-side abstractions for WebSocket interactions.
193
*/
204
package org.springframework.web.socket.server;
21-

spring-websocket/src/main/java/org/springframework/web/socket/server/standard/AbstractStandardUpgradeStrategy.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -112,15 +112,15 @@ public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
112112
StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);
113113

114114
List<Extension> extensions = new ArrayList<Extension>();
115-
for (WebSocketExtension e : selectedExtensions) {
116-
extensions.add(new WebSocketToStandardExtensionAdapter(e));
115+
for (WebSocketExtension extension : selectedExtensions) {
116+
extensions.add(new WebSocketToStandardExtensionAdapter(extension));
117117
}
118118

119119
upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
120120
}
121121

122122
protected abstract void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
123-
String selectedProtocol, List<Extension> selectedExtensions,
124-
Endpoint endpoint) throws HandshakeFailureException;
123+
String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
124+
throws HandshakeFailureException;
125125

126126
}

spring-websocket/src/main/java/org/springframework/web/socket/server/standard/AbstractTyrusRequestUpgradeStrategy.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,6 +16,23 @@
1616

1717
package org.springframework.web.socket.server.standard;
1818

19+
import java.io.IOException;
20+
import java.lang.reflect.Constructor;
21+
import java.lang.reflect.Method;
22+
import java.net.URI;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.Random;
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
import javax.websocket.DeploymentException;
31+
import javax.websocket.Endpoint;
32+
import javax.websocket.EndpointConfig;
33+
import javax.websocket.Extension;
34+
import javax.websocket.WebSocketContainer;
35+
1936
import org.glassfish.tyrus.core.ComponentProviderService;
2037
import org.glassfish.tyrus.core.RequestContext;
2138
import org.glassfish.tyrus.core.TyrusEndpoint;
@@ -26,6 +43,7 @@
2643
import org.glassfish.tyrus.core.WebSocketApplication;
2744
import org.glassfish.tyrus.server.TyrusServerContainer;
2845
import org.glassfish.tyrus.spi.WebSocketEngine.UpgradeInfo;
46+
2947
import org.springframework.beans.DirectFieldAccessor;
3048
import org.springframework.http.HttpHeaders;
3149
import org.springframework.http.server.ServerHttpRequest;
@@ -35,23 +53,6 @@
3553
import org.springframework.web.socket.WebSocketExtension;
3654
import org.springframework.web.socket.server.HandshakeFailureException;
3755

38-
import javax.servlet.ServletException;
39-
import javax.servlet.http.HttpServletRequest;
40-
import javax.servlet.http.HttpServletResponse;
41-
import javax.websocket.DeploymentException;
42-
import javax.websocket.Endpoint;
43-
import javax.websocket.EndpointConfig;
44-
import javax.websocket.Extension;
45-
import javax.websocket.WebSocketContainer;
46-
import java.io.IOException;
47-
import java.lang.reflect.Constructor;
48-
import java.lang.reflect.Method;
49-
import java.net.URI;
50-
import java.util.ArrayList;
51-
import java.util.Arrays;
52-
import java.util.List;
53-
import java.util.Random;
54-
5556
/**
5657
* An base class for WebSocket servers using Tyrus.
5758
*
@@ -65,7 +66,6 @@ public abstract class AbstractTyrusRequestUpgradeStrategy extends AbstractStanda
6566

6667
private static final Random random = new Random();
6768

68-
6969
private final ComponentProviderService componentProvider = ComponentProviderService.create();
7070

7171

@@ -78,17 +78,15 @@ protected List<WebSocketExtension> getInstalledExtensions(WebSocketContainer con
7878
try {
7979
return super.getInstalledExtensions(container);
8080
}
81-
catch (UnsupportedOperationException e) {
82-
return new ArrayList<WebSocketExtension>();
81+
catch (UnsupportedOperationException ex) {
82+
return new ArrayList<WebSocketExtension>(0);
8383
}
8484
}
8585

86-
protected abstract TyrusEndpointHelper getEndpointHelper();
87-
88-
8986
@Override
9087
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
91-
String subProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException {
88+
String selectedProtocol, List<Extension> extensions, Endpoint endpoint)
89+
throws HandshakeFailureException {
9290

9391
HttpServletRequest servletRequest = getHttpServletRequest(request);
9492
HttpServletResponse servletResponse = getHttpServletResponse(response);
@@ -100,7 +98,7 @@ public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse respon
10098
try {
10199
// Shouldn't matter for processing but must be unique
102100
String path = "/" + random.nextLong();
103-
tyrusEndpoint = createTyrusEndpoint(endpoint, path, subProtocol, extensions, serverContainer, engine);
101+
tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine);
104102
getEndpointHelper().register(engine, tyrusEndpoint);
105103

106104
HttpHeaders headers = request.getHeaders();
@@ -133,9 +131,6 @@ public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse respon
133131
}
134132
}
135133

136-
protected abstract void handleSuccess(HttpServletRequest request, HttpServletResponse response,
137-
UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException;
138-
139134
private Object createTyrusEndpoint(Endpoint endpoint, String endpointPath, String protocol,
140135
List<Extension> extensions, WebSocketContainer container, TyrusWebSocketEngine engine)
141136
throws DeploymentException {
@@ -161,6 +156,12 @@ private RequestContext createRequestContext(HttpServletRequest request, String e
161156
}
162157

163158

159+
protected abstract TyrusEndpointHelper getEndpointHelper();
160+
161+
protected abstract void handleSuccess(HttpServletRequest request, HttpServletResponse response,
162+
UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException;
163+
164+
164165
/**
165166
* Helps with the creation, registration, and un-registration of endpoints.
166167
*/
@@ -172,9 +173,9 @@ Object createdEndpoint(ServerEndpointRegistration registration, ComponentProvide
172173
void register(TyrusWebSocketEngine engine, Object endpoint);
173174

174175
void unregister(TyrusWebSocketEngine engine, Object endpoint);
175-
176176
}
177177

178+
178179
protected static class Tyrus17EndpointHelper implements TyrusEndpointHelper {
179180

180181
private static final Constructor<?> constructor;
@@ -243,6 +244,7 @@ public void unregister(TyrusWebSocketEngine engine, Object endpoint) {
243244
}
244245
}
245246

247+
246248
protected static class Tyrus135EndpointHelper implements TyrusEndpointHelper {
247249

248250
private static final Method registerMethod;

0 commit comments

Comments
 (0)