Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2013 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.messaging.support;
 
 import java.util.Map;
 
A builder for creating a GenericMessage (or ErrorMessage if the payload is of type java.lang.Throwable).

Author(s):
Arjen Poutsma
Mark Fisher
Rossen Stoyanchev
Since:
4.0
See also:
GenericMessage
ErrorMessage
 
 public final class MessageBuilder<T> {
 
 	private final T payload;
 
 
 	private final Message<T> originalMessage;


Private constructor to be invoked from the static factory methods only.
 
 	private MessageBuilder(T payloadMessage<T> originalMessage) {
 		Assert.notNull(payload"payload must not be null");
 		this. = payload;
 		this. = originalMessage;
 		this. = new MessageHeaderAccessor(originalMessage);
 	}


Create a builder for a new org.springframework.messaging.Message instance pre-populated with all of the headers copied from the provided message. The payload of the provided Message will also be used as the payload for the new message.

Parameters:
message the Message from which the payload and all headers will be copied
 
 	public static <T> MessageBuilder<T> fromMessage(Message<T> message) {
 		Assert.notNull(message"message must not be null");
 		return new MessageBuilder<T>(message.getPayload(), message);
 	}

Create a builder for a new org.springframework.messaging.Message instance with the provided payload.

Parameters:
payload the payload for the new message
 
 	public static <T> MessageBuilder<T> withPayload(T payload) {
 		return new MessageBuilder<T>(payloadnull);
 	}


Set the message headers.

Parameters:
headerAccessor the headers for the message
 
 	public MessageBuilder<T> setHeaders(MessageHeaderAccessor headerAccessor) {
 		Assert.notNull(headerAccessor"HeaderAccessor must not be null");
 		this. = headerAccessor;
 		return this;
 	}

Set the value for the given header name. If the provided value is null, the header will be removed.
 
 	public MessageBuilder<T> setHeader(String headerNameObject headerValue) {
 		this..setHeader(headerNameheaderValue);
 		return this;
 	}

Set the value for the given header name only if the header name is not already associated with a value.
	public MessageBuilder<T> setHeaderIfAbsent(String headerNameObject headerValue) {
		this..setHeaderIfAbsent(headerNameheaderValue);
		return this;
	}

Removes all headers provided via array of 'headerPatterns'. As the name suggests the array may contain simple matching patterns for header names. Supported pattern styles are: "xxx*", "*xxx", "*xxx*" and "xxx*yyy".
	public MessageBuilder<T> removeHeaders(String... headerPatterns) {
		this..removeHeaders(headerPatterns);
		return this;
	}
Remove the value for the given header name.
	public MessageBuilder<T> removeHeader(String headerName) {
		this..removeHeader(headerName);
		return this;
	}

Copy the name-value pairs from the provided Map. This operation will overwrite any existing values. Use { copyHeadersIfAbsent(java.util.Map) to avoid overwriting values. Note that the 'id' and 'timestamp' header values will never be overwritten.
	public MessageBuilder<T> copyHeaders(Map<String, ?> headersToCopy) {
		this..copyHeaders(headersToCopy);
		return this;
	}

Copy the name-value pairs from the provided Map. This operation will not overwrite any existing values.
	public MessageBuilder<T> copyHeadersIfAbsent(Map<String, ?> headersToCopy) {
		return this;
	}
	public MessageBuilder<T> setReplyChannel(MessageChannel replyChannel) {
		this..setReplyChannel(replyChannel);
		return this;
	}
	public MessageBuilder<T> setReplyChannelName(String replyChannelName) {
		this..setReplyChannelName(replyChannelName);
		return this;
	}
	public MessageBuilder<T> setErrorChannel(MessageChannel errorChannel) {
		this..setErrorChannel(errorChannel);
		return this;
	}
	public MessageBuilder<T> setErrorChannelName(String errorChannelName) {
		this..setErrorChannelName(errorChannelName);
		return this;
	}
	@SuppressWarnings("unchecked")
	public Message<T> build() {
		if ((this. != null) && !this..isModified()) {
			return this.;
		}
		if (this. instanceof Throwable) {
			return (Message<T>) new ErrorMessage((Throwablethis.this..toMap());
		}
		return new GenericMessage<T>(this.this..toMap());
	}
New to GrepCode? Check out our FAQ X