Java Code Examples for org.springframework.messaging.converter.MessageConverter#fromMessage()

The following examples show how to use org.springframework.messaging.converter.MessageConverter#fromMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageUtils.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public static Object convertMessageToObject(
    Message<String> message, List<MessageConverter> messageConverters) {
  notEmpty(messageConverters, "messageConverters cannot be empty");
  for (MessageConverter messageConverter : messageConverters) {
    try {
      return messageConverter.fromMessage(message, null);
    } catch (Exception e) {
    }
  }
  return null;
}
 
Example 2
Source File: MultiServerUserRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
void addRemoteRegistryDto(Message<?> message, MessageConverter converter, long expirationPeriod) {
	UserRegistrySnapshot registry = (UserRegistrySnapshot) converter.fromMessage(message, UserRegistrySnapshot.class);
	if (registry != null && !registry.getId().equals(this.id)) {
		registry.init(expirationPeriod, this.sessionLookup);
		this.remoteRegistries.put(registry.getId(), registry);
	}
}
 
Example 3
Source File: AbstractMessageReceivingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Convert from the given message to the given target class.
 * @param message the message to convert
 * @param targetClass the target class to convert the payload to
 * @return the converted payload of the reply message (never {@code null})
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
	MessageConverter messageConverter = getMessageConverter();
	T value = (T) messageConverter.fromMessage(message, targetClass);
	if (value == null) {
		throw new MessageConversionException(message, "Unable to convert payload [" + message.getPayload() +
				"] to type [" + targetClass + "] using converter [" + messageConverter + "]");
	}
	return value;
}
 
Example 4
Source File: MultiServerUserRegistry.java    From java-technology-stack with MIT License 5 votes vote down vote up
void addRemoteRegistryDto(Message<?> message, MessageConverter converter, long expirationPeriod) {
	UserRegistrySnapshot registry = (UserRegistrySnapshot) converter.fromMessage(message, UserRegistrySnapshot.class);
	if (registry != null && !registry.getId().equals(this.id)) {
		registry.init(expirationPeriod, this.sessionLookup);
		this.remoteRegistries.put(registry.getId(), registry);
	}
}
 
Example 5
Source File: AbstractMessageReceivingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Convert from the given message to the given target class.
 * @param message the message to convert
 * @param targetClass the target class to convert the payload to
 * @return the converted payload of the reply message (never {@code null})
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
	MessageConverter messageConverter = getMessageConverter();
	T value = (T) messageConverter.fromMessage(message, targetClass);
	if (value == null) {
		throw new MessageConversionException(message, "Unable to convert payload [" + message.getPayload() +
				"] to type [" + targetClass + "] using converter [" + messageConverter + "]");
	}
	return value;
}
 
Example 6
Source File: MultiServerUserRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
void addRemoteRegistryDto(Message<?> message, MessageConverter converter, long expirationPeriod) {
	UserRegistryDto registryDto = (UserRegistryDto) converter.fromMessage(message, UserRegistryDto.class);
	if (registryDto != null && !registryDto.getId().equals(this.id)) {
		long expirationTime = System.currentTimeMillis() + expirationPeriod;
		registryDto.setExpirationTime(expirationTime);
		registryDto.restoreParentReferences();
		this.remoteRegistries.put(registryDto.getId(), registryDto);
	}
}
 
Example 7
Source File: AbstractMessageReceivingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convert from the given message to the given target class.
 * @param message the message to convert
 * @param targetClass the target class to convert the payload to
 * @return the converted payload of the reply message (never {@code null})
 */
@SuppressWarnings("unchecked")
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
	MessageConverter messageConverter = getMessageConverter();
	T value = (T) messageConverter.fromMessage(message, targetClass);
	if (value == null) {
		throw new MessageConversionException(message, "Unable to convert payload [" + message.getPayload() +
				"] to type [" + targetClass + "] using converter [" + messageConverter + "]");
	}
	return value;
}
 
