提交 5e925ac0 编写于 作者: R Rossen Stoyanchev

Rename header for "orig" destination

The UserDestinationMessageHandler adds a header providing a hint for
what the original destination a user may have used when subscribing.
That is then used when writing messages back to WebSocket clients to
ensure they dont see the internally used, transformed user destination.

This change moves the header name constatn to make it more broadly
applicable. For example SPR-11645.
上级 ec0b672b
......@@ -52,6 +52,16 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
public static final String USER_HEADER = "simpUser";
/**
* For internal use.
*
* <p>The original destination used by a client when subscribing. Such a
* destination may have been modified (e.g. user destinations) on the server
* side. This header provides a hint so messages sent to clients may have
* a destination matching to their original subscription.
*/
public static final String ORIGINAL_DESTINATION = "simpOrigDestination";
/**
* A constructor for creating new message headers.
......@@ -100,6 +110,9 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
}
}
/**
* @return the message type
*/
public SimpMessageType getMessageType() {
return (SimpMessageType) getHeader(MESSAGE_TYPE_HEADER);
}
......@@ -109,10 +122,16 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
setHeader(DESTINATION_HEADER, destination);
}
/**
* @return the message destination
*/
public String getDestination() {
return (String) getHeader(DESTINATION_HEADER);
}
/**
* @return the subscription id (if any) of the message
*/
public String getSubscriptionId() {
return (String) getHeader(SUBSCRIPTION_ID_HEADER);
}
......@@ -121,6 +140,9 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
setHeader(SUBSCRIPTION_ID_HEADER, subscriptionId);
}
/**
* @return the id of the current session
*/
public String getSessionId() {
return (String) getHeader(SESSION_ID_HEADER);
}
......@@ -129,6 +151,9 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
setHeader(SESSION_ID_HEADER, sessionId);
}
/**
* @return attributes associated with the current session.
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getSessionAttributes() {
return (Map<String, Object>) getHeader(SESSION_ATTRIBUTES);
......@@ -138,6 +163,9 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
setHeader(SESSION_ATTRIBUTES, attributes);
}
/**
* @return the user associated with the current session.
*/
public Principal getUser() {
return (Principal) getHeader(USER_HEADER);
}
......
......@@ -45,8 +45,6 @@ import org.springframework.util.Assert;
*/
public class UserDestinationMessageHandler implements MessageHandler, SmartLifecycle {
public static final String SUBSCRIBE_DESTINATION = "subscribeDestination";
private static final Log logger = LogFactory.getLog(UserDestinationMessageHandler.class);
......@@ -156,7 +154,7 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
}
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);
if (SimpMessageType.MESSAGE.equals(headerAccessor.getMessageType())) {
headerAccessor.setHeader(SUBSCRIBE_DESTINATION, result.getSubscribeDestination());
headerAccessor.setHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headerAccessor).build();
}
for (String targetDestination : destinations) {
......
......@@ -96,7 +96,7 @@ public class UserDestinationMessageHandlerTests {
assertEquals("/queue/foo-user123",
captor.getValue().getHeaders().get(SimpMessageHeaderAccessor.DESTINATION_HEADER));
assertEquals("/user/queue/foo",
captor.getValue().getHeaders().get(UserDestinationMessageHandler.SUBSCRIBE_DESTINATION));
captor.getValue().getHeaders().get(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION));
}
......
......@@ -33,6 +33,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.stomp.BufferingStompDecoder;
import org.springframework.messaging.simp.stomp.StompCommand;
......@@ -251,7 +252,7 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
logger.error("Ignoring message, no subscriptionId header: " + message);
return;
}
String header = UserDestinationMessageHandler.SUBSCRIBE_DESTINATION;
String header = SimpMessageHeaderAccessor.ORIGINAL_DESTINATION;
if (message.getHeaders().containsKey(header)) {
headers.setDestination((String) message.getHeaders().get(header));
}
......
......@@ -240,7 +240,7 @@ public class StompSubProtocolHandlerTests {
headers.setMessageId("mess0");
headers.setSubscriptionId("sub0");
headers.setDestination("/queue/foo-user123");
headers.setHeader(UserDestinationMessageHandler.SUBSCRIBE_DESTINATION, "/user/queue/foo");
headers.setHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo");
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
this.protocolHandler.handleMessageToClient(this.session, message);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册