45
45
import org .springframework .web .socket .adapter .AbstractWebSocketSession ;
46
46
47
47
/**
48
- * A {@link WebSocketSession} for use with the Jetty 9 WebSocket API.
48
+ * A {@link WebSocketSession} for use with the Jetty 9.3/9.4 WebSocket API.
49
49
*
50
50
* @author Phillip Webb
51
51
* @author Rossen Stoyanchev
52
52
* @author Brian Clozel
53
+ * @author Juergen Hoeller
53
54
* @since 4.0
54
55
*/
55
56
public class JettyWebSocketSession extends AbstractWebSocketSession <Session > {
56
57
57
58
// As of Jetty 9.4, UpgradeRequest and UpgradeResponse are interfaces instead of classes
58
- private static final boolean isJetty94 ;
59
+ private static final boolean directInterfaceCalls ;
59
60
60
61
private static Method getUpgradeRequest ;
61
62
private static Method getUpgradeResponse ;
62
63
private static Method getRequestURI ;
63
64
private static Method getHeaders ;
65
+ private static Method getUserPrincipal ;
64
66
private static Method getAcceptedSubProtocol ;
65
67
private static Method getExtensions ;
66
- private static Method getUserPrincipal ;
67
-
68
- private String id ;
69
-
70
- private URI uri ;
71
-
72
- private HttpHeaders headers ;
73
-
74
- private String acceptedProtocol ;
75
-
76
- private List <WebSocketExtension > extensions ;
77
-
78
- private Principal user ;
79
68
80
69
static {
81
- isJetty94 = UpgradeRequest .class .isInterface ();
82
- if (!isJetty94 ) {
70
+ directInterfaceCalls = UpgradeRequest .class .isInterface ();
71
+ if (!directInterfaceCalls ) {
83
72
try {
84
73
getUpgradeRequest = Session .class .getMethod ("getUpgradeRequest" );
85
74
getUpgradeResponse = Session .class .getMethod ("getUpgradeResponse" );
86
75
getRequestURI = UpgradeRequest .class .getMethod ("getRequestURI" );
87
76
getHeaders = UpgradeRequest .class .getMethod ("getHeaders" );
77
+ getUserPrincipal = UpgradeRequest .class .getMethod ("getUserPrincipal" );
88
78
getAcceptedSubProtocol = UpgradeResponse .class .getMethod ("getAcceptedSubProtocol" );
89
79
getExtensions = UpgradeResponse .class .getMethod ("getExtensions" );
90
- getUserPrincipal = UpgradeRequest .class .getMethod ("getUserPrincipal" );
91
80
}
92
81
catch (NoSuchMethodException ex ) {
93
82
throw new IllegalStateException ("Incompatible Jetty API" , ex );
94
83
}
95
84
}
96
85
}
97
86
87
+
88
+ private String id ;
89
+
90
+ private URI uri ;
91
+
92
+ private HttpHeaders headers ;
93
+
94
+ private String acceptedProtocol ;
95
+
96
+ private List <WebSocketExtension > extensions ;
97
+
98
+ private Principal user ;
99
+
100
+
98
101
/**
99
102
* Create a new {@link JettyWebSocketSession} instance.
100
- *
101
103
* @param attributes attributes from the HTTP handshake to associate with the WebSocket session
102
104
*/
103
105
public JettyWebSocketSession (Map <String , Object > attributes ) {
@@ -106,11 +108,10 @@ public JettyWebSocketSession(Map<String, Object> attributes) {
106
108
107
109
/**
108
110
* Create a new {@link JettyWebSocketSession} instance associated with the given user.
109
- *
110
111
* @param attributes attributes from the HTTP handshake to associate with the WebSocket
111
112
* session; the provided attributes are copied, the original map is not used.
112
- * @param user the user associated with the session; if {@code null} we'll fallback on the user
113
- * available via {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()}
113
+ * @param user the user associated with the session; if {@code null} we'll fallback on the
114
+ * user available via {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()}
114
115
*/
115
116
public JettyWebSocketSession (Map <String , Object > attributes , Principal user ) {
116
117
super (attributes );
@@ -191,36 +192,32 @@ public int getBinaryMessageSizeLimit() {
191
192
192
193
@ Override
193
194
public boolean isOpen () {
194
- return (( getNativeSession () != null ) && getNativeSession ().isOpen ());
195
+ return (getNativeSession () != null && getNativeSession ().isOpen ());
195
196
}
196
197
198
+
197
199
@ Override
198
200
public void initializeNativeSession (Session session ) {
199
201
super .initializeNativeSession (session );
200
- if (isJetty94 ) {
201
- initializeJetty94Session (session );
202
+ if (directInterfaceCalls ) {
203
+ initializeJettySessionDirectly (session );
202
204
}
203
205
else {
204
- initializeJettySession (session );
206
+ initializeJettySessionReflectively (session );
205
207
}
206
208
}
207
209
208
- @ SuppressWarnings ("unchecked" )
209
- private void initializeJettySession (Session session ) {
210
-
211
- Object request = ReflectionUtils .invokeMethod (getUpgradeRequest , session );
212
- Object response = ReflectionUtils .invokeMethod (getUpgradeResponse , session );
213
-
210
+ private void initializeJettySessionDirectly (Session session ) {
214
211
this .id = ObjectUtils .getIdentityHexString (getNativeSession ());
215
- this .uri = ( URI ) ReflectionUtils . invokeMethod ( getRequestURI , request );
212
+ this .uri = session . getUpgradeRequest (). getRequestURI ( );
216
213
217
214
this .headers = new HttpHeaders ();
218
- this .headers .putAll (( Map < String , List < String >>) ReflectionUtils . invokeMethod ( getHeaders , request ));
215
+ this .headers .putAll (session . getUpgradeRequest (). getHeaders ( ));
219
216
this .headers = HttpHeaders .readOnlyHttpHeaders (headers );
220
217
221
- this .acceptedProtocol = ( String ) ReflectionUtils . invokeMethod ( getAcceptedSubProtocol , response );
218
+ this .acceptedProtocol = session . getUpgradeResponse (). getAcceptedSubProtocol ( );
222
219
223
- List <ExtensionConfig > source = ( List < ExtensionConfig >) ReflectionUtils . invokeMethod ( getExtensions , response );
220
+ List <ExtensionConfig > source = session . getUpgradeResponse (). getExtensions ( );
224
221
if (source != null ) {
225
222
this .extensions = new ArrayList <>(source .size ());
226
223
for (ExtensionConfig ec : source ) {
@@ -232,21 +229,25 @@ private void initializeJettySession(Session session) {
232
229
}
233
230
234
231
if (this .user == null ) {
235
- this .user = ( Principal ) ReflectionUtils . invokeMethod ( getUserPrincipal , request );
232
+ this .user = session . getUpgradeRequest (). getUserPrincipal ( );
236
233
}
237
234
}
238
235
239
- private void initializeJetty94Session (Session session ) {
236
+ @ SuppressWarnings ("unchecked" )
237
+ private void initializeJettySessionReflectively (Session session ) {
238
+ Object request = ReflectionUtils .invokeMethod (getUpgradeRequest , session );
239
+ Object response = ReflectionUtils .invokeMethod (getUpgradeResponse , session );
240
+
240
241
this .id = ObjectUtils .getIdentityHexString (getNativeSession ());
241
- this .uri = session . getUpgradeRequest (). getRequestURI ( );
242
+ this .uri = ( URI ) ReflectionUtils . invokeMethod ( getRequestURI , request );
242
243
243
244
this .headers = new HttpHeaders ();
244
- this .headers .putAll (session . getUpgradeRequest (). getHeaders ( ));
245
+ this .headers .putAll (( Map < String , List < String >>) ReflectionUtils . invokeMethod ( getHeaders , request ));
245
246
this .headers = HttpHeaders .readOnlyHttpHeaders (headers );
246
247
247
- this .acceptedProtocol = session . getUpgradeResponse (). getAcceptedSubProtocol ( );
248
+ this .acceptedProtocol = ( String ) ReflectionUtils . invokeMethod ( getAcceptedSubProtocol , response );
248
249
249
- List <ExtensionConfig > source = session . getUpgradeResponse (). getExtensions ( );
250
+ List <ExtensionConfig > source = ( List < ExtensionConfig >) ReflectionUtils . invokeMethod ( getExtensions , response );
250
251
if (source != null ) {
251
252
this .extensions = new ArrayList <>(source .size ());
252
253
for (ExtensionConfig ec : source ) {
@@ -258,10 +259,11 @@ private void initializeJetty94Session(Session session) {
258
259
}
259
260
260
261
if (this .user == null ) {
261
- this .user = session . getUpgradeRequest (). getUserPrincipal ( );
262
+ this .user = ( Principal ) ReflectionUtils . invokeMethod ( getUserPrincipal , request );
262
263
}
263
264
}
264
265
266
+
265
267
@ Override
266
268
protected void sendTextMessage (TextMessage message ) throws IOException {
267
269
getRemoteEndpoint ().sendString (message .getPayload ());
@@ -287,7 +289,7 @@ private RemoteEndpoint getRemoteEndpoint() throws IOException {
287
289
return getNativeSession ().getRemote ();
288
290
}
289
291
catch (WebSocketException ex ) {
290
- throw new IOException ("Unable to obtain RemoteEndpoint in session= " + getId (), ex );
292
+ throw new IOException ("Unable to obtain RemoteEndpoint in session " + getId (), ex );
291
293
}
292
294
}
293
295
0 commit comments