Search in sources :

Example 16 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolErrorHandler method handleClientMessageProcessingError.

@Override
public Message<byte[]> handleClientMessageProcessingError(Message<byte[]> clientMessage, Throwable ex) {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
    accessor.setMessage(ex.getMessage());
    accessor.setLeaveMutable(true);
    StompHeaderAccessor clientHeaderAccessor = null;
    if (clientMessage != null) {
        clientHeaderAccessor = MessageHeaderAccessor.getAccessor(clientMessage, StompHeaderAccessor.class);
        String receiptId = clientHeaderAccessor.getReceipt();
        if (receiptId != null) {
            accessor.setReceiptId(receiptId);
        }
    }
    return handleInternal(accessor, EMPTY_PAYLOAD, ex, clientHeaderAccessor);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Example 17 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolErrorHandler method handleErrorMessageToClient.

@Override
public Message<byte[]> handleErrorMessageToClient(Message<byte[]> errorMessage) {
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
    Assert.notNull(accessor, "Expected STOMP headers");
    if (!accessor.isMutable()) {
        accessor = StompHeaderAccessor.wrap(errorMessage);
    }
    return handleInternal(accessor, errorMessage.getPayload(), null, null);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Example 18 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolHandler method getDisconnectReceipt.

private String getDisconnectReceipt(SimpMessageHeaderAccessor simpHeaders) {
    String name = StompHeaderAccessor.DISCONNECT_MESSAGE_HEADER;
    Message<?> message = (Message<?>) simpHeaders.getHeader(name);
    if (message != null) {
        StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        return accessor.getReceipt();
    }
    return null;
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Example 19 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolHandler method createDisconnectMessage.

private Message<byte[]> createDisconnectMessage(WebSocketSession session) {
    StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
    if (getHeaderInitializer() != null) {
        getHeaderInitializer().initHeaders(headerAccessor);
    }
    headerAccessor.setSessionId(session.getId());
    headerAccessor.setSessionAttributes(session.getAttributes());
    headerAccessor.setUser(getUser(session));
    return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Example 20 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor in project spring-framework by spring-projects.

the class StompSubProtocolHandler method getStompHeaderAccessor.

private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor instanceof StompHeaderAccessor) {
        return (StompHeaderAccessor) accessor;
    } else {
        StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
        SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
        if (SimpMessageType.CONNECT_ACK.equals(messageType)) {
            stompAccessor = convertConnectAcktoStompConnected(stompAccessor);
        } else if (SimpMessageType.DISCONNECT_ACK.equals(messageType)) {
            String receipt = getDisconnectReceipt(stompAccessor);
            if (receipt != null) {
                stompAccessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
                stompAccessor.setReceiptId(receipt);
            } else {
                stompAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
                stompAccessor.setMessage("Session closed.");
            }
        } else if (SimpMessageType.HEARTBEAT.equals(messageType)) {
            stompAccessor = StompHeaderAccessor.createForHeartbeat();
        } else if (stompAccessor.getCommand() == null || StompCommand.SEND.equals(stompAccessor.getCommand())) {
            stompAccessor.updateStompCommandAsServerMessage();
        }
        return stompAccessor;
    }
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Aggregations

StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)38 Test (org.junit.Test)28 TextMessage (org.springframework.web.socket.TextMessage)19 Message (org.springframework.messaging.Message)10 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)6 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)5 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)3 PongMessage (org.springframework.web.socket.PongMessage)3 Principal (java.security.Principal)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 SimpAttributes (org.springframework.messaging.simp.SimpAttributes)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)2 ByteBuffer (java.nio.ByteBuffer)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1