Example usage for org.springframework.messaging.simp.stomp StompHeaderAccessor getContentType

List of usage examples for org.springframework.messaging.simp.stomp StompHeaderAccessor getContentType

Introduction

In this page you can find the example usage for org.springframework.messaging.simp.stomp StompHeaderAccessor getContentType.

Prototype

@Nullable
    public MimeType getContentType() 

Source Link

Usage

From source file:org.springframework.messaging.simp.stomp.DefaultStompSession.java

@SuppressWarnings("unchecked")
private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable Object payload) {
    accessor.updateSimpMessageHeadersFromStompHeaders();
    Message<byte[]> message;
    if (payload == null) {
        message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    } else if (payload instanceof byte[]) {
        message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders());
    } else {//w ww.j  a  v  a2s  . c  om
        message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders());
        accessor.updateStompHeadersFromSimpMessageHeaders();
        if (message == null) {
            throw new MessageConversionException(
                    "Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='"
                            + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]");
        }
    }
    return message;
}

From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java

private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload) {
    StompCommand command = stompAccessor.getCommand();
    try {/*from   w  w  w  .j a v a2  s .co  m*/
        byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), payload);
        boolean useBinary = (payload.length > 0 && !(session instanceof SockJsSession)
                && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType()));
        if (useBinary) {
            session.sendMessage(new BinaryMessage(bytes));
        } else {
            session.sendMessage(new TextMessage(bytes));
        }
    } catch (SessionLimitExceededException ex) {
        // Bad session, just get out
        throw ex;
    } catch (Throwable ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to send WebSocket message to client in session " + session.getId(), ex);
        }
        command = StompCommand.ERROR;
    } finally {
        if (StompCommand.ERROR.equals(command)) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
}