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

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#setDestination() . 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: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public Message<?> preHandle(Message<?> message) throws MessagingException {
	String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	if (!getBroadcastDestination().equals(destination)) {
		return message;
	}
	SimpMessageHeaderAccessor accessor = getAccessor(message, SimpMessageHeaderAccessor.class);
	if (accessor.getSessionId() == null) {
		// Our own broadcast
		return null;
	}
	destination = accessor.getFirstNativeHeader(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));
	}
	newAccessor.setDestination(destination);
	newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
	return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}
 
Example 2
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack 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 3
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 4
Source File: UserDestinationMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> createWith(SimpMessageType type, String user, String sessionId, String destination) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(type);
	if (destination != null) {
		headers.setDestination(destination);
	}
	if (user != null) {
		headers.setUser(new TestPrincipal(user));
	}
	if (sessionId != null) {
		headers.setSessionId(sessionId);
	}
	return MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
}
 
Example 5
Source File: DefaultSimpUserRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<byte[]> createMessage(SimpMessageType type, String sessionId, String subscriptionId,
		String destination) {

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(type);
	accessor.setSessionId(sessionId);
	if (destination != null) {
		accessor.setDestination(destination);
	}
	if (subscriptionId != null) {
		accessor.setSubscriptionId(subscriptionId);
	}
	return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
 
Example 6
Source File: SimpleBrokerMessageHandlerTests.java    From spring-analysis-note 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 7
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 8
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void registerSubscriptionWithSelector() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

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

	// First, try with selector header

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bar");
	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));

	// Then without

	actual = this.registry.findSubscriptions(createMessage(destination));
	assertNotNull(actual);
	assertEquals(0, actual.size());
}
 
Example 9
Source File: StompBrokerRelayMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void destinationExcluded() throws Exception {

	this.brokerRelay.start();

	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	headers.setSessionId("sess1");
	headers.setDestination("/user/daisy/foo");
	this.brokerRelay.handleMessage(MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()));

	assertEquals(1, this.tcpClient.getSentMessages().size());
	StompHeaderAccessor headers1 = this.tcpClient.getSentHeaders(0);
	assertEquals(StompCommand.CONNECT, headers1.getCommand());
	assertEquals(StompBrokerRelayMessageHandler.SYSTEM_SESSION_ID, headers1.getSessionId());
}
 
Example 10
Source File: SubscriptionMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> createInputMessage(String sessId, String subsId, String dest, Principal principal) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
	headers.setSessionId(sessId);
	headers.setSubscriptionId(subsId);
	headers.setDestination(dest);
	headers.setUser(principal);
	return MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
}
 
Example 11
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 12
Source File: StompBrokerRelayMessageHandlerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public static MessageExchangeBuilder send(String destination, String payload) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	headers.setDestination(destination);
	Message<?> message = MessageBuilder.createMessage(payload.getBytes(StandardCharsets.UTF_8),
			headers.getMessageHeaders());
	return new MessageExchangeBuilder(message);
}
 
Example 13
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 14
Source File: SendToMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> createInputMessage(String sessId, String subsId, String destinationPrefix,
           String destination, Principal principal) {

	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
	headerAccessor.setSessionId(sessId);
	headerAccessor.setSubscriptionId(subsId);
	if (destination != null && destinationPrefix != null) {
		headerAccessor.setDestination(destinationPrefix + destination);
		headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	}
	if (principal != null) {
		headerAccessor.setUser(principal);
	}
	return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
 
Example 15
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 16
Source File: UserDestinationMessageHandler.java    From spring-analysis-note 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 17
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Message<?> createMessage(String destination) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 18
Source File: SimpleBrokerMessageHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private Message<String> createMessage(String destination, String payload) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	headers.setDestination(destination);
	return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}
 
Example 19
Source File: SimpleBrokerMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private Message<String> createMessage(String destination, String payload) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	headers.setDestination(destination);
	return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}
 
Example 20
Source File: SimpleBrokerMessageHandlerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Message<String> createMessage(String destination, String payload) {
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	headers.setDestination(destination);
	return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}