Example usage for org.springframework.messaging Message getHeaders

List of usage examples for org.springframework.messaging Message getHeaders

Introduction

In this page you can find the example usage for org.springframework.messaging Message getHeaders.

Prototype

MessageHeaders getHeaders();

Source Link

Document

Return message headers for the message (never null but may be empty).

Usage

From source file:org.springframework.messaging.converter.AbstractMessageConverter.java

protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
    return (supports(targetClass) && supportsMimeType(message.getHeaders()));
}

From source file:org.springframework.messaging.core.GenericMessagingTemplate.java

protected final void doSend(MessageChannel channel, Message<?> message, long timeout) {
    Assert.notNull(channel, "MessageChannel is required");

    Message<?> messageToSend = message;
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor != null && accessor.isMutable()) {
        accessor.removeHeader(this.sendTimeoutHeader);
        accessor.removeHeader(this.receiveTimeoutHeader);
        accessor.setImmutable();/*from   www  . j  ava 2  s .  c o  m*/
    } else if (message.getHeaders().containsKey(this.sendTimeoutHeader)
            || message.getHeaders().containsKey(this.receiveTimeoutHeader)) {
        messageToSend = MessageBuilder.fromMessage(message).setHeader(this.sendTimeoutHeader, null)
                .setHeader(this.receiveTimeoutHeader, null).build();
    }

    boolean sent = (timeout >= 0 ? channel.send(messageToSend, timeout) : channel.send(messageToSend));

    if (!sent) {
        throw new MessageDeliveryException(message,
                "Failed to send message to channel '" + channel + "' within timeout: " + timeout);
    }
}

From source file:org.springframework.messaging.core.GenericMessagingTemplate.java

@Override
@Nullable/*w  w  w.j  a v a 2s  .  c om*/
protected final Message<?> doSendAndReceive(MessageChannel channel, Message<?> requestMessage) {
    Assert.notNull(channel, "'channel' is required");
    Object originalReplyChannelHeader = requestMessage.getHeaders().getReplyChannel();
    Object originalErrorChannelHeader = requestMessage.getHeaders().getErrorChannel();

    long sendTimeout = sendTimeout(requestMessage);
    long receiveTimeout = receiveTimeout(requestMessage);

    TemporaryReplyChannel tempReplyChannel = new TemporaryReplyChannel(this.throwExceptionOnLateReply);
    requestMessage = MessageBuilder.fromMessage(requestMessage).setReplyChannel(tempReplyChannel)
            .setHeader(this.sendTimeoutHeader, null).setHeader(this.receiveTimeoutHeader, null)
            .setErrorChannel(tempReplyChannel).build();

    try {
        doSend(channel, requestMessage, sendTimeout);
    } catch (RuntimeException ex) {
        tempReplyChannel.setSendFailed(true);
        throw ex;
    }

    Message<?> replyMessage = this.doReceive(tempReplyChannel, receiveTimeout);
    if (replyMessage != null) {
        replyMessage = MessageBuilder.fromMessage(replyMessage)
                .setHeader(MessageHeaders.REPLY_CHANNEL, originalReplyChannelHeader)
                .setHeader(MessageHeaders.ERROR_CHANNEL, originalErrorChannelHeader).build();
    }

    return replyMessage;
}

From source file:org.springframework.messaging.core.GenericMessagingTemplate.java

private long sendTimeout(Message<?> requestMessage) {
    Long sendTimeout = headerToLong(requestMessage.getHeaders().get(this.sendTimeoutHeader));
    return (sendTimeout != null ? sendTimeout : this.sendTimeout);
}

From source file:org.springframework.messaging.core.GenericMessagingTemplate.java

private long receiveTimeout(Message<?> requestMessage) {
    Long receiveTimeout = headerToLong(requestMessage.getHeaders().get(this.receiveTimeoutHeader));
    return (receiveTimeout != null ? receiveTimeout : this.receiveTimeout);
}

From source file:org.springframework.messaging.handler.annotation.reactive.HeaderMethodArgumentResolver.java

@Override
@Nullable//w  w  w.j a  va2 s  .com
protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, String name) {

    Object headerValue = message.getHeaders().get(name);
    Object nativeHeaderValue = getNativeHeaderValue(message, name);

    if (headerValue != null && nativeHeaderValue != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("A value was found for '" + name + "', in both the top level header map "
                    + "and also in the nested map for native headers. Using the value from top level map. "
                    + "Use 'nativeHeader.myHeader' to resolve the native header.");
        }
    }

    return (headerValue != null ? headerValue : nativeHeaderValue);
}

From source file:org.springframework.messaging.handler.annotation.reactive.HeaderMethodArgumentResolver.java

@SuppressWarnings("unchecked")
@Nullable//from w  ww .  jav a 2s.c om
private Map<String, List<String>> getNativeHeaders(Message<?> message) {
    return (Map<String, List<String>>) message.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS);
}

From source file:org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.java

/**
 * Return the mime type for the content. By default this method checks the
 * {@link MessageHeaders#CONTENT_TYPE} header expecting to find a
 * {@link MimeType} value or a String to parse to a {@link MimeType}.
 * @param message the input message// w  w w .ja  v  a  2  s.  co m
 */
@Nullable
protected MimeType getMimeType(Message<?> message) {
    Object headerValue = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    if (headerValue == null) {
        return null;
    } else if (headerValue instanceof String) {
        return MimeTypeUtils.parseMimeType((String) headerValue);
    } else if (headerValue instanceof MimeType) {
        return (MimeType) headerValue;
    } else {
        throw new IllegalArgumentException("Unexpected MimeType value: " + headerValue);
    }
}

From source file:org.springframework.messaging.handler.annotation.support.HeaderMethodArgumentResolver.java

@Override
@Nullable/* w  w  w  . j  a  va  2  s .c  om*/
protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, String name)
        throws Exception {

    Object headerValue = message.getHeaders().get(name);
    Object nativeHeaderValue = getNativeHeaderValue(message, name);

    if (headerValue != null && nativeHeaderValue != null) {
        if (logger.isWarnEnabled()) {
            logger.warn("Message headers contain two values for the same header '" + name + "', "
                    + "one in the top level header map and a second in the nested map with native headers. "
                    + "Using the value from top level map. "
                    + "Use 'nativeHeader.myHeader' to resolve to the value from the nested native header map.");
        }
    }

    return (headerValue != null ? headerValue : nativeHeaderValue);
}

From source file:org.springframework.messaging.handler.annotation.support.HeaderMethodArgumentResolver.java

@SuppressWarnings("unchecked")
private Map<String, List<String>> getNativeHeaders(Message<?> message) {
    return (Map<String, List<String>>) message.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS);
}