org.springframework.messaging.simp.SimpAttributes Java Examples

The following examples show how to use org.springframework.messaging.simp.SimpAttributes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StompSubProtocolHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
	}
}
 
Example #2
Source File: StompSubProtocolHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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();
	}
}
 
Example #3
Source File: StompSubProtocolHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) {
	this.decoders.remove(session.getId());

	Principal principal = session.getPrincipal();
	if (principal != null && this.userSessionRegistry != null) {
		String userName = getSessionRegistryUserName(principal);
		this.userSessionRegistry.unregisterSessionId(userName, session.getId());
	}

	Message<byte[]> message = createDisconnectMessage(session);
	SimpAttributes simpAttributes = SimpAttributes.fromMessage(message);
	try {
		SimpAttributesContextHolder.setAttributes(simpAttributes);
		if (this.eventPublisher != null) {
			Principal user = session.getPrincipal();
			publishEvent(new SessionDisconnectEvent(this, message, session.getId(), closeStatus, user));
		}
		outputChannel.send(message);
	}
	finally {
		SimpAttributesContextHolder.resetAttributes();
		simpAttributes.sessionCompleted();
	}
}
 
Example #4
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@MessageMapping("/scope")
public void scope() {
	SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
	assertThat(simpAttributes.getAttribute("name"), is("value"));
	this.method = "scope";
}
 
Example #5
Source File: StompSubProtocolHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Handle STOMP messages going back out to WebSocket clients.
 */
@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) {
			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);
}
 
Example #6
Source File: SimpAnnotationMethodMessageHandlerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@MessageMapping("/scope")
public void scope() {
	SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
	assertThat(simpAttributes.getAttribute("name"), is("value"));
	this.method = "scope";
}
 
Example #7
Source File: StompSubProtocolHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Handle STOMP messages going back out to WebSocket clients.
 */
@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) {
			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);
}
 
Example #8
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@MessageMapping("/scope")
public void scope() {
	SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
	assertThat(simpAttributes.getAttribute("name"), is("value"));
	this.method = "scope";
}
 
Example #9
Source File: StompSubProtocolHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Handle STOMP messages going back out to WebSocket clients.
 */
@Override
@SuppressWarnings("unchecked")
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
	if (!(message.getPayload() instanceof byte[])) {
		logger.error("Expected byte[] payload. Ignoring " + message + ".");
		return;
	}

	StompHeaderAccessor stompAccessor = getStompHeaderAccessor(message);
	StompCommand command = stompAccessor.getCommand();

	if (StompCommand.MESSAGE.equals(command)) {
		if (stompAccessor.getSubscriptionId() == null) {
			logger.warn("No STOMP \"subscription\" header in " + message);
		}
		String origDestination = stompAccessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
		if (origDestination != null) {
			stompAccessor = toMutableAccessor(stompAccessor, message);
			stompAccessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
			stompAccessor.setDestination(origDestination);
		}
	}
	else if (StompCommand.CONNECTED.equals(command)) {
		this.stats.incrementConnectedCount();
		stompAccessor = afterStompSessionConnected(message, stompAccessor, session);
		if (this.eventPublisher != null && StompCommand.CONNECTED.equals(command)) {
			try {
				SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes());
				SimpAttributesContextHolder.setAttributes(simpAttributes);
				Principal user = session.getPrincipal();
				publishEvent(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);
		stompAccessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
		Assert.notNull(stompAccessor, "Expected STOMP headers");
		payload = errorMessage.getPayload();
	}

	sendToClient(session, stompAccessor, payload);
}