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

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#getDestination() . 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 void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 2
Source File: SubscriptionMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
	if (returnValue == null) {
		return;
	}

	MessageHeaders headers = message.getHeaders();
	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);

	if (subscriptionId == null) {
		throw new IllegalStateException(
				"No subscriptionId in " + message + " returned by: " + returnType.getMethod());
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Reply to @SubscribeMapping: " + returnValue);
	}
	this.messagingTemplate.convertAndSend(
			destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
}
 
Example 3
Source File: DefaultUserDestinationResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public UserDestinationResult resolveDestination(Message<?> message) {
	String sourceDestination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	ParseResult parseResult = parse(message);
	if (parseResult == null) {
		return null;
	}
	String user = parseResult.getUser();
	Set<String> targetSet = new HashSet<String>();
	for (String sessionId : parseResult.getSessionIds()) {
		String actualDestination = parseResult.getActualDestination();
		String targetDestination = getTargetDestination(sourceDestination, actualDestination, sessionId, user);
		if (targetDestination != null) {
			targetSet.add(targetDestination);
		}
	}
	String subscribeDestination = parseResult.getSubscribeDestination();
	return new UserDestinationResult(sourceDestination, targetSet, subscribeDestination, user);
}
 
Example 4
Source File: DefaultUserDestinationResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private ParseResult parse(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	String sourceDestination = SimpMessageHeaderAccessor.getDestination(headers);
	if (sourceDestination == null || !checkDestination(sourceDestination, this.prefix)) {
		return null;
	}
	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	if (messageType != null) {
		switch (messageType) {
			case SUBSCRIBE:
			case UNSUBSCRIBE:
				return parseSubscriptionMessage(message, sourceDestination);
			case MESSAGE:
				return parseMessage(headers, sourceDestination);
		}
	}
	return null;
}
 
Example 5
Source File: AbstractSubscriptionRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public final MultiValueMap<String, String> findSubscriptions(Message<?> message) {
	MessageHeaders headers = message.getHeaders();

	SimpMessageType type = SimpMessageHeaderAccessor.getMessageType(headers);
	if (!SimpMessageType.MESSAGE.equals(type)) {
		throw new IllegalArgumentException("Unexpected message type: " + type);
	}

	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	if (destination == null) {
		if (logger.isErrorEnabled()) {
			logger.error("No destination in " + message);
		}
		return EMPTY_MAP;
	}

	return findSubscriptionsInternal(destination, message);
}
 
Example 6
Source File: AbstractSubscriptionRegistry.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public final MultiValueMap<String, String> findSubscriptions(Message<?> message) {
	MessageHeaders headers = message.getHeaders();

	SimpMessageType type = SimpMessageHeaderAccessor.getMessageType(headers);
	if (!SimpMessageType.MESSAGE.equals(type)) {
		throw new IllegalArgumentException("Unexpected message type: " + type);
	}

	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	if (destination == null) {
		if (logger.isErrorEnabled()) {
			logger.error("No destination in " + message);
		}
		return EMPTY_MAP;
	}

	return findSubscriptionsInternal(destination, message);
}
 
Example 7
Source File: DefaultUserDestinationResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private ParseResult parse(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	String sourceDestination = SimpMessageHeaderAccessor.getDestination(headers);
	if (sourceDestination == null || !checkDestination(sourceDestination, this.prefix)) {
		return null;
	}
	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	if (messageType != null) {
		switch (messageType) {
			case SUBSCRIBE:
			case UNSUBSCRIBE:
				return parseSubscriptionMessage(message, sourceDestination);
			case MESSAGE:
				return parseMessage(headers, sourceDestination);
		}
	}
	return null;
}
 
Example 8
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
public void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(
			SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 9
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 10
Source File: DefaultSubscriptionRegistry.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) {
	Object value;
	if (target instanceof Message) {
		value = name.equals("headers") ? ((Message) target).getHeaders() : null;
	}
	else if (target instanceof MessageHeaders) {
		MessageHeaders headers = (MessageHeaders) target;
		SimpMessageHeaderAccessor accessor =
				MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
		Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
		if ("destination".equalsIgnoreCase(name)) {
			value = accessor.getDestination();
		}
		else {
			value = accessor.getFirstNativeHeader(name);
			if (value == null) {
				value = headers.get(name);
			}
		}
	}
	else {
		// Should never happen...
		throw new IllegalStateException("Expected Message or MessageHeaders.");
	}
	return new TypedValue(value);
}
 
