Java Code Examples for org.springframework.messaging.simp.SimpMessageHeaderAccessor#setSessionId()

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#setSessionId() . 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: SendToMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-12170
public void sendToWithDestinationPlaceholders() throws Exception {
	given(this.messageChannel.send(any(Message.class))).willReturn(true);

	Map<String, String> vars = new LinkedHashMap<>(1);
	vars.put("roomName", "roomA");

	String sessionId = "sess1";
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId("sub1");
	accessor.setHeader(DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
	Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders());
	this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message);

	verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());

	SimpMessageHeaderAccessor actual = getCapturedAccessor(0);
	assertEquals(sessionId, actual.getSessionId());
	assertEquals("/topic/chat.message.filtered.roomA", actual.getDestination());
}
 
Example 2
Source File: SimpleBrokerMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void run() {
	long now = System.currentTimeMillis();
	for (SessionInfo info : sessions.values()) {
		if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) {
			handleDisconnect(info.getSessionId(), info.getUser(), null);
		}
		if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) {
			SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
			accessor.setSessionId(info.getSessionId());
			Principal user = info.getUser();
			if (user != null) {
				accessor.setUser(user);
			}
			initHeaders(accessor);
			accessor.setLeaveMutable(true);
			MessageHeaders headers = accessor.getMessageHeaders();
			info.getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers));
		}
	}
}
 
Example 3
Source File: SendToMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12170
public void sendToWithDestinationPlaceholders() throws Exception {
	given(this.messageChannel.send(any(Message.class))).willReturn(true);

	Map<String, String> vars = new LinkedHashMap<>(1);
	vars.put("roomName", "roomA");

	String sessionId = "sess1";
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId("sub1");
	accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
	Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders());
	this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message);

	verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());

	SimpMessageHeaderAccessor actual = getCapturedAccessor(0);
	assertEquals(sessionId, actual.getSessionId());
	assertEquals("/topic/chat.message.filtered.roomA", actual.getDestination());
}
 
Example 4
Source File: SimpleBrokerMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	long now = System.currentTimeMillis();
	for (SessionInfo info : sessions.values()) {
		if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) {
			handleDisconnect(info.getSessiondId(), info.getUser());
		}
		if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) {
			SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
			accessor.setSessionId(info.getSessiondId());
			accessor.setUser(info.getUser());
			initHeaders(accessor);
			MessageHeaders headers = accessor.getMessageHeaders();
			getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers));
		}
	}
}
 
Example 5
Source File: SimpAnnotationMethodMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void simpScope() {
	Map<String, Object> sessionAttributes = new ConcurrentHashMap<>();
	sessionAttributes.put("name", "value");

	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
	headers.setSessionId("session1");
	headers.setSessionAttributes(sessionAttributes);
	headers.setDestination("/pre/scope");
	Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
	this.messageHandler.registerHandler(this.testController);
	this.messageHandler.handleMessage(message);

	assertEquals("scope", this.testController.method);
}
 
Example 6
Source File: SendToMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private MessageHeaders createHeaders(String sessionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(headerAccessor);
	}
	if (sessionId != null) {
		headerAccessor.setSessionId(sessionId);
	}
	headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	headerAccessor.setLeaveMutable(true);
	return headerAccessor.getMessageHeaders();
}
 
Example 7
Source File: WebSocketAnnotationMethodMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void globalException() throws Exception {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
	headers.setSessionId("session1");
	headers.setSessionAttributes(new ConcurrentHashMap<>());
	headers.setDestination("/exception");
	Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
	this.messageHandler.handleMessage(message);

	TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class);
	assertTrue(controllerAdvice.isExceptionHandled());
}
 
Example 8
Source File: DefaultSubscriptionRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest, String selector) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	if (dest != null) {
		accessor.setDestination(dest);
	}
	if (selector != null) {
		accessor.setNativeHeader("selector", selector);
	}
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 9
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest, String selector) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	if (dest != null) {
		accessor.setDestination(dest);
	}
	if (selector != null) {
		accessor.setNativeHeader("selector", selector);
	}
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 10
Source File: SendToMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<?> createMessage(String sessId, String subsId, String destPrefix, String dest, Principal user) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
	headerAccessor.setSessionId(sessId);
	headerAccessor.setSubscriptionId(subsId);
	if (dest != null && destPrefix != null) {
		headerAccessor.setDestination(destPrefix + dest);
		headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, dest);
	}
	if (user != null) {
		headerAccessor.setUser(user);
	}
	return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
 
Example 11
Source File: WebSocketAnnotationMethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void globalException() throws Exception {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
	headers.setSessionId("session1");
	headers.setSessionAttributes(new ConcurrentHashMap<>());
	headers.setDestination("/exception");
	Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
	this.messageHandler.handleMessage(message);

	TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class);
	assertTrue(controllerAdvice.isExceptionHandled());
}
 
Example 12
Source File: DefaultUserDestinationResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<?> createMessage(SimpMessageType type, Principal user, String sessionId, String destination) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(type);
	if (destination != null) {
		headers.setDestination(destination);
	}
	if (user != null) {
		headers.setUser(user);
	}
	if (sessionId != null) {
		headers.setSessionId(sessionId);
	}
	return MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
}
 
