Search in sources :

Example 6 with MessageHeaderAccessor

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

the class SimpAnnotationMethodMessageHandler method handleMatch.

@Override
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) {
    Set<String> patterns = mapping.getDestinationConditions().getPatterns();
    if (!CollectionUtils.isEmpty(patterns)) {
        String pattern = patterns.iterator().next();
        Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
        if (!CollectionUtils.isEmpty(vars)) {
            MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
            Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
            mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
        }
    }
    try {
        SimpAttributesContextHolder.setAttributesFromMessage(message);
        super.handleMatch(mapping, handlerMethod, lookupDestination, message);
    } finally {
        SimpAttributesContextHolder.resetAttributes();
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor)

Example 7 with MessageHeaderAccessor

use of org.springframework.messaging.support.MessageHeaderAccessor 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 8 with MessageHeaderAccessor

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

the class HeadersMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
    Class<?> paramType = parameter.getParameterType();
    if (Map.class.isAssignableFrom(paramType)) {
        return message.getHeaders();
    } else if (MessageHeaderAccessor.class == paramType) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
        return (accessor != null ? accessor : new MessageHeaderAccessor(message));
    } else if (MessageHeaderAccessor.class.isAssignableFrom(paramType)) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
        if (accessor != null && paramType.isAssignableFrom(accessor.getClass())) {
            return accessor;
        } else {
            Method method = ReflectionUtils.findMethod(paramType, "wrap", Message.class);
            Assert.notNull(method, "Cannot create accessor of type " + paramType + " for message " + message);
            return ReflectionUtils.invokeMethod(method, null, message);
        }
    } else {
        throw new IllegalStateException("Unexpected method parameter type " + paramType + "in method " + parameter.getMethod() + ". " + "@Headers method arguments must be assignable to java.util.Map.");
    }
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) Method(java.lang.reflect.Method)

Example 9 with MessageHeaderAccessor

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

the class AbstractMessageConverter method toMessage.

@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }
    payload = convertToInternal(payload, headers, conversionHint);
    if (payload == null) {
        return null;
    }
    MimeType mimeType = getDefaultContentType(payload);
    if (headers != null) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
        if (accessor != null && accessor.isMutable()) {
            accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
            return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
        }
    }
    MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    return builder.build();
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MimeType(org.springframework.util.MimeType)

Example 10 with MessageHeaderAccessor

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

the class AbstractMethodMessageHandler method handleMessage.

@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String destination = getDestination(message);
    if (destination == null) {
        return;
    }
    String lookupDestination = getLookupDestination(destination);
    if (lookupDestination == null) {
        return;
    }
    MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
    headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
    headerAccessor.setLeaveMutable(true);
    message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());
    if (logger.isDebugEnabled()) {
        logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()));
    }
    handleMessageInternal(message, lookupDestination);
    headerAccessor.setImmutable();
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor)

Aggregations

MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)12 Test (org.junit.Test)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 Message (org.springframework.messaging.Message)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 NativeMessageHeaderAccessor (org.springframework.messaging.support.NativeMessageHeaderAccessor)2 MimeType (org.springframework.util.MimeType)2 Method (java.lang.reflect.Method)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1