Example 11
Source File: AbstractSubscriptionRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final void registerSubscription(Message<?> message) {
	MessageHeaders headers = message.getHeaders();

	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	if (!SimpMessageType.SUBSCRIBE.equals(messageType)) {
		throw new IllegalArgumentException("Expected SUBSCRIBE: " + message);
	}

	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	if (sessionId == null) {
		logger.error("No sessionId in  " + message);
		return;
	}

	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
	if (subscriptionId == null) {
		logger.error("No subscriptionId in " + message);
		return;
	}

	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	if (destination == null) {
		logger.error("No destination in " + message);
		return;
	}

	addSubscriptionInternal(sessionId, subscriptionId, destination, message);
}
 
Example 12
Source File: SubscriptionMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message)
		throws Exception {

	if (returnValue == null) {
		return;
	}

	MessageHeaders headers = message.getHeaders();
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
	String destination = SimpMessageHeaderAccessor.getDestination(headers);

	if (subscriptionId == null) {
		throw new IllegalStateException("No simpSubscriptionId in " + message +
				" returned by: " + returnType.getMethod());
	}
	if (destination == null) {
		throw new IllegalStateException("No simpDestination in " + message +
				" returned by: " + returnType.getMethod());
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Reply to @SubscribeMapping: " + returnValue);
	}
	MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
	this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
}
 
Example 13
Source File: DefaultSubscriptionRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) {
	Object value;
	if (target instanceof Message) {
		value = name.equals("headers") ? ((Message) target).getHeaders() : null;
	}
	else if (target instanceof MessageHeaders) {
		MessageHeaders headers = (MessageHeaders) target;
		SimpMessageHeaderAccessor accessor =
				MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
		Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
		if ("destination".equalsIgnoreCase(name)) {
			value = accessor.getDestination();
		}
		else {
			value = accessor.getFirstNativeHeader(name);
			if (value == null) {
				value = headers.get(name);
			}
		}
	}
	else {
		// Should never happen...
		throw new IllegalStateException("Expected Message or MessageHeaders.");
	}
	return new TypedValue(value);
}
 
Example 14
Source File: AbstractSubscriptionRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public final void registerSubscription(Message<?> message) {
	MessageHeaders headers = message.getHeaders();

	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	if (!SimpMessageType.SUBSCRIBE.equals(messageType)) {
		throw new IllegalArgumentException("Expected SUBSCRIBE: " + message);
	}

	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	if (sessionId == null) {
		if (logger.isErrorEnabled()) {
			logger.error("No sessionId in  " + message);
		}
		return;
	}

	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
	if (subscriptionId == null) {
		if (logger.isErrorEnabled()) {
			logger.error("No subscriptionId in " + message);
		}
		return;
	}

	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	if (destination == null) {
		if (logger.isErrorEnabled()) {
			logger.error("No destination in " + message);
		}
		return;
	}

	addSubscriptionInternal(sessionId, subscriptionId, destination, message);
}
 
Example 15
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 16
Source File: SubscriptionMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message)
		throws Exception {

	if (returnValue == null) {
		return;
	}

	MessageHeaders headers = message.getHeaders();
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
	String destination = SimpMessageHeaderAccessor.getDestination(headers);

	if (subscriptionId == null) {
		throw new IllegalStateException("No simpSubscriptionId in " + message +
				" returned by: " + returnType.getMethod());
	}
	if (destination == null) {
		throw new IllegalStateException("No simpDestination in " + message +
				" returned by: " + returnType.getMethod());
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Reply to @SubscribeMapping: " + returnValue);
	}
	MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
	this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
}
 
Example 17
Source File: SimpAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
protected String getDestination(Message<?> message) {
	return SimpMessageHeaderAccessor.getDestination(message.getHeaders());
}
 
Example 18
Source File: SimpAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Nullable
protected String getDestination(Message<?> message) {
	return SimpMessageHeaderAccessor.getDestination(message.getHeaders());
}
 
Example 19
Source File: SimpAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected String getDestination(Message<?> message) {
	return SimpMessageHeaderAccessor.getDestination(message.getHeaders());
}
 
Example 20
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);
	}
}