Example 13
Source File: SimpleBrokerMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<String> createConnectMessage(String sessionId, Principal user, long[] heartbeat) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
	accessor.setSessionId(sessionId);
	accessor.setUser(user);
	accessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, heartbeat);
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 14
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> createMessage(SimpMessageType messageType, String destination, Map<String, Object> headers) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(messageType);
	accessor.setSessionId("session1");
	accessor.setSessionAttributes(new HashMap<>());
	accessor.setDestination(destination);
	if (headers != null) {
		for (Map.Entry<String, Object> entry : headers.entrySet()) {
			accessor.setHeader(entry.getKey(), entry.getValue());
		}
	}
	return MessageBuilder.withPayload(new byte[0]).setHeaders(accessor).build();
}
 
Example 15
Source File: SimpleBrokerMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void handleDisconnect(String sessionId, @Nullable Principal user, @Nullable Message<?> origMessage) {
	this.sessions.remove(sessionId);
	this.subscriptionRegistry.unregisterAllSubscriptions(sessionId);
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
	accessor.setSessionId(sessionId);
	if (user != null) {
		accessor.setUser(user);
	}
	if (origMessage != null) {
		accessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, origMessage);
	}
	initHeaders(accessor);
	Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	getClientOutboundChannel().send(message);
}
 
Example 16
Source File: SubscriptionMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(@Nullable String sessionId, String subscriptionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(accessor);
	}
	if (sessionId != null) {
		accessor.setSessionId(sessionId);
	}
	accessor.setSubscriptionId(subscriptionId);
	accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	accessor.setLeaveMutable(true);
	return accessor.getMessageHeaders();
}
 
Example 17
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private Message<?> unsubscribeMessage(String sessionId, String subscriptionId) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 18
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void testDotSeparator(ApplicationContext context, boolean expectLeadingSlash) {
	MessageChannel inChannel = context.getBean("clientInboundChannel", MessageChannel.class);
	TestChannel outChannel = context.getBean("clientOutboundChannel", TestChannel.class);
	MessageChannel brokerChannel = context.getBean("brokerChannel", MessageChannel.class);

	inChannel.send(createConnectMessage("sess1", new long[] {0,0}));

	// 1. Subscribe to user destination

	StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
	headers.setSessionId("sess1");
	headers.setSubscriptionId("subs1");
	headers.setDestination("/user/queue.q1");
	Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
	inChannel.send(message);

	// 2. Send message to user via inboundChannel

	headers = StompHeaderAccessor.create(StompCommand.SEND);
	headers.setSessionId("sess1");
	headers.setDestination("/user/sess1/queue.q1");
	message = MessageBuilder.createMessage("123".getBytes(), headers.getMessageHeaders());
	inChannel.send(message);

	assertEquals(2, outChannel.messages.size());
	Message<?> outputMessage = outChannel.messages.remove(1);
	headers = StompHeaderAccessor.wrap(outputMessage);

	assertEquals(SimpMessageType.MESSAGE, headers.getMessageType());
	assertEquals(expectLeadingSlash ? "/queue.q1-usersess1" : "queue.q1-usersess1", headers.getDestination());
	assertEquals("123", new String((byte[]) outputMessage.getPayload()));
	outChannel.messages.clear();

	// 3. Send message via broker channel

	SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel);
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setSessionId("sess1");
	template.convertAndSendToUser("sess1", "queue.q1", "456".getBytes(), accessor.getMessageHeaders());

	assertEquals(1, outChannel.messages.size());
	outputMessage = outChannel.messages.remove(0);
	headers = StompHeaderAccessor.wrap(outputMessage);

	assertEquals(SimpMessageType.MESSAGE, headers.getMessageType());
	assertEquals(expectLeadingSlash ? "/queue.q1-usersess1" : "queue.q1-usersess1", headers.getDestination());
	assertEquals("456", new String((byte[]) outputMessage.getPayload()));

}
 
Example 19
Source File: SimpleBrokerMessageHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleMessageInternal(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	Principal user = SimpMessageHeaderAccessor.getUser(headers);

	updateSessionReadTime(sessionId);

	if (!checkDestinationPrefix(destination)) {
		return;
	}

	SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
	if (accessor == null) {
		throw new IllegalStateException(
				"No header accessor (not using the SimpMessagingTemplate?): " + message);
	}

	if (SimpMessageType.MESSAGE.equals(messageType)) {
		logMessage(message);
		sendMessageToSubscribers(destination, message);
	}
	else if (SimpMessageType.CONNECT.equals(messageType)) {
		logMessage(message);
		long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers);
		long[] serverHeartbeat = getHeartbeatValue();
		this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat));
		SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
		initHeaders(connectAck);
		connectAck.setSessionId(sessionId);
		connectAck.setUser(SimpMessageHeaderAccessor.getUser(headers));
		connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
		connectAck.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, serverHeartbeat);
		Message<byte[]> messageOut = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
		getClientOutboundChannel().send(messageOut);
	}
	else if (SimpMessageType.DISCONNECT.equals(messageType)) {
		logMessage(message);
		handleDisconnect(sessionId, user);
	}
	else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
		logMessage(message);
		this.subscriptionRegistry.registerSubscription(message);
	}
	else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
		logMessage(message);
		this.subscriptionRegistry.unregisterSubscription(message);
	}
}
 
Example 20
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Message<?> unsubscribeMessage(String sessionId, String subscriptionId) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}