@@ -175,11 +175,10 @@ public void afterConnectionEstablished(WebSocketSession session) {
175175
176176 }
177177
178- @ SuppressWarnings ({ "unused" , " try"} )
178+ @ SuppressWarnings (" try" )
179179 @ Override
180180 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 )) {
183182 handleInternal (session , webSocketMessage );
184183 }
185184 }
@@ -354,12 +353,14 @@ public boolean supportsPartialMessages() {
354353 */
355354 private static class ContextHandshakeInterceptor implements HandshakeInterceptor {
356355
356+ private static final String KEY = ContextSnapshot .class .getName ();
357+
357358 @ Override
358359 public boolean beforeHandshake (
359360 ServerHttpRequest request , ServerHttpResponse response , WebSocketHandler wsHandler ,
360361 Map <String , Object > attributes ) {
361362
362- attributes .put (ContextSnapshot . class . getName () , ContextSnapshot .capture ());
363+ attributes .put (KEY , ContextSnapshot .capture ());
363364 return true ;
364365 }
365366
@@ -369,10 +370,10 @@ public void afterHandshake(
369370 @ Nullable Exception exception ) {
370371 }
371372
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 () ;
376377 }
377378 }
378379
0 commit comments