@Override protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) { Set<String> patterns = mapping.getDestinationConditions().getPatterns(); if (!CollectionUtils.isEmpty(patterns)) { String pattern = patterns.iterator().next(); Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination); if (!CollectionUtils.isEmpty(vars)) { MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class); Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required"); mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars); } } try { SimpAttributesContextHolder.setAttributesFromMessage(message); super.handleMatch(mapping, handlerMethod, lookupDestination, message); } finally { SimpAttributesContextHolder.resetAttributes(); } }
@Override public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) { this.decoders.remove(session.getId()); Message<byte[]> message = createDisconnectMessage(session); SimpAttributes simpAttributes = SimpAttributes.fromMessage(message); try { SimpAttributesContextHolder.setAttributes(simpAttributes); if (this.eventPublisher != null) { Principal user = getUser(session); publishEvent(this.eventPublisher, new SessionDisconnectEvent(this, message, session.getId(), closeStatus, user)); } outputChannel.send(message); } finally { this.stompAuthentications.remove(session.getId()); SimpAttributesContextHolder.resetAttributes(); simpAttributes.sessionCompleted(); } }
@Override public void registerDestructionCallback(String name, Runnable callback) { SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback); }
@Test public void resetAttributes() { SimpAttributesContextHolder.setAttributes(this.simpAttributes); assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes)); SimpAttributesContextHolder.resetAttributes(); assertThat(SimpAttributesContextHolder.getAttributes(), nullValue()); }
@Test public void setAttributes() { SimpAttributesContextHolder.setAttributes(this.simpAttributes); assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes)); SimpAttributesContextHolder.setAttributes(null); assertThat(SimpAttributesContextHolder.getAttributes(), nullValue()); }
@Test public void currentAttributes() { SimpAttributesContextHolder.setAttributes(this.simpAttributes); assertThat(SimpAttributesContextHolder.currentAttributes(), sameInstance(this.simpAttributes)); }
/** * Bind the given SimpAttributes to the current thread. * @param attributes the RequestAttributes to expose */ public static void setAttributes(@Nullable SimpAttributes attributes) { if (attributes != null) { attributesHolder.set(attributes); } else { resetAttributes(); } }
@Test public void setAttributesFromMessage() { String sessionId = "session1"; ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId(sessionId); headerAccessor.setSessionAttributes(map); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); SimpAttributes attrs = SimpAttributesContextHolder.getAttributes(); assertThat(attrs, notNullValue()); assertThat(attrs.getSessionId(), is(sessionId)); attrs.setAttribute("name1", "value1"); assertThat(map.get("name1"), is("value1")); }
/** * Extract the SiMP session attributes from the given message, wrap them in * a {@link SimpAttributes} instance and bind it to the current thread. * @param message the message to extract session attributes from */ public static void setAttributesFromMessage(Message<?> message) { setAttributes(SimpAttributes.fromMessage(message)); }
@Test public void setAttributesFromMessageWithMissingSessionId() { this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage(startsWith("No session id in")); SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>("")); }
/** * Return the SimpAttributes currently bound to the thread or raise an * {@link java.lang.IllegalStateException} if none are bound. * @return the attributes, never {@code null} * @throws java.lang.IllegalStateException if attributes are not bound */ public static SimpAttributes currentAttributes() throws IllegalStateException { SimpAttributes attributes = getAttributes(); if (attributes == null) { throw new IllegalStateException("No thread-bound SimpAttributes found. " + "Your code is probably not processing a client message and executing in " + "message-handling methods invoked by the SimpAnnotationMethodMessageHandler?"); } return attributes; }
@After public void tearDown() { SimpAttributesContextHolder.resetAttributes(); }
@Before public void setUp() { this.scope = new SimpSessionScope(); this.objectFactory = Mockito.mock(ObjectFactory.class); this.simpAttributes = new SimpAttributes("session1", new ConcurrentHashMap<>()); SimpAttributesContextHolder.setAttributes(this.simpAttributes); }
@Test public void getAttributes() { assertThat(SimpAttributesContextHolder.getAttributes(), nullValue()); SimpAttributesContextHolder.setAttributes(this.simpAttributes); assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes)); }
@Test public void setAttributesFromMessageWithMissingSessionAttributes() { this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage(startsWith("No session attributes in")); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId("session1"); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); }
/** * Return the SimpAttributes currently bound to the thread or raise an * {@link java.lang.IllegalStateException} if none are bound. * @return the attributes, never {@code null} * @throws java.lang.IllegalStateException if attributes are not bound */ public static SimpAttributes currentAttributes() throws IllegalStateException { SimpAttributes attributes = getAttributes(); if (attributes == null) { throw new IllegalStateException("No thread-bound SimpAttributes found. " + "Your code is probably not processing a client message and executing in " + "message-handling methods invoked by the SimpAnnotationMethodMessageHandler?"); } return attributes; }
SimpAttributesContextHolder.setAttributesFromMessage(message); boolean sent = outputChannel.send(message); SimpAttributesContextHolder.resetAttributes();
try { SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes()); SimpAttributesContextHolder.setAttributes(simpAttributes); Principal user = getUser(session); publishEvent(this.eventPublisher, new SessionConnectedEvent(this, (Message<byte[]>) message, user)); SimpAttributesContextHolder.resetAttributes();
@Override public String getConversationId() { return SimpAttributesContextHolder.currentAttributes().getSessionId(); }
@After public void tearDown() { SimpAttributesContextHolder.resetAttributes(); }