Java Code Examples for org.springframework.messaging.simp.stomp.StompHeaderAccessor#getMessageHeaders()

The following examples show how to use org.springframework.messaging.simp.stomp.StompHeaderAccessor#getMessageHeaders() . 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: RestTemplateXhrTransportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
	accessor.setDestination("/destination");
	MessageHeaders headers = accessor.getMessageHeaders();
	Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
	byte[] bytes = new StompEncoder().encode(message);
	TextMessage textMessage = new TextMessage(bytes);
	SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());

	String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
	ClientHttpResponse response = response(HttpStatus.OK, body);
	connect(response);

	verify(this.webSocketHandler).afterConnectionEstablished(any());
	verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
	verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
	verifyNoMoreInteractions(this.webSocketHandler);
}
 
Example 2
Source File: RestTemplateXhrTransportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
	accessor.setDestination("/destination");
	MessageHeaders headers = accessor.getMessageHeaders();
	Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
	byte[] bytes = new StompEncoder().encode(message);
	TextMessage textMessage = new TextMessage(bytes);
	SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());

	String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
	ClientHttpResponse response = response(HttpStatus.OK, body);
	connect(response);

	verify(this.webSocketHandler).afterConnectionEstablished(any());
	verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
	verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
	verifyNoMoreInteractions(this.webSocketHandler);
}
 
Example 3
Source File: RestTemplateXhrTransportTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
	accessor.setDestination("/destination");
	MessageHeaders headers = accessor.getMessageHeaders();
	Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(Charset.forName("UTF-8")), headers);
	byte[] bytes = new StompEncoder().encode(message);
	TextMessage textMessage = new TextMessage(bytes);
	SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());

	String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
	ClientHttpResponse response = response(HttpStatus.OK, body);
	connect(response);

	verify(this.webSocketHandler).afterConnectionEstablished(any());
	verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
	verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
	verifyNoMoreInteractions(this.webSocketHandler);
}
 
Example 4
Source File: StompSubProtocolErrorHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleClientMessageProcessingErrorWithReceipt() throws Exception {

	String receiptId = "123";
	StompHeaderAccessor clientHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
	clientHeaderAccessor.setReceipt(receiptId);
	MessageHeaders clientHeaders = clientHeaderAccessor.getMessageHeaders();
	Message<byte[]> clientMessage = MessageBuilder.createMessage(new byte[0], clientHeaders);
	Message<byte[]> actual = this.handler.handleClientMessageProcessingError(clientMessage, new Exception());

	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertEquals(receiptId, accessor.getReceiptId());
}
 
Example 5
Source File: WeEventStompCommand.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public String encodeRaw(StompHeaderAccessor accessor, byte[] payload) {
    log.debug("encode STOMP command: {}", accessor.getCommand());

    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage(payload != null ? payload : "".getBytes(StandardCharsets.UTF_8), headers);
    return new String((new StompEncoder()).encode(message), StandardCharsets.UTF_8);
}
 
Example 6
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private void sendSimpleMessage(WebSocketSession session, StompHeaderAccessor accessor) {
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message1 = MessageBuilder.createMessage("".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message1);
    TextMessage textMessage = new TextMessage(bytes);
    send2Remote(session, textMessage);
}
 
Example 7
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
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 8
Source File: StompSubProtocolErrorHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleClientMessageProcessingErrorWithReceipt() throws Exception {

	String receiptId = "123";
	StompHeaderAccessor clientHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
	clientHeaderAccessor.setReceipt(receiptId);
	MessageHeaders clientHeaders = clientHeaderAccessor.getMessageHeaders();
	Message<byte[]> clientMessage = MessageBuilder.createMessage(new byte[0], clientHeaders);
	Message<byte[]> actual = this.handler.handleClientMessageProcessingError(clientMessage, new Exception());

	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertEquals(receiptId, accessor.getReceiptId());
}
 
Example 9
Source File: StompSubProtocolErrorHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handleClientMessageProcessingErrorWithReceipt() throws Exception {

	String receiptId = "123";
	StompHeaderAccessor clientHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
	clientHeaderAccessor.setReceipt(receiptId);
	MessageHeaders clientHeaders = clientHeaderAccessor.getMessageHeaders();
	Message<byte[]> clientMessage = MessageBuilder.createMessage(new byte[0], clientHeaders);
	Message<byte[]> actual = this.handler.handleClientMessageProcessingError(clientMessage, new Exception());

	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertEquals(receiptId, accessor.getReceiptId());
}