Java Code Examples for org.springframework.messaging.simp.SimpMessageHeaderAccessor#setUser()
The following examples show how to use
org.springframework.messaging.simp.SimpMessageHeaderAccessor#setUser() .
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: SimpleBrokerMessageHandler.java From spring-analysis-note with MIT License | 6 votes |
@Override public void run() { long now = System.currentTimeMillis(); for (SessionInfo info : sessions.values()) { if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) { handleDisconnect(info.getSessionId(), info.getUser(), null); } if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT); accessor.setSessionId(info.getSessionId()); Principal user = info.getUser(); if (user != null) { accessor.setUser(user); } initHeaders(accessor); accessor.setLeaveMutable(true); MessageHeaders headers = accessor.getMessageHeaders(); info.getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)); } } }
Example 2
Source File: SimpleBrokerMessageHandler.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public void run() { long now = System.currentTimeMillis(); for (SessionInfo info : sessions.values()) { if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) { handleDisconnect(info.getSessiondId(), info.getUser()); } if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT); accessor.setSessionId(info.getSessiondId()); accessor.setUser(info.getUser()); initHeaders(accessor); MessageHeaders headers = accessor.getMessageHeaders(); getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)); } } }
Example 3
Source File: SimpleBrokerMessageHandler.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void handleDisconnect(String sessionId, Principal user) { this.sessions.remove(sessionId); this.subscriptionRegistry.unregisterAllSubscriptions(sessionId); SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK); accessor.setSessionId(sessionId); accessor.setUser(user); initHeaders(accessor); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); getClientOutboundChannel().send(message); }
Example 4
Source File: DefaultUserDestinationResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 5
Source File: StompSubProtocolHandlerTests.java From java-technology-stack with MIT License | 5 votes |
@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 6
Source File: SimpleBrokerMessageHandlerTests.java From java-technology-stack with MIT License | 5 votes |
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 7
Source File: SimpleBrokerMessageHandlerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void subscribeDisconnectPublish() { String sess1 = "sess1"; String sess2 = "sess2"; startSession(sess1); startSession(sess2); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar")); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT); headers.setSessionId(sess1); headers.setUser(new TestPrincipal("joe")); Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()); this.messageHandler.handleMessage(message); this.messageHandler.handleMessage(createMessage("/foo", "message1")); this.messageHandler.handleMessage(createMessage("/bar", "message2")); verify(this.clientOutChannel, times(4)).send(this.messageCaptor.capture()); Message<?> captured = this.messageCaptor.getAllValues().get(2); assertEquals(SimpMessageType.DISCONNECT_ACK, SimpMessageHeaderAccessor.getMessageType(captured.getHeaders())); assertSame(message, captured.getHeaders().get(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER)); assertEquals(sess1, SimpMessageHeaderAccessor.getSessionId(captured.getHeaders())); assertEquals("joe", SimpMessageHeaderAccessor.getUser(captured.getHeaders()).getName()); assertTrue(messageCaptured(sess2, "sub1", "/foo")); assertTrue(messageCaptured(sess2, "sub2", "/foo")); assertTrue(messageCaptured(sess2, "sub3", "/bar")); }
Example 8
Source File: UserDestinationMessageHandlerTests.java From java-technology-stack with MIT License | 5 votes |
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 9
Source File: SimpleBrokerMessageHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 10
Source File: SubscriptionMethodReturnValueHandlerTests.java From java-technology-stack with MIT License | 5 votes |
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: SendToMethodReturnValueHandlerTests.java From java-technology-stack with MIT License | 5 votes |
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 12
Source File: SubscriptionMethodReturnValueHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 13
Source File: SimpleBrokerMessageHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void subcribeDisconnectPublish() { String sess1 = "sess1"; String sess2 = "sess2"; this.messageHandler.start(); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo")); this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar")); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT); headers.setSessionId(sess1); headers.setUser(new TestPrincipal("joe")); Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()); this.messageHandler.handleMessage(message); this.messageHandler.handleMessage(createMessage("/foo", "message1")); this.messageHandler.handleMessage(createMessage("/bar", "message2")); verify(this.clientOutboundChannel, times(4)).send(this.messageCaptor.capture()); Message<?> captured = this.messageCaptor.getAllValues().get(0); assertEquals(SimpMessageType.DISCONNECT_ACK, SimpMessageHeaderAccessor.getMessageType(captured.getHeaders())); assertEquals(sess1, SimpMessageHeaderAccessor.getSessionId(captured.getHeaders())); assertEquals("joe", SimpMessageHeaderAccessor.getUser(captured.getHeaders()).getName()); assertTrue(messageCaptured(sess2, "sub1", "/foo")); assertTrue(messageCaptured(sess2, "sub2", "/foo")); assertTrue(messageCaptured(sess2, "sub3", "/bar")); }
Example 14
Source File: StompSubProtocolHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
@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 15
Source File: SimpleBrokerMessageHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
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 16
Source File: SendToMethodReturnValueHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 17
Source File: UserDestinationMessageHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 18
Source File: DefaultUserDestinationResolverTests.java From spring-analysis-note with MIT License | 5 votes |
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 19
Source File: SendToMethodReturnValueHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
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 20
Source File: SimpleBrokerMessageHandler.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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); } }