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

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#setHeader() . 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: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleMessageToClientWithSimpDisconnectAckAndReceipt() {

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
	accessor.setReceipt("message-123");
	Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());

	SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
	ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage);
	Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(this.session, ackMessage);

	assertEquals(1, this.session.getSentMessages().size());
	TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
	assertEquals("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000", actual.getPayload());
}
 
Example 2
Source File: StompSubProtocolHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handleMessageToClientWithSimpDisconnectAckAndReceipt() {

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
	accessor.setReceipt("message-123");
	Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());

	SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
	ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage);
	Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(this.session, ackMessage);

	assertEquals(1, this.session.getSentMessages().size());
	TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
	assertEquals("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000", actual.getPayload());
}
 
Example 3
Source File: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
	accessor.setHeartbeat(10000, 10000);
	accessor.setAcceptVersion("1.0");
	Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());

	SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
	ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
	Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(this.session, ackMessage);

	assertEquals(1, this.session.getSentMessages().size());
	TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
	assertEquals("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" +
			"user-name:joe\n" + "\n" + "\u0000", actual.getPayload());
}
 
Example 4
Source File: SendToMethodReturnValueHandlerTests.java    From spring-analysis-note 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 5
Source File: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleMessageToClientWithSimpConnectAck() {

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
	accessor.setHeartbeat(10000, 10000);
	accessor.setAcceptVersion("1.0,1.1,1.2");
	Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());

	SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
	ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
	ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] {15000, 15000});
	Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(this.session, ackMessage);

	assertEquals(1, this.session.getSentMessages().size());
	TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
	assertEquals("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" +
			"user-name:joe\n" + "\n" + "\u0000", actual.getPayload());
}
 
Example 6
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> createMessage(String destination, Map<String, Object> headers) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	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 7
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
public Message<?> preHandle(Message<?> message) throws MessagingException {
	String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	if (!getBroadcastDestination().equals(destination)) {
		return message;
	}
	SimpMessageHeaderAccessor accessor =
			SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
	Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
	if (accessor.getSessionId() == null) {
		// Our own broadcast
		return null;
	}
	destination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
	if (logger.isTraceEnabled()) {
		logger.trace("Checking unresolved user destination: " + destination);
	}
	SimpMessageHeaderAccessor newAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	for (String name : accessor.toNativeHeaderMap().keySet()) {
		if (NO_COPY_LIST.contains(name)) {
			continue;
		}
		newAccessor.setNativeHeader(name, accessor.getFirstNativeHeader(name));
	}
	if (destination != null) {
		newAccessor.setDestination(destination);
	}
	newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
	return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}
 
Example 8
Source File: MessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 9
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 10
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 11
Source File: UserRegistryMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void run() {
	try {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true);
		accessor.setLeaveMutable(true);
		Object payload = userRegistry.getLocalRegistryDto();
		brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders());
	}
	finally {
		userRegistry.purgeExpiredRegistries();
	}
}
 
Example 12
Source File: SendToMethodReturnValueHandlerTests.java    From spring-analysis-note 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 13
Source File: MessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 14
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 15
Source File: UserRegistryMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true);
		accessor.setLeaveMutable(true);
		Object payload = userRegistry.getLocalRegistryDto();
		brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders());
	}
	finally {
		userRegistry.purgeExpiredRegistries();
	}
}
 
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: OrderedMessageSenderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void test() throws InterruptedException {

	int start = 1;
	int end = 1000;

	AtomicInteger index = new AtomicInteger(start);
	AtomicReference<Object> result = new AtomicReference<>();
	CountDownLatch latch = new CountDownLatch(1);

	this.channel.subscribe(message -> {
		int expected = index.getAndIncrement();
		Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1);
		if (actual != expected) {
			result.set("Expected: " + expected + ", but was: " + actual);
			latch.countDown();
			return;
		}
		if (actual == 100 || actual == 200) {
			try {
				Thread.sleep(200);
			}
			catch (InterruptedException ex) {
				result.set(ex.toString());
				latch.countDown();
			}
		}
		if (actual == end) {
			result.set("Done");
			latch.countDown();
		}
	});

	for (int i = start; i <= end; i++) {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader("seq", i);
		accessor.setLeaveMutable(true);
		this.sender.send(MessageBuilder.createMessage("payload", accessor.getMessageHeaders()));
	}

	latch.await(10, TimeUnit.SECONDS);
	assertEquals("Done", result.get());
}
 
Example 18
Source File: OrderedMessageSender.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void addCompletionCallback(Message<?> msg) {
	SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(msg, SimpMessageHeaderAccessor.class);
	Assert.isTrue(accessor != null && accessor.isMutable(), "Expected mutable SimpMessageHeaderAccessor");
	accessor.setHeader(COMPLETION_TASK_HEADER, (Runnable) this::sendNextMessage);
}
 
Example 19
Source File: OrderedMessageSender.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void addCompletionCallback(Message<?> msg) {
	SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(msg, SimpMessageHeaderAccessor.class);
	Assert.isTrue(accessor != null && accessor.isMutable(), "Expected mutable SimpMessageHeaderAccessor");
	accessor.setHeader(COMPLETION_TASK_HEADER, (Runnable) this::sendNextMessage);
}
 
Example 20
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Message<String> createConnectMessage(String sessionId, long[] heartbeat) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
	accessor.setSessionId(sessionId);
	accessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, heartbeat);
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}