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

The following examples show how to use org.springframework.messaging.simp.stomp.StompHeaderAccessor#setMessage() . 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: StompSubProtocolErrorHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public Message<byte[]> handleClientMessageProcessingError(@Nullable 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);
		if (clientHeaderAccessor != null) {
			String receiptId = clientHeaderAccessor.getReceipt();
			if (receiptId != null) {
				accessor.setReceiptId(receiptId);
			}
		}
	}

	return handleInternal(accessor, EMPTY_PAYLOAD, ex, clientHeaderAccessor);
}
 
Example 2
Source File: StompSubProtocolErrorHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Message<byte[]> handleClientMessageProcessingError(@Nullable 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);
		if (clientHeaderAccessor != null) {
			String receiptId = clientHeaderAccessor.getReceipt();
			if (receiptId != null) {
				accessor.setReceiptId(receiptId);
			}
		}
	}

	return handleInternal(accessor, EMPTY_PAYLOAD, ex, clientHeaderAccessor);
}
 
Example 3
Source File: StompSubProtocolErrorHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: StompSubProtocolHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Invoked when no
 * {@link #setErrorHandler(StompSubProtocolErrorHandler) errorHandler}
 * is configured to send an ERROR frame to the client.
 */
private void sendErrorMessage(WebSocketSession session, Throwable error) {
	StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
	headerAccessor.setMessage(error.getMessage());

	byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
	try {
		session.sendMessage(new TextMessage(bytes));
	}
	catch (Throwable ex) {
		// Could be part of normal workflow (e.g. browser tab closed)
		logger.debug("Failed to send STOMP ERROR to client", ex);
	}
}
 
Example 5
Source File: StompSubProtocolHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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;
	}
}
 
Example 6
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private void handleConnectMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    boolean result = authorize(stompHeaderAccessor);

    // send response
    StompHeaderAccessor accessor;
    if (result) {
        accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
        accessor.setHeartbeat(0, this.weEventConfig.getStompHeartbeats() * 1000);
    } else {
        accessor = StompHeaderAccessor.create(StompCommand.ERROR);
        accessor.setMessage("login or password is wrong");
    }
    accessor.setVersion("1.1");
    sendSimpleMessage(session, accessor);
}
 
Example 7
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
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 8
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private void handleDefaultMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    String simpDestination = stompHeaderAccessor.getDestination();
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    // send response
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
    accessor.setDestination(simpDestination);
    accessor.setMessage("NOT SUPPORT COMMAND");
    accessor.setReceiptId(headerIdStr);
    // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
    sendSimpleMessage(session, accessor);

    // follow protocol 1.2 to close connection
    this.closeSession(session);
}
 
Example 9
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
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 10
Source File: StompSubProtocolHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Invoked when no
 * {@link #setErrorHandler(StompSubProtocolErrorHandler) errorHandler}
 * is configured to send an ERROR frame to the client.
 */
private void sendErrorMessage(WebSocketSession session, Throwable error) {
	StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
	headerAccessor.setMessage(error.getMessage());

	byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
	try {
		session.sendMessage(new TextMessage(bytes));
	}
	catch (Throwable ex) {
		// Could be part of normal workflow (e.g. browser tab closed)
		logger.debug("Failed to send STOMP ERROR to client", ex);
	}
}
 
Example 11
Source File: StompSubProtocolHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
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;
	}
}
 
Example 12
Source File: StompSubProtocolHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Invoked when no
 * {@link #setErrorHandler(StompSubProtocolErrorHandler) errorHandler}
 * is configured to send an ERROR frame to the client.
 * @deprecated as of Spring 4.2, in favor of
 * {@link #setErrorHandler(StompSubProtocolErrorHandler) configuring}
 * a {@code StompSubProtocolErrorHandler}
 */
@Deprecated
protected void sendErrorMessage(WebSocketSession session, Throwable error) {
	StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
	headerAccessor.setMessage(error.getMessage());

	byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD);
	try {
		session.sendMessage(new TextMessage(bytes));
	}
	catch (Throwable ex) {
		// Could be part of normal workflow (e.g. browser tab closed)
		logger.debug("Failed to send STOMP ERROR to client", ex);
	}
}