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

The following examples show how to use org.springframework.messaging.simp.stomp.StompHeaderAccessor#setReceiptId() . 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: BrokerStomp.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
private void handleUnsubscribeMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    String simpDestination = stompHeaderAccessor.getDestination();
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    try {
        boolean result = handleUnSubscribe(session, headerIdStr);

        // send response
        StompHeaderAccessor accessor;
        if (result) {
            accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
            accessor.setDestination(simpDestination);
        } else {
            accessor = StompHeaderAccessor.create(StompCommand.ERROR);
        }
        // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
        accessor.setReceiptId(headerIdStr);
        sendSimpleMessage(session, accessor);
    } catch (BrokerException e) {
        handleErrorMessage(session, e, headerIdStr);
    }
}
 
Example 3
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 4
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 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 handleDisconnectMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    // send response
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
    accessor.setReceiptId(stompHeaderAccessor.getReceipt());
    sendSimpleMessage(session, accessor);

    // close session after reply to client
    this.closeSession(session);
}
 
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
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 11
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
private void handleSubscribeMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    // subscribe command id
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    // group
    String groupId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EVENT_GROUP_ID);
    if (StringUtils.isBlank(groupId)) {
        groupId = "";
    }

    // check if there hasn't the event, need use the last id
    String subEventId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID);
    if (StringUtils.isBlank(subEventId)) {
        subEventId = WeEvent.OFFSET_LAST;
    }

    // subscription id
    String continueSubscriptionIdStr = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_SubscriptionId);
    if (StringUtils.isBlank(continueSubscriptionIdStr)) {
        continueSubscriptionIdStr = "";
    }

    // tag
    String tag = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_TAG);
    if (StringUtils.isBlank(tag)) {
        tag = "";
    }

    // is ephemeral
    boolean ephemeral = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_EPHEMERAL) != null;

    try {
        String simpDestination = stompHeaderAccessor.getDestination();
        String subscriptionId = handleSubscribe(session,
                simpDestination,
                groupId,
                headerIdStr,
                subEventId,
                continueSubscriptionIdStr,
                tag,
                ephemeral);

        // send response
        StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
        accessor.setDestination(simpDestination);
        // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
        accessor.setReceiptId(headerIdStr);
        accessor.setSubscriptionId(subscriptionId);
        accessor.setNativeHeader("subscription-id", subscriptionId);
        sendSimpleMessage(session, accessor);
    } catch (BrokerException e) {
        handleErrorMessage(session, e, headerIdStr);
    }
}