Example usage for org.springframework.messaging.simp.stomp StompConversionException StompConversionException

List of usage examples for org.springframework.messaging.simp.stomp StompConversionException StompConversionException

Introduction

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

Prototype

public StompConversionException(String msg, Throwable cause) 

Source Link

Usage

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

/**
 * Encodes the given payload and headers into a {@code byte[]}.
 * @param headers the headers/*from w  w  w  .j  a v  a 2s  .  c  om*/
 * @param payload the payload
 * @return the encoded message
 */
public byte[] encode(Map<String, Object> headers, byte[] payload) {
    Assert.notNull(headers, "'headers' is required");
    Assert.notNull(payload, "'payload' is required");

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length);
        DataOutputStream output = new DataOutputStream(baos);

        if (SimpMessageType.HEARTBEAT.equals(SimpMessageHeaderAccessor.getMessageType(headers))) {
            logger.trace("Encoding heartbeat");
            output.write(StompDecoder.HEARTBEAT_PAYLOAD);
        }

        else {
            StompCommand command = StompHeaderAccessor.getCommand(headers);
            if (command == null) {
                throw new IllegalStateException("Missing STOMP command: " + headers);
            }

            output.write(command.toString().getBytes(StandardCharsets.UTF_8));
            output.write(LF);
            writeHeaders(command, headers, payload, output);
            output.write(LF);
            writeBody(payload, output);
            output.write((byte) 0);
        }

        return baos.toByteArray();
    } catch (IOException ex) {
        throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers, ex);
    }
}