Java Code Examples for org.springframework.messaging.simp.stomp.StompHeaderAccessor#setNativeHeader()
The following examples show how to use
org.springframework.messaging.simp.stomp.StompHeaderAccessor#setNativeHeader() .
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: UserDestinationMessageHandlerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void handleMessageFromBrokerWithoutActiveSession() { this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); // No re-broadcast verifyNoMoreInteractions(this.brokerChannel); }
Example 2
Source File: StompSubProtocolHandlerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void handleMessageToClientWithUserDestination() { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE); headers.setMessageId("mess0"); headers.setSubscriptionId("sub0"); headers.setDestination("/queue/foo-user123"); headers.setNativeHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo"); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertTrue(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n")); assertFalse(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION)); }
Example 3
Source File: StompSubProtocolHandler.java From spring4-understanding with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor, WebSocketSession session) { Principal principal = session.getPrincipal(); if (principal != null) { accessor = toMutableAccessor(accessor, message); accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName()); if (this.userSessionRegistry != null) { String userName = getSessionRegistryUserName(principal); this.userSessionRegistry.registerSessionId(userName, session.getId()); } } long[] heartbeat = accessor.getHeartbeat(); if (heartbeat[1] > 0) { session = WebSocketSessionDecorator.unwrap(session); if (session instanceof SockJsSession) { ((SockJsSession) session).disableHeartbeat(); } } return accessor; }
Example 4
Source File: UserDestinationMessageHandlerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void handleMessageFromBrokerWithoutActiveSession() { this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(Charset.forName("UTF-8")); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); // No re-broadcast verifyNoMoreInteractions(this.brokerChannel); }
Example 5
Source File: StompSubProtocolHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleMessageToClientWithUserDestination() { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE); headers.setMessageId("mess0"); headers.setSubscriptionId("sub0"); headers.setDestination("/queue/foo-user123"); headers.setNativeHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo"); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertTrue(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n")); assertFalse(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION)); }
Example 6
Source File: StompSubProtocolHandler.java From java-technology-stack with MIT License | 6 votes |
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor, WebSocketSession session) { Principal principal = getUser(session); if (principal != null) { accessor = toMutableAccessor(accessor, message); accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName()); } long[] heartbeat = accessor.getHeartbeat(); if (heartbeat[1] > 0) { session = WebSocketSessionDecorator.unwrap(session); if (session instanceof SockJsSession) { ((SockJsSession) session).disableHeartbeat(); } } return accessor; }
Example 7
Source File: UserDestinationMessageHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleMessageFromBrokerWithoutActiveSession() { this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); // No re-broadcast verifyNoMoreInteractions(this.brokerChannel); }
Example 8
Source File: WeEventStompCommand.java From WeEvent with Apache License 2.0 | 6 votes |
public String encodeSubscribe(TopicContent topic, Long id) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE); accessor.setDestination(topic.getTopicName()); accessor.setNativeHeader("eventId", topic.getOffset()); accessor.setNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER, Long.toString(id)); if (!StringUtils.isBlank(topic.getGroupId())) { accessor.setNativeHeader("groupId", topic.getGroupId()); } // keep the original SubscriptionId if (topic.getExtension().containsKey(WeEvent.WeEvent_SubscriptionId)) { accessor.setNativeHeader(WeEvent.WeEvent_SubscriptionId, topic.getExtension().get(WeEvent.WeEvent_SubscriptionId)); } if (topic.getExtension().containsKey(WeEvent.WeEvent_TAG)) { accessor.setNativeHeader(WeEvent.WeEvent_TAG, topic.getExtension().get(WeEvent.WeEvent_TAG)); } if (topic.getExtension().containsKey(WeEvent.WeEvent_EPHEMERAL)) { accessor.setNativeHeader(WeEvent.WeEvent_EPHEMERAL, topic.getExtension().get(WeEvent.WeEvent_EPHEMERAL)); } return encodeRaw(accessor); }
Example 9
Source File: StompSubProtocolHandlerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void handleMessageToClientWithUserDestination() { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE); headers.setMessageId("mess0"); headers.setSubscriptionId("sub0"); headers.setDestination("/queue/foo-user123"); headers.setNativeHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo"); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertTrue(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n")); assertFalse(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION)); }
Example 10
Source File: StompSubProtocolHandler.java From spring-analysis-note with MIT License | 6 votes |
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor, WebSocketSession session) { Principal principal = getUser(session); if (principal != null) { accessor = toMutableAccessor(accessor, message); accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName()); } long[] heartbeat = accessor.getHeartbeat(); if (heartbeat[1] > 0) { session = WebSocketSessionDecorator.unwrap(session); if (session instanceof SockJsSession) { ((SockJsSession) session).disableHeartbeat(); } } return accessor; }
Example 11
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 5 votes |
private void handleSendMessage(StompHeaderAccessor stompHeaderAccessor, Message<byte[]> msg, WebSocketSession session) { Map<String, String> extensions = this.getWeEventExtend(msg); // group id String groupId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EVENT_GROUP_ID); if (StringUtils.isBlank(groupId)) { groupId = ""; } try { String destination = stompHeaderAccessor.getDestination(); // publish event SendResult sendResult = this.iproducer.publish(new WeEvent(destination, msg.getPayload(), extensions), groupId, this.fiscoConfig.getWeb3sdkTimeout()); // send response StompHeaderAccessor accessor; if (sendResult.getStatus().equals(SendResult.SendResultStatus.SUCCESS)) { accessor = StompHeaderAccessor.create(StompCommand.RECEIPT); accessor.setDestination(destination); accessor.setNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID, sendResult.getEventId()); } else { accessor = StompHeaderAccessor.create(StompCommand.ERROR); accessor.setMessage(sendResult.toString()); } accessor.setReceiptId(stompHeaderAccessor.getReceipt()); sendSimpleMessage(session, accessor); } catch (BrokerException e) { handleErrorMessage(session, e, stompHeaderAccessor.getReceipt()); } }
Example 12
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 5 votes |
private void handleErrorMessage(WebSocketSession session, BrokerException e, String receiptId) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR); // package the return frame ,include the error message and error code accessor.setMessage(e.getMessage()); accessor.setNativeHeader("code", String.valueOf(e.getCode())); accessor.setReceiptId(receiptId); sendSimpleMessage(session, accessor); }
Example 13
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 5 votes |
private void handleOnEvent(String headerIdStr, String subscriptionId, WeEvent event, WebSocketSession session) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); // package the return frame accessor.setSubscriptionId(headerIdStr); accessor.setNativeHeader("subscription-id", subscriptionId); accessor.setMessageId(headerIdStr); accessor.setDestination(event.getTopic()); accessor.setContentType(new MimeType("text", "plain", StandardCharsets.UTF_8)); // set custom properties in header for (Map.Entry<String, String> custom : event.getExtensions().entrySet()) { accessor.setNativeHeader(custom.getKey(), custom.getValue()); } // set eventId in header accessor.setNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID, event.getEventId()); // payload == content MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message = MessageBuilder.createMessage(event.getContent(), headers); byte[] bytes = new StompEncoder().encode(message); // send to remote send2Remote(session, new TextMessage(bytes)); }
Example 14
Source File: UserDestinationMessageHandlerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void handleMessageFromBrokerWithActiveSession() { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123")); when(this.registry.getUser("joe")).thenReturn(simpUser); this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setNativeHeader("customHeader", "customHeaderValue"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); assertNotNull(captor.getValue()); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue()); assertEquals("/queue/foo-user123", headers.getDestination()); assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION)); assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader")); assertArrayEquals(payload, (byte[]) captor.getValue().getPayload()); }
Example 15
Source File: WeEventStompCommand.java From WeEvent with Apache License 2.0 | 5 votes |
public String encodeSend(Long id, TopicContent topic, WeEvent weEvent) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND); accessor.setDestination(topic.getTopicName()); accessor.setContentType(new MimeType("text", "plain", StandardCharsets.UTF_8)); accessor.setContentLength(weEvent.getContent().length); accessor.setReceipt(Long.toString(id)); if (!StringUtils.isBlank(topic.getGroupId())) { accessor.setNativeHeader("groupId", topic.getGroupId()); } for (Map.Entry<String, String> entry : weEvent.getExtensions().entrySet()) { accessor.setNativeHeader(entry.getKey(), entry.getValue()); } return encodeRaw(accessor, weEvent.getContent()); }
Example 16
Source File: WeEventStompCommand.java From WeEvent with Apache License 2.0 | 5 votes |
public String encodeUnSubscribe(String subscriptionId, String headerId) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.UNSUBSCRIBE); accessor.setNativeHeader(StompHeaderAccessor.SUBSCRIPTION_ID_HEADER, headerId); accessor.setNativeHeader(StompHeaderAccessor.STOMP_SUBSCRIPTION_HEADER, subscriptionId); accessor.setNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER, headerId); return encodeRaw(accessor); }
Example 17
Source File: UserDestinationMessageHandlerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void handleMessageFromBrokerWithActiveSession() { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123")); when(this.registry.getUser("joe")).thenReturn(simpUser); this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setNativeHeader("customHeader", "customHeaderValue"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(Charset.forName("UTF-8")); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); assertNotNull(captor.getValue()); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue()); assertEquals("/queue/foo-user123", headers.getDestination()); assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION)); assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader")); assertArrayEquals(payload, (byte[]) captor.getValue().getPayload()); }
Example 18
Source File: UserDestinationMessageHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void handleMessageFromBrokerWithActiveSession() { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123")); given(this.registry.getUser("joe")).willReturn(simpUser); this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); accessor.setSessionId("system123"); accessor.setDestination("/topic/unresolved"); accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo"); accessor.setNativeHeader("customHeader", "customHeaderValue"); accessor.setLeaveMutable(true); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); assertNotNull(captor.getValue()); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue()); assertEquals("/queue/foo-user123", headers.getDestination()); assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION)); assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader")); assertArrayEquals(payload, (byte[]) captor.getValue().getPayload()); }
Example 19
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 4 votes |
private void handleSubscribeMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) { // subscribe command id String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER); // group String groupId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EVENT_GROUP_ID); if (StringUtils.isBlank(groupId)) { groupId = ""; } // check if there hasn't the event, need use the last id String subEventId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID); if (StringUtils.isBlank(subEventId)) { subEventId = WeEvent.OFFSET_LAST; } // subscription id String continueSubscriptionIdStr = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_SubscriptionId); if (StringUtils.isBlank(continueSubscriptionIdStr)) { continueSubscriptionIdStr = ""; } // tag String tag = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_TAG); if (StringUtils.isBlank(tag)) { tag = ""; } // is ephemeral boolean ephemeral = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_EPHEMERAL) != null; try { String simpDestination = stompHeaderAccessor.getDestination(); String subscriptionId = handleSubscribe(session, simpDestination, groupId, headerIdStr, subEventId, continueSubscriptionIdStr, tag, ephemeral); // send response StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT); accessor.setDestination(simpDestination); // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message. accessor.setReceiptId(headerIdStr); accessor.setSubscriptionId(subscriptionId); accessor.setNativeHeader("subscription-id", subscriptionId); sendSimpleMessage(session, accessor); } catch (BrokerException e) { handleErrorMessage(session, e, headerIdStr); } }