Example 8
Source File: KafkaStreamsMessageConversionDelegate.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private void convertAndSetMessage(Object o, Class<?> valueClass,
		MessageConverter messageConverter, Message<?> msg) {
	Object result = valueClass.isAssignableFrom(msg.getPayload().getClass())
			? msg.getPayload() : messageConverter.fromMessage(msg, valueClass);

	Assert.notNull(result, "Failed to convert message " + msg);

	keyValueThreadLocal.set(new KeyValue<>(o, result));
}
 
Example 9
Source File: ObjectMessageConverterTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testToMessageAndFromMessage() throws Exception {
	String content = "stringwithspecialcharsöäü€a8";
	MySerializableClass sourceMessage = new MySerializableClass(content);
	MessageConverter messageConverter = new ObjectMessageConverter();
	Message<?> message = messageConverter.toMessage(sourceMessage,
			getMessageHeaders("UTF-8"));
	assertThat(Base64.isBase64(message.getPayload().toString().getBytes("UTF-8")))
			.isTrue();
	MySerializableClass result = (MySerializableClass) messageConverter
			.fromMessage(message, MySerializableClass.class);
	assertThat(result.getContent()).isEqualTo(content);
}
 
Example 10
Source File: ObjectMessageConverterTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testToMessageAndFromMessageWithCustomEncoding() throws Exception {
	String content = "stringwithspecialcharsöäü€a8";
	MySerializableClass sourceMessage = new MySerializableClass(content);
	MessageConverter messageConverter = new ObjectMessageConverter("ISO-8859-1");
	Message<?> message = messageConverter.toMessage(sourceMessage,
			getMessageHeaders("ISO-8859-1"));
	assertThat(
			Base64.isBase64(message.getPayload().toString().getBytes("ISO-8859-1")))
					.isTrue();
	MySerializableClass result = (MySerializableClass) messageConverter
			.fromMessage(message, MySerializableClass.class);
	assertThat(result.getContent()).isEqualTo(content);
}
 
Example 11
Source File: MessageUtils.java    From rqueue with Apache License 2.0 4 votes vote down vote up
public static Object convertMessageToObject(
    Message<String> message, MessageConverter messageConverter) {
  return messageConverter.fromMessage(message, null);
}
 
Example 12
Source File: TestSupportBinder.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
	Class<?> targetClass = null;
	MessageConverter converter = null;
	MimeType contentType = MimeType.valueOf(this.contentTypeResolver
							.resolve(message.getHeaders()).toString());

	if (contentType != null) {
		if (equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT,
				contentType)
				|| equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT,
						contentType)) {
			// for Java and Kryo de-serialization we need to reset the content
			// type
			message = MessageBuilder.fromMessage(message)
					.setHeader(MessageHeaders.CONTENT_TYPE, contentType).build();
			converter = equalTypeAndSubType(
					MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType)
							? this.converterFactory
									.getMessageConverterForType(contentType)
							: this.converterFactory
									.getMessageConverterForAllRegistered();
			String targetClassName = contentType.getParameter("type");
			if (StringUtils.hasText(targetClassName)) {
				try {
					targetClass = Class.forName(targetClassName, false,
							Thread.currentThread().getContextClassLoader());
				}
				catch (Exception e) {
					throw new IllegalStateException(
							"Failed to determine class name for contentType: "
									+ message.getHeaders(),
							e);
				}
			}
		}

	}

	Object payload;
	if (converter != null) {
		Assert.isTrue(
				!(equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT,
						contentType) && targetClass == null),
				"Cannot deserialize into message since 'contentType` is not "
						+ "encoded with the actual target type."
						+ "Consider 'application/x-java-object; type=foo.bar.MyClass'");
		payload = converter.fromMessage(message, targetClass);
	}
	else {
		MimeType deserializeContentType = this.contentTypeResolver
				.resolve(message.getHeaders());
		if (deserializeContentType == null) {
			deserializeContentType = contentType;
		}
		payload = deserializeContentType == null ? message.getPayload() : this
				.deserializePayload(message.getPayload(), deserializeContentType);
	}
	message = MessageBuilder.withPayload(payload)
			.copyHeaders(message.getHeaders())
			.setHeader(MessageHeaders.CONTENT_TYPE, contentType)
			.build();
	return message;
}