Search in sources :

Example 36 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class AbstractSubscriptionRegistry method registerSubscription.

@Override
public final void registerSubscription(Message<?> message) {
    MessageHeaders headers = message.getHeaders();
    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    if (!SimpMessageType.SUBSCRIBE.equals(messageType)) {
        throw new IllegalArgumentException("Expected SUBSCRIBE: " + message);
    }
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No sessionId in  " + message);
        }
        return;
    }
    String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
    if (subscriptionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No subscriptionId in " + message);
        }
        return;
    }
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    if (destination == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No destination in " + message);
        }
        return;
    }
    addSubscriptionInternal(sessionId, subscriptionId, destination, message);
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 37 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class StompBrokerRelayMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    String sessionId = SimpMessageHeaderAccessor.getSessionId(message.getHeaders());
    if (!isBrokerAvailable()) {
        if (sessionId == null || SYSTEM_SESSION_ID.equals(sessionId)) {
            throw new MessageDeliveryException("Message broker not active. Consider subscribing to " + "receive BrokerAvailabilityEvent's from an ApplicationListener Spring bean.");
        }
        StompConnectionHandler handler = this.connectionHandlers.get(sessionId);
        if (handler != null) {
            handler.sendStompErrorFrameToClient("Broker not available.");
            handler.clearConnection();
        } else {
            StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
            if (getHeaderInitializer() != null) {
                getHeaderInitializer().initHeaders(accessor);
            }
            accessor.setSessionId(sessionId);
            accessor.setUser(SimpMessageHeaderAccessor.getUser(message.getHeaders()));
            accessor.setMessage("Broker not available.");
            MessageHeaders headers = accessor.getMessageHeaders();
            getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers));
        }
        return;
    }
    StompHeaderAccessor stompAccessor;
    StompCommand command;
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor == null) {
        throw new IllegalStateException("No header accessor (not using the SimpMessagingTemplate?): " + message);
    } else if (accessor instanceof StompHeaderAccessor) {
        stompAccessor = (StompHeaderAccessor) accessor;
        command = stompAccessor.getCommand();
    } else if (accessor instanceof SimpMessageHeaderAccessor) {
        stompAccessor = StompHeaderAccessor.wrap(message);
        command = stompAccessor.getCommand();
        if (command == null) {
            command = stompAccessor.updateStompCommandAsClientMessage();
        }
    } else {
        throw new IllegalStateException("Unexpected header accessor type " + accessor.getClass() + " in " + message);
    }
    if (sessionId == null) {
        if (!SimpMessageType.MESSAGE.equals(stompAccessor.getMessageType())) {
            if (logger.isErrorEnabled()) {
                logger.error("Only STOMP SEND supported from within the server side. Ignoring " + message);
            }
            return;
        }
        sessionId = SYSTEM_SESSION_ID;
        stompAccessor.setSessionId(sessionId);
    }
    String destination = stompAccessor.getDestination();
    if (command != null && command.requiresDestination() && !checkDestinationPrefix(destination)) {
        return;
    }
    if (StompCommand.CONNECT.equals(command)) {
        if (logger.isDebugEnabled()) {
            logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
        }
        stompAccessor = (stompAccessor.isMutable() ? stompAccessor : StompHeaderAccessor.wrap(message));
        stompAccessor.setLogin(this.clientLogin);
        stompAccessor.setPasscode(this.clientPasscode);
        if (getVirtualHost() != null) {
            stompAccessor.setHost(getVirtualHost());
        }
        StompConnectionHandler handler = new StompConnectionHandler(sessionId, stompAccessor);
        this.connectionHandlers.put(sessionId, handler);
        this.stats.incrementConnectCount();
        this.tcpClient.connect(handler);
    } else if (StompCommand.DISCONNECT.equals(command)) {
        StompConnectionHandler handler = this.connectionHandlers.get(sessionId);
        if (handler == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignoring DISCONNECT in session " + sessionId + ". Connection already cleaned up.");
            }
            return;
        }
        stats.incrementDisconnectCount();
        handler.forward(message, stompAccessor);
    } else {
        StompConnectionHandler handler = this.connectionHandlers.get(sessionId);
        if (handler == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("No TCP connection for session " + sessionId + " in " + message);
            }
            return;
        }
        handler.forward(message, stompAccessor);
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageHeaders(org.springframework.messaging.MessageHeaders) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 38 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class AbstractMessageSendingTemplate method doConvert.

