@@ -175,11 +175,10 @@ public void afterConnectionEstablished(WebSocketSession session) {
175
175
176
176
}
177
177
178
- @ SuppressWarnings ({ "unused" , " try"} )
178
+ @ SuppressWarnings (" try" )
179
179
@ Override
180
180
protected void handleTextMessage (WebSocketSession session , TextMessage webSocketMessage ) throws Exception {
181
- ContextSnapshot snapshot = this .contextHandshakeInterceptor .getContextSnapshot (session );
182
- try (AutoCloseable closeable = snapshot .setThreadLocalValues ()) {
181
+ try (AutoCloseable closeable = ContextHandshakeInterceptor .setThreadLocals (session )) {
183
182
handleInternal (session , webSocketMessage );
184
183
}
185
184
}
@@ -354,12 +353,14 @@ public boolean supportsPartialMessages() {
354
353
*/
355
354
private static class ContextHandshakeInterceptor implements HandshakeInterceptor {
356
355
356
+ private static final String KEY = ContextSnapshot .class .getName ();
357
+
357
358
@ Override
358
359
public boolean beforeHandshake (
359
360
ServerHttpRequest request , ServerHttpResponse response , WebSocketHandler wsHandler ,
360
361
Map <String , Object > attributes ) {
361
362
362
- attributes .put (ContextSnapshot . class . getName () , ContextSnapshot .capture ());
363
+ attributes .put (KEY , ContextSnapshot .capture ());
363
364
return true ;
364
365
}
365
366
@@ -369,10 +370,10 @@ public void afterHandshake(
369
370
@ Nullable Exception exception ) {
370
371
}
371
372
372
- public ContextSnapshot getContextSnapshot (WebSocketSession session ) {
373
- ContextSnapshot snapshot = (ContextSnapshot ) session .getAttributes ().get (ContextSnapshot . class . getName () );
374
- Assert .notNull (snapshot , "No ContextSnapshot in WebSocketSession attributes" );
375
- return snapshot ;
373
+ public static AutoCloseable setThreadLocals (WebSocketSession session ) {
374
+ ContextSnapshot snapshot = (ContextSnapshot ) session .getAttributes ().get (KEY );
375
+ Assert .notNull (snapshot , "Expected ContextSnapshot in WebSocketSession attributes" );
376
+ return snapshot . setThreadLocalValues () ;
376
377
}
377
378
}
378
379
0 commit comments