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

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#create() . 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: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void registerSubscriptionWithSelectorNotSupported() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.setSelectorHeaderName(null);
	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bazz");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));
}
 
Example 2
Source File: SimpleBrokerMessageHandlerTests.java    From java-technology-stack with MIT License 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 3
Source File: StompSubProtocolHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleMessageToClientWithSimpHeartbeat() {

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
	accessor.setSessionId("s1");
	accessor.setUser(new TestPrincipal("joe"));
	Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(this.session, ackMessage);

	assertEquals(1, this.session.getSentMessages().size());
	TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
	assertEquals("\n", actual.getPayload());
}
 
Example 4
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 5
Source File: SimpleBrokerMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<String> createSubscriptionMessage(String sessionId, String subscriptionId, String destination) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	headers.setSubscriptionId(subscriptionId);
	headers.setDestination(destination);
	headers.setSessionId(sessionId);
	return MessageBuilder.createMessage("", headers.getMessageHeaders());
}
 
Example 6
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack 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 7
Source File: SimpleBrokerMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<String> createSubscriptionMessage(String sessionId, String subcriptionId, String destination) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	headers.setSubscriptionId(subcriptionId);
	headers.setDestination(destination);
	headers.setSessionId(sessionId);
	return MessageBuilder.createMessage("", headers.getMessageHeaders());
}
 
Example 8
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 9
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 10
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 11
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 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 12
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 13
Source File: StompHeaderAccessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toNativeHeadersContentType() {

	SimpMessageHeaderAccessor simpHeaderAccessor = SimpMessageHeaderAccessor.create();
	simpHeaderAccessor.setContentType(MimeTypeUtils.APPLICATION_ATOM_XML);
	Message<byte[]> message = MessageBuilder.createMessage(new byte[0], simpHeaderAccessor.getMessageHeaders());

	StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
	Map<String, List<String>> map = stompHeaderAccessor.toNativeHeaderMap();

	assertEquals("application/atom+xml", map.get(StompHeaderAccessor.STOMP_CONTENT_TYPE_HEADER).get(0));
}
 
Example 14
Source File: UserRegistryMessageHandler.java    From spring-analysis-note 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 15
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 16
Source File: MessageConverterTests.java    From spring4-understanding with Apache License 2.0 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 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: OrderedMessageSenderTests.java    From java-technology-stack 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 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: 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());
}