/**
	 * Convert the given Object to serialized form, possibly using a
	 * {@link MessageConverter}, wrap it as a message with the given
	 * headers and apply the given post processor.
	 * @param payload the Object to use as payload
	 * @param headers headers for the message to send
	 * @param postProcessor the post processor to apply to the message
	 * @return the converted message
	 */
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    MessageHeaders messageHeaders = null;
    Object conversionHint = (headers != null ? headers.get(CONVERSION_HINT_HEADER) : null);
    Map<String, Object> headersToUse = processHeadersToSend(headers);
    if (headersToUse != null) {
        if (headersToUse instanceof MessageHeaders) {
            messageHeaders = (MessageHeaders) headersToUse;
        } else {
            messageHeaders = new MessageHeaders(headersToUse);
        }
    }
    MessageConverter converter = getMessageConverter();
    Message<?> message = (converter instanceof SmartMessageConverter ? ((SmartMessageConverter) converter).toMessage(payload, messageHeaders, conversionHint) : converter.toMessage(payload, messageHeaders));
    if (message == null) {
        String payloadType = (payload != null ? payload.getClass().getName() : null);
        Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
        throw new MessageConversionException("Unable to convert payload with type='" + payloadType + "', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
    }
    if (postProcessor != null) {
        message = postProcessor.postProcessMessage(message);
    }
    return message;
}
Also used : SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageHeaders(org.springframework.messaging.MessageHeaders) SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter)

Example 39 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class DefaultSubscriptionRegistry method addSubscriptionInternal.

@Override
protected void addSubscriptionInternal(String sessionId, String subsId, String destination, Message<?> message) {
    Expression expression = null;
    MessageHeaders headers = message.getHeaders();
    String selector = SimpMessageHeaderAccessor.getFirstNativeHeader(getSelectorHeaderName(), headers);
    if (selector != null) {
        try {
            expression = this.expressionParser.parseExpression(selector);
            this.selectorHeaderInUse = true;
            if (logger.isTraceEnabled()) {
                logger.trace("Subscription selector: [" + selector + "]");
            }
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse selector: " + selector, ex);
            }
        }
    }
    this.subscriptionRegistry.addSubscription(sessionId, subsId, destination, expression);
    this.destinationCache.updateAfterNewSubscription(destination, sessionId, subsId);
}
Also used : Expression(org.springframework.expression.Expression) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 40 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    MessageHeaders headers = message.getHeaders();
    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    Principal user = SimpMessageHeaderAccessor.getUser(headers);
    updateSessionReadTime(sessionId);
    if (!checkDestinationPrefix(destination)) {
        return;
    }
    if (SimpMessageType.MESSAGE.equals(messageType)) {
        logMessage(message);
        sendMessageToSubscribers(destination, message);
    } else if (SimpMessageType.CONNECT.equals(messageType)) {
        logMessage(message);
        long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers);
        long[] serverHeartbeat = getHeartbeatValue();
        this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat));
        SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
        initHeaders(connectAck);
        connectAck.setSessionId(sessionId);
        connectAck.setUser(SimpMessageHeaderAccessor.getUser(headers));
        connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
        connectAck.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, serverHeartbeat);
        Message<byte[]> messageOut = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
        getClientOutboundChannel().send(messageOut);
    } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
        logMessage(message);
        handleDisconnect(sessionId, user, message);
    } else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        logMessage(message);
        this.subscriptionRegistry.registerSubscription(message);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        logMessage(message);
        this.subscriptionRegistry.unregisterSubscription(message);
    }
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)54 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)9 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1