Search in sources :

Example 1 with ContentTypeDelegatingMessageConverter

use of org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter in project spring-integration by spring-projects.

the class MappingUtilsTests method testMapping.

@Test
public void testMapping() {
    Message<?> requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, "my/ct").build();
    MessageConverter converter = new SimpleMessageConverter();
    AmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.outboundMapper();
    MessageDeliveryMode defaultDeliveryMode = MessageDeliveryMode.NON_PERSISTENT;
    boolean headersMappedLast = false;
    org.springframework.amqp.core.Message mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    ContentTypeDelegatingMessageConverter ctdConverter = new ContentTypeDelegatingMessageConverter();
    ctdConverter.addDelegate("my/ct", converter);
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    headersMappedLast = false;
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, 42).build();
    try {
        mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("contentType header must be a MimeType or String, found: java.lang.Integer"));
    }
}
Also used : SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode) Test(org.junit.Test)

Example 2 with ContentTypeDelegatingMessageConverter

use of org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter in project spring-integration by spring-projects.

the class MappingUtils method mapMessage.

/**
 * Map an o.s.Message to an o.s.a.core.Message. When using a
 * {@link ContentTypeDelegatingMessageConverter}, {@link AmqpHeaders#CONTENT_TYPE} and
 * {@link MessageHeaders#CONTENT_TYPE} will be used for the selection, with the AMQP
 * header taking precedence.
 * @param requestMessage the request message.
 * @param converter the message converter to use.
 * @param headerMapper the header mapper to use.
 * @param defaultDeliveryMode the default delivery mode.
 * @param headersMappedLast true if headers are mapped after conversion.
 * @return the mapped Message.
 */
public static org.springframework.amqp.core.Message mapMessage(Message<?> requestMessage, MessageConverter converter, AmqpHeaderMapper headerMapper, MessageDeliveryMode defaultDeliveryMode, boolean headersMappedLast) {
    MessageProperties amqpMessageProperties = new MessageProperties();
    org.springframework.amqp.core.Message amqpMessage;
    if (!headersMappedLast) {
        headerMapper.fromHeadersToRequest(requestMessage.getHeaders(), amqpMessageProperties);
    }
    if (converter instanceof ContentTypeDelegatingMessageConverter && headersMappedLast) {
        String contentType = contentTypeAsString(requestMessage.getHeaders());
        if (contentType != null) {
            amqpMessageProperties.setContentType(contentType);
        }
    }
    amqpMessage = converter.toMessage(requestMessage.getPayload(), amqpMessageProperties);
    if (headersMappedLast) {
        headerMapper.fromHeadersToRequest(requestMessage.getHeaders(), amqpMessageProperties);
    }
    checkDeliveryMode(requestMessage, amqpMessageProperties, defaultDeliveryMode);
    return amqpMessage;
}
Also used : MessageProperties(org.springframework.amqp.core.MessageProperties) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter)

Aggregations

ContentTypeDelegatingMessageConverter (org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter)2 Test (org.junit.Test)1 MessageDeliveryMode (org.springframework.amqp.core.MessageDeliveryMode)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)1 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)1