List of usage examples for org.springframework.messaging.simp SimpAttributes SimpAttributes
public SimpAttributes(String sessionId, Map<String, Object> attributes)
From source file:org.springframework.messaging.simp.SimpAttributes.java
/** * Extract the SiMP session attributes from the given message and * wrap them in a {@link SimpAttributes} instance. * @param message the message to extract session attributes from *//*from w ww .j a v a 2 s . c om*/ public static SimpAttributes fromMessage(Message<?> message) { Assert.notNull(message, "Message must not be null"); MessageHeaders headers = message.getHeaders(); String sessionId = SimpMessageHeaderAccessor.getSessionId(headers); Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor.getSessionAttributes(headers); if (sessionId == null) { throw new IllegalStateException("No session id in " + message); } if (sessionAttributes == null) { throw new IllegalStateException("No session attributes in " + message); } return new SimpAttributes(sessionId, sessionAttributes); }
From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java
/** * Handle STOMP messages going back out to WebSocket clients. *///from w w w . j a va 2 s . c om @Override @SuppressWarnings("unchecked") public void handleMessageToClient(WebSocketSession session, Message<?> message) { if (!(message.getPayload() instanceof byte[])) { if (logger.isErrorEnabled()) { logger.error("Expected byte[] payload. Ignoring " + message + "."); } return; } StompHeaderAccessor accessor = getStompHeaderAccessor(message); StompCommand command = accessor.getCommand(); if (StompCommand.MESSAGE.equals(command)) { if (accessor.getSubscriptionId() == null && logger.isWarnEnabled()) { logger.warn("No STOMP \"subscription\" header in " + message); } String origDestination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION); if (origDestination != null) { accessor = toMutableAccessor(accessor, message); accessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION); accessor.setDestination(origDestination); } } else if (StompCommand.CONNECTED.equals(command)) { this.stats.incrementConnectedCount(); accessor = afterStompSessionConnected(message, accessor, session); if (this.eventPublisher != null && StompCommand.CONNECTED.equals(command)) { 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)); } finally { SimpAttributesContextHolder.resetAttributes(); } } } byte[] payload = (byte[]) message.getPayload(); if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) { Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message); if (errorMessage != null) { accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class); Assert.state(accessor != null, "No StompHeaderAccessor"); payload = errorMessage.getPayload(); } } sendToClient(session, accessor, payload); }