Here are the examples of the java api org.springframework.messaging.MessageHeaders taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
264 Examples
19
View Source File : GenericMessage.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* An implementation of {@link Message} with a generic payload.
* Once created, a GenericMessage is immutable.
*
* @author Mark Fisher
* @since 4.0
* @param <T> the payload type
* @see MessageBuilder
*/
public clreplaced GenericMessage<T> implements Message<T>, Serializable {
private static final long serialVersionUID = 4268801052358035098L;
private final T payload;
private final MessageHeaders headers;
/**
* Create a new message with the given payload.
* @param payload the message payload (never {@code null})
*/
public GenericMessage(T payload) {
this(payload, new MessageHeaders(null));
}
/**
* Create a new message with the given payload and headers.
* The content of the given header map is copied.
* @param payload the message payload (never {@code null})
* @param headers message headers to use for initialization
*/
public GenericMessage(T payload, Map<String, Object> headers) {
this(payload, new MessageHeaders(headers));
}
/**
* A constructor with the {@link MessageHeaders} instance to use.
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used
* directly in the new message, i.e. it is not copied.
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public GenericMessage(T payload, MessageHeaders headers) {
replacedert.notNull(payload, "Payload must not be null");
replacedert.notNull(headers, "MessageHeaders must not be null");
this.payload = payload;
this.headers = headers;
}
public T getPayload() {
return this.payload;
}
public MessageHeaders getHeaders() {
return this.headers;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof GenericMessage)) {
return false;
}
GenericMessage<?> otherMsg = (GenericMessage<?>) other;
// Using nullSafeEquals for proper array equals comparisons
return (ObjectUtils.nullSafeEquals(this.payload, otherMsg.payload) && this.headers.equals(otherMsg.headers));
}
public int hashCode() {
// Using nullSafeHashCode for proper array hashCode handling
return (ObjectUtils.nullSafeHashCode(this.payload) * 23 + this.headers.hashCode());
}
public String toString() {
StringBuilder sb = new StringBuilder(getClreplaced().getSimpleName());
sb.append(" [payload=");
if (this.payload instanceof byte[]) {
sb.append("byte[").append(((byte[]) this.payload).length).append("]");
} else {
sb.append(this.payload);
}
sb.append(", headers=").append(this.headers).append("]");
return sb.toString();
}
}
19
View Source File : SendToMethodReturnValueHandler.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
if (returnValue == null) {
return;
}
MessageHeaders headers = message.getHeaders();
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
DestinationHelper destinationHelper = getDestinationHelper(headers, returnType);
SendToUser sendToUser = destinationHelper.getSendToUser();
if (sendToUser != null) {
boolean broadcast = sendToUser.broadcast();
String user = getUserName(message, headers);
if (user == null) {
if (sessionId == null) {
throw new MissingSessionUserException(message);
}
user = sessionId;
broadcast = false;
}
String[] destinations = getTargetDestinations(sendToUser, message, this.defaultUserDestinationPrefix);
for (String destination : destinations) {
destination = destinationHelper.expandTemplateVars(destination);
if (broadcast) {
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(null, returnType));
} else {
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId, returnType));
}
}
}
SendTo sendTo = destinationHelper.getSendTo();
if (sendTo != null || sendToUser == null) {
String[] destinations = getTargetDestinations(sendTo, message, this.defaultDestinationPrefix);
for (String destination : destinations) {
destination = destinationHelper.expandTemplateVars(destination);
this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
}
}
}
19
View Source File : SendToMethodReturnValueHandler.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Nullable
protected String getUserName(Message<?> message, MessageHeaders headers) {
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
if (principal != null) {
return (principal instanceof DestinationUserNameProvider ? ((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
}
return null;
}
19
View Source File : AbstractMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
protected boolean supportsMimeType(@Nullable MessageHeaders headers) {
if (getSupportedMimeTypes().isEmpty()) {
return true;
}
MimeType mimeType = getMimeType(headers);
if (mimeType == null) {
return !isStrictContentTypeMatch();
}
for (MimeType current : getSupportedMimeTypes()) {
if (current.getType().equals(mimeType.getType()) && current.getSubtype().equals(mimeType.getSubtype())) {
return true;
}
}
return false;
}
19
View Source File : AbstractMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
return toMessage(payload, headers, null);
}
19
View Source File : AbstractMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
return (supports(payload.getClreplaced()) && supportsMimeType(headers));
}
19
View Source File : AbstractMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Convert the payload object to serialized form.
* @param payload the Object to convert
* @param headers optional headers for the message (may be {@code null})
* @param conversionHint an extra object preplaceded to the {@link MessageConverter},
* e.g. the replacedociated {@code MethodParameter} (may be {@code null}}
* @return the resulting payload for the message, or {@code null} if the converter
* cannot perform the conversion
* @since 4.2
*/
@Nullable
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
return null;
}
19
View Source File : AbstractMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Nullable
protected MimeType getMimeType(@Nullable MessageHeaders headers) {
return (headers != null && this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
}
19
View Source File : FunctionTypeConversionHelper.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object doConvert(Object incoming, MimeType mimeType) {
MessageHeaders headers = new MessageHeaders(Collections.singletonMap(MessageHeaders.CONTENT_TYPE, mimeType));
if (incoming instanceof Publisher) {
incoming = incoming instanceof Mono ? Mono.from((Publisher) incoming).map(value -> this.messageConverter.toMessage(value, headers)) : Flux.from((Publisher) incoming).map(value -> this.messageConverter.toMessage(value, headers));
} else {
replacedert.isTrue(!Publisher.clreplaced.isreplacedignableFrom(this.functionRegistration.getType().getInputWrapper()), "Invoking reactive function as imperative is not allowed. Function name(s): " + this.functionRegistration.getNames());
incoming = this.messageConverter.toMessage(incoming, headers);
}
return incoming;
}
19
View Source File : CloudEventMessageUtils.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
private static Message<?> buildBinaryMessageFromStructuredMap(Map<String, Object> structuredCloudEvent, MessageHeaders originalHeaders) {
Object payload = structuredCloudEvent.remove(DATA);
if (payload == null) {
payload = Collections.emptyMap();
}
CloudEventMessageBuilder<?> messageBuilder = CloudEventMessageBuilder.withData(payload).copyHeaders(structuredCloudEvent);
for (String key : originalHeaders.keySet()) {
if (!MessageHeaders.ID.equals(key)) {
messageBuilder.setHeader(key, originalHeaders.get(key));
}
}
return messageBuilder.build();
}
19
View Source File : CloudEventMessageBuilder.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
private Message<T> doBuild(String prefix) {
if (!this.headers.containsKey(prefix + CloudEventMessageUtils._SPECVERSION)) {
this.headers.put(prefix + CloudEventMessageUtils._SPECVERSION, "1.0");
}
if (!this.headers.containsKey(prefix + CloudEventMessageUtils._ID)) {
this.headers.put(prefix + CloudEventMessageUtils._ID, UUID.randomUUID().toString());
}
this.headers.put(MessageUtils.MESSAGE_TYPE, CloudEventMessageUtils.CLOUDEVENT_VALUE);
if (!this.headers.containsKey(prefix + CloudEventMessageUtils._TYPE)) {
this.headers.put(prefix + CloudEventMessageUtils._TYPE, this.data.getClreplaced().getName());
}
if (!this.headers.containsKey(prefix + CloudEventMessageUtils._SOURCE)) {
this.headers.put(prefix + CloudEventMessageUtils._SOURCE, URI.create("https://spring.io/"));
}
MessageHeaders headers = new MessageHeaders(this.headers);
GenericMessage<T> message = new GenericMessage<T>(this.data, headers);
replacedert.isTrue(CloudEventMessageUtils.isCloudEvent(message), "The message does not appear to be a valid Cloud Event, " + "since one of the required attributes (id, specversion, type, source) is missing");
return message;
}
19
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void leaveMutableDefaultBehavior() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar");
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setLeaveMutable(true)).withMessageContaining("Already immutable");
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setHeader("foo", "baz")).withMessageContaining("Already immutable");
replacedertThat(headers.get("foo")).isEqualTo("bar");
replacedertThat(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced)).isSameAs(accessor);
}
19
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void leaveMutable() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar");
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
accessor.setHeader("foo", "baz");
replacedertThat(headers.get("foo")).isEqualTo("baz");
replacedertThat(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced)).isSameAs(accessor);
}
19
View Source File : MessageBuilderTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testBuildMessageWithMutableHeaders() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
accessor.setHeader("foo", "bar");
replacedertThat(headers.get("foo")).isEqualTo("bar");
replacedertThat(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced)).isSameAs(accessor);
}
19
View Source File : MessageBuilderTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testBuildMessageWithDefaultMutability() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("foo", headers);
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setHeader("foo", "bar")).withMessageContaining("Already immutable");
replacedertThat(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced)).isSameAs(accessor);
}
19
View Source File : GenericMessage.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* An implementation of {@link Message} with a generic payload.
* Once created, a GenericMessage is immutable.
*
* @author Mark Fisher
* @since 4.0
* @param <T> the payload type
* @see MessageBuilder
*/
public clreplaced GenericMessage<T> implements Message<T>, Serializable {
private static final long serialVersionUID = 4268801052358035098L;
private final T payload;
private final MessageHeaders headers;
/**
* Create a new message with the given payload.
* @param payload the message payload (never {@code null})
*/
public GenericMessage(T payload) {
this(payload, new MessageHeaders(null));
}
/**
* Create a new message with the given payload and headers.
* The content of the given header map is copied.
* @param payload the message payload (never {@code null})
* @param headers message headers to use for initialization
*/
public GenericMessage(T payload, Map<String, Object> headers) {
this(payload, new MessageHeaders(headers));
}
/**
* A constructor with the {@link MessageHeaders} instance to use.
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used
* directly in the new message, i.e. it is not copied.
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public GenericMessage(T payload, MessageHeaders headers) {
replacedert.notNull(payload, "Payload must not be null");
replacedert.notNull(headers, "MessageHeaders must not be null");
this.payload = payload;
this.headers = headers;
}
@Override
public T getPayload() {
return this.payload;
}
@Override
public MessageHeaders getHeaders() {
return this.headers;
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof GenericMessage)) {
return false;
}
GenericMessage<?> otherMsg = (GenericMessage<?>) other;
// Using nullSafeEquals for proper array equals comparisons
return (ObjectUtils.nullSafeEquals(this.payload, otherMsg.payload) && this.headers.equals(otherMsg.headers));
}
@Override
public int hashCode() {
// Using nullSafeHashCode for proper array hashCode handling
return (ObjectUtils.nullSafeHashCode(this.payload) * 23 + this.headers.hashCode());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClreplaced().getSimpleName());
sb.append(" [payload=");
if (this.payload instanceof byte[]) {
sb.append("byte[").append(((byte[]) this.payload).length).append("]");
} else {
sb.append(this.payload);
}
sb.append(", headers=").append(this.headers).append("]");
return sb.toString();
}
}
19
View Source File : RqueueMessageHeaders.java
License : Apache License 2.0
Project Creator : sonus21
License : Apache License 2.0
Project Creator : sonus21
/**
*/
public final clreplaced RqueueMessageHeaders {
/**
* This field is mapped to queue name, in the case of the Priority based queueing destination is
* defined as
*
* <p><queue_name>_<priority>
*/
public static final String DESTINATION = "destination";
/**
* Id corresponding to this message
*/
public static final String ID = "messageId";
/**
* this field will provide a {@link RqueueMessage} object
*/
public static final String MESSAGE = "message";
/**
* A reference to {@link Job} object, that can be used in listener to perform different operation
*/
public static final String JOB = "job";
/**
* A reference to {@link Execution} object, that can provide current execution detail A single job
* can have more than one executions due to retry
*/
public static final String EXECUTION = "execution";
private static final MessageHeaders emptyMessageHeaders = new MessageHeaders(Collections.emptyMap());
private RqueueMessageHeaders() {
}
public static MessageHeaders emptyMessageHeaders() {
return emptyMessageHeaders;
}
public static MessageHeaders buildMessageHeaders(String destination, RqueueMessage rqueueMessage, Job job, Execution execution) {
Map<String, Object> headers = new HashMap<>(5);
headers.put(DESTINATION, destination);
headers.put(ID, rqueueMessage.getId());
headers.put(MESSAGE, rqueueMessage);
if (job != null) {
headers.put(JOB, job);
}
if (execution != null) {
headers.put(EXECUTION, execution);
}
return new MessageHeaders(headers);
}
}
19
View Source File : SimpleRqueueListenerContainerFactory.java
License : Apache License 2.0
Project Creator : sonus21
License : Apache License 2.0
Project Creator : sonus21
public void setMessageHeaders(MessageHeaders messageHeaders) {
notEmpty(messageHeaders, "messageHeaders can not be empty");
this.messageHeaders = messageHeaders;
}
19
View Source File : GenericMessage.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* An implementation of {@link Message} with a generic payload.
* Once created, a GenericMessage is immutable.
*
* @author Mark Fisher
* @since 4.0
* @see MessageBuilder
*/
public clreplaced GenericMessage<T> implements Message<T>, Serializable {
private static final long serialVersionUID = 4268801052358035098L;
private final T payload;
private final MessageHeaders headers;
/**
* Create a new message with the given payload.
* @param payload the message payload (never {@code null})
*/
public GenericMessage(T payload) {
this(payload, new MessageHeaders(null));
}
/**
* Create a new message with the given payload and headers.
* The content of the given header map is copied.
* @param payload the message payload (never {@code null})
* @param headers message headers to use for initialization
*/
public GenericMessage(T payload, Map<String, Object> headers) {
this(payload, new MessageHeaders(headers));
}
/**
* A constructor with the {@link MessageHeaders} instance to use.
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used
* directly in the new message, i.e. it is not copied.
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public GenericMessage(T payload, MessageHeaders headers) {
replacedert.notNull(payload, "Payload must not be null");
replacedert.notNull(headers, "MessageHeaders must not be null");
this.payload = payload;
this.headers = headers;
}
public T getPayload() {
return this.payload;
}
public MessageHeaders getHeaders() {
return this.headers;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof GenericMessage)) {
return false;
}
GenericMessage<?> otherMsg = (GenericMessage<?>) other;
// Using nullSafeEquals for proper array equals comparisons
return (ObjectUtils.nullSafeEquals(this.payload, otherMsg.payload) && this.headers.equals(otherMsg.headers));
}
public int hashCode() {
// Using nullSafeHashCode for proper array hashCode handling
return (ObjectUtils.nullSafeHashCode(this.payload) * 23 + this.headers.hashCode());
}
public String toString() {
StringBuilder sb = new StringBuilder(getClreplaced().getSimpleName());
sb.append(" [payload=");
if (this.payload instanceof byte[]) {
sb.append("byte[").append(((byte[]) this.payload).length).append("]");
} else {
sb.append(this.payload);
}
sb.append(", headers=").append(this.headers).append("]");
return sb.toString();
}
}
19
View Source File : SendToMethodReturnValueHandler.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
protected String getUserName(Message<?> message, MessageHeaders headers) {
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
if (principal != null) {
return (principal instanceof DestinationUserNameProvider ? ((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
}
return null;
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
Clreplaced<?> clazz = (payload != null ? payload.getClreplaced() : null);
return (supports(clazz) && supportsMimeType(headers));
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Convert the payload object to serialized form.
* @deprecated as of Spring 4.2, in favor of {@link #convertFromInternal(Message, Clreplaced, Object)}
* (which is also protected instead of public)
*/
@Deprecated
public Object convertToInternal(Object payload, MessageHeaders headers) {
return null;
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Convert the payload object to serialized form.
* @param payload the Object to convert
* @param headers optional headers for the message (may be {@code null})
* @param conversionHint an extra object preplaceded to the {@link MessageConverter},
* e.g. the replacedociated {@code MethodParameter} (may be {@code null}}
* @return the resulting payload for the message, or {@code null} if the converter
* cannot perform the conversion
* @since 4.2
*/
@SuppressWarnings("deprecation")
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
return convertToInternal(payload, headers);
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
protected boolean supportsMimeType(MessageHeaders headers) {
if (getSupportedMimeTypes().isEmpty()) {
return true;
}
MimeType mimeType = getMimeType(headers);
if (mimeType == null) {
return !isStrictContentTypeMatch();
}
for (MimeType current : getSupportedMimeTypes()) {
if (current.getType().equals(mimeType.getType()) && current.getSubtype().equals(mimeType.getSubtype())) {
return true;
}
}
return false;
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
protected MimeType getMimeType(MessageHeaders headers) {
return (this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
}
19
View Source File : AbstractMessageConverter.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers) {
return toMessage(payload, headers, null);
}
19
View Source File : NotificationRequestConverter.java
License : Apache License 2.0
Project Creator : awspring
License : Apache License 2.0
Project Creator : awspring
@Override
public Message<?> toMessage(Object payload, MessageHeaders headers) {
throw new UnsupportedOperationException("This converter only supports reading a SNS notification and not writing them");
}
18
View Source File : MessageHeaderAccessorTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void leaveMutableDefaultBehavior() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar");
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setLeaveMutable(true)).withMessageContaining("Already immutable");
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setHeader("foo", "baz")).withMessageContaining("Already immutable");
replacedertEquals("bar", headers.get("foo"));
replacedertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced));
}
18
View Source File : MessageHeaderAccessorTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void leaveMutable() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar");
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
accessor.setHeader("foo", "baz");
replacedertEquals("baz", headers.get("foo"));
replacedertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced));
}
18
View Source File : MessageBuilderTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testBuildMessageWithMutableHeaders() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
accessor.setHeader("foo", "bar");
replacedertEquals("bar", headers.get("foo"));
replacedertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced));
}
18
View Source File : MessageBuilderTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testBuildMessageWithDefaultMutability() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("foo", headers);
replacedertThatIllegalStateException().isThrownBy(() -> accessor.setHeader("foo", "bar")).withMessageContaining("Already immutable");
replacedertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.clreplaced));
}
18
View Source File : HeadersMethodArgumentResolverTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@SuppressWarnings("unused")
private void handleMessage(@Headers Map<String, Object> param1, @Headers String param2, MessageHeaders param3, MessageHeaderAccessor param4, TestMessageHeaderAccessor param5) {
}
18
View Source File : DefaultContentTypeResolverTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void resolveNoContentTypeHeader() {
MessageHeaders headers = new MessageHeaders(Collections.<String, Object>emptyMap());
replacedertNull(this.resolver.resolve(headers));
}
18
View Source File : MessageHeaderAccessor.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* A variation of {@link #getAccessor(org.springframework.messaging.Message, Clreplaced)}
* with a {@code MessageHeaders} instance instead of a {@code Message}.
* <p>This is for cases when a full message may not have been created yet.
* @param messageHeaders the message headers to get an accessor for
* @param requiredType the required accessor type (or {@code null} for any)
* @return an accessor instance of the specified type, or {@code null} if none
* @since 4.1
*/
@SuppressWarnings("unchecked")
@Nullable
public static <T extends MessageHeaderAccessor> T getAccessor(MessageHeaders messageHeaders, @Nullable Clreplaced<T> requiredType) {
if (messageHeaders instanceof MutableMessageHeaders) {
MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders;
MessageHeaderAccessor headerAccessor = mutableHeaders.getAccessor();
if (requiredType == null || requiredType.isInstance(headerAccessor)) {
return (T) headerAccessor;
}
}
return null;
}
18
View Source File : MessageBuilder.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@SuppressWarnings("unchecked")
public Message<T> build() {
if (this.originalMessage != null && !this.headerAccessor.isModified()) {
return this.originalMessage;
}
MessageHeaders headersToUse = this.headerAccessor.toMessageHeaders();
if (this.payload instanceof Throwable) {
return (Message<T>) new ErrorMessage((Throwable) this.payload, headersToUse);
} else {
return new GenericMessage<>(this.payload, headersToUse);
}
}
18
View Source File : DefaultUserDestinationResolver.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
private ParseResult parseMessage(MessageHeaders headers, String sourceDest) {
int prefixEnd = this.prefix.length();
int userEnd = sourceDest.indexOf('/', prefixEnd);
replacedert.isTrue(userEnd > 0, "Expected destination pattern \"/user/{userId}/**\"");
String actualDest = sourceDest.substring(userEnd);
String subscribeDest = this.prefix.substring(0, prefixEnd - 1) + actualDest;
String userName = sourceDest.substring(prefixEnd, userEnd);
userName = StringUtils.replace(userName, "%2F", "/");
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
Set<String> sessionIds;
if (userName.equals(sessionId)) {
userName = null;
sessionIds = Collections.singleton(sessionId);
} else {
sessionIds = getSessionIdsByUser(userName, sessionId);
}
if (isRemoveLeadingSlash()) {
actualDest = actualDest.substring(1);
}
return new ParseResult(sourceDest, actualDest, subscribeDest, sessionIds, userName);
}
18
View Source File : SubscriptionMethodReturnValueHandler.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
if (returnValue == null) {
return;
}
MessageHeaders headers = message.getHeaders();
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
String destination = SimpMessageHeaderAccessor.getDestination(headers);
if (subscriptionId == null) {
throw new IllegalStateException("No simpSubscriptionId in " + message + " returned by: " + returnType.getMethod());
}
if (destination == null) {
throw new IllegalStateException("No simpDestination in " + message + " returned by: " + returnType.getMethod());
}
if (logger.isDebugEnabled()) {
logger.debug("Reply to @SubscribeMapping: " + returnValue);
}
MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
}
18
View Source File : SendToMethodReturnValueHandler.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
private DestinationHelper getDestinationHelper(MessageHeaders headers, MethodParameter returnType) {
SendToUser m1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendToUser.clreplaced);
SendTo m2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendTo.clreplaced);
if ((m1 != null && !ObjectUtils.isEmpty(m1.value())) || (m2 != null && !ObjectUtils.isEmpty(m2.value()))) {
return new DestinationHelper(headers, m1, m2);
}
SendToUser c1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClreplaced(), SendToUser.clreplaced);
SendTo c2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClreplaced(), SendTo.clreplaced);
if ((c1 != null && !ObjectUtils.isEmpty(c1.value())) || (c2 != null && !ObjectUtils.isEmpty(c2.value()))) {
return new DestinationHelper(headers, c1, c2);
}
return (m1 != null || m2 != null ? new DestinationHelper(headers, m1, m2) : new DestinationHelper(headers, c1, c2));
}
18
View Source File : MarshallingMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
return (supportsMimeType(headers) && this.marshaller != null && this.marshaller.supports(payload.getClreplaced()));
}
18
View Source File : ByteArrayMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
@Nullable
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
return payload;
}
18
View Source File : MessagingMessageConverter.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public javax.jms.Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
if (!(object instanceof Message)) {
throw new IllegalArgumentException("Could not convert [" + object + "] - only [" + Message.clreplaced.getName() + "] is handled by this converter");
}
Message<?> input = (Message<?>) object;
MessageHeaders headers = input.getHeaders();
Object conversionHint = headers.get(AbstractMessagingTemplate.CONVERSION_HINT_HEADER);
javax.jms.Message reply = createMessageForPayload(input.getPayload(), session, conversionHint);
this.headerMapper.fromHeaders(headers, reply);
return reply;
}
18
View Source File : MappingFastJsonMessageConverter.java
License : Apache License 2.0
Project Creator : uavorg
License : Apache License 2.0
Project Creator : uavorg
@Override
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
return supports(payload.getClreplaced());
}
18
View Source File : JsonMessageConverter.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
@Override
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
if (!supportsMimeType(headers)) {
return false;
}
return true;
}
18
View Source File : ApplicationJsonMessageMarshallingConverter.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
@Override
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
if (payload instanceof byte[]) {
return payload;
} else if (payload instanceof String) {
return ((String) payload).getBytes(StandardCharsets.UTF_8);
} else {
return super.convertToInternal(payload, headers, conversionHint);
}
}
18
View Source File : SpringBootApiGatewayRequestHandler.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
private Map<String, String> toResponseHeaders(MessageHeaders messageHeaders) {
Map<String, String> responseHeaders = new HashMap<>();
messageHeaders.forEach((key, value) -> responseHeaders.put(key, value.toString()));
return responseHeaders;
}
18
View Source File : AWSLambdaUtils.java
License : Apache License 2.0
Project Creator : spring-cloud
License : Apache License 2.0
Project Creator : spring-cloud
public static Message<byte[]> generateMessage(byte[] payload, MessageHeaders headers, Type inputType, ObjectMapper objectMapper) {
return generateMessage(payload, headers, inputType, objectMapper, null);
}
18
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void copyHeaders() {
Map<String, Object> map1 = new HashMap<>();
map1.put("foo", "bar");
GenericMessage<String> message = new GenericMessage<>("payload", map1);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
Map<String, Object> map2 = new HashMap<>();
map2.put("foo", "BAR");
map2.put("bar", "baz");
accessor.copyHeaders(map2);
MessageHeaders actual = accessor.getMessageHeaders();
replacedertThat(actual.size()).isEqualTo(3);
replacedertThat(actual.get("foo")).isEqualTo("BAR");
replacedertThat(actual.get("bar")).isEqualTo("baz");
}
18
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void removeHeaders() {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bar", "baz");
GenericMessage<String> message = new GenericMessage<>("payload", map);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.removeHeaders("fo*");
MessageHeaders actual = accessor.getMessageHeaders();
replacedertThat(actual.size()).isEqualTo(2);
replacedertThat(actual.get("foo")).isNull();
replacedertThat(actual.get("bar")).isEqualTo("baz");
}
18
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void existingHeaders() throws InterruptedException {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bar", "baz");
GenericMessage<String> message = new GenericMessage<>("payload", map);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
MessageHeaders actual = accessor.getMessageHeaders();
replacedertThat(actual.size()).isEqualTo(3);
replacedertThat(actual.get("foo")).isEqualTo("bar");
replacedertThat(actual.get("bar")).isEqualTo("baz");
}
18
View Source File : MessageHeaderAccessorTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void copyHeadersIfAbsent() {
Map<String, Object> map1 = new HashMap<>();
map1.put("foo", "bar");
GenericMessage<String> message = new GenericMessage<>("payload", map1);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
Map<String, Object> map2 = new HashMap<>();
map2.put("foo", "BAR");
map2.put("bar", "baz");
accessor.copyHeadersIfAbsent(map2);
MessageHeaders actual = accessor.getMessageHeaders();
replacedertThat(actual.size()).isEqualTo(3);
replacedertThat(actual.get("foo")).isEqualTo("bar");
replacedertThat(actual.get("bar")).isEqualTo("baz");
}
See More Examples