From 3400f0c373126ac5bbd0226209b756c2a9461133 Mon Sep 17 00:00:00 2001 From: James Carr Date: Thu, 5 Apr 2012 22:44:46 -0500 Subject: [PATCH] AMQP-222 Configure Content Type When Marshalling Allow capability to set content type on message being marshalled. Polishing - standardize white space and fix copyrights; add constant for application/xml; add asserts on setters. --- .../amqp/core/MessageProperties.java | 5 +++- .../MarshallingMessageConverter.java | 20 +++++++++++-- .../MarshallingMessageConverterTests.java | 28 +++++++++++++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/core/MessageProperties.java b/spring-amqp-core/src/main/java/org/springframework/amqp/core/MessageProperties.java index 2eb13f7c..edeaf5cb 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/core/MessageProperties.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/core/MessageProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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 @@ -22,6 +22,7 @@ import java.util.Map; * * @author Mark Fisher * @author Mark Pollack + * @author Gary Russell */ public class MessageProperties { @@ -33,6 +34,8 @@ public class MessageProperties { public static final String CONTENT_TYPE_JSON = "application/json"; + public static final String CONTENT_TYPE_XML = "application/xml"; + private static final String DEFAULT_CONTENT_TYPE = CONTENT_TYPE_BYTES; private static final MessageDeliveryMode DEFAULT_DELIVERY_MODE = MessageDeliveryMode.PERSISTENT; diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java index 1ff31c1f..94f8e67c 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -38,14 +38,16 @@ import org.springframework.util.Assert; * @author Mark Fisher * @author Arjen Poutsma * @author Juergen Hoeller + * @author James Carr * @see org.springframework.amqp.rabbit.core.RabbitTemplate#convertAndSend * @see org.springframework.amqp.rabbit.core.RabbitTemplate#receiveAndConvert */ public class MarshallingMessageConverter extends AbstractMessageConverter implements InitializingBean { + private volatile Marshaller marshaller; - private Marshaller marshaller; + private volatile Unmarshaller unmarshaller; - private Unmarshaller unmarshaller; + private volatile String contentType; /** @@ -94,10 +96,18 @@ public class MarshallingMessageConverter extends AbstractMessageConverter implem } + /** + * Set the contentType to be used by this message converter. + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + /** * Set the {@link Marshaller} to be used by this message converter. */ public void setMarshaller(Marshaller marshaller) { + Assert.notNull(marshaller, "marshaller must not be null"); this.marshaller = marshaller; } @@ -105,6 +115,7 @@ public class MarshallingMessageConverter extends AbstractMessageConverter implem * Set the {@link Unmarshaller} to be used by this message converter. */ public void setUnmarshaller(Unmarshaller unmarshaller) { + Assert.notNull(unmarshaller, "unmarshaller must not be null"); this.unmarshaller = unmarshaller; } @@ -119,6 +130,9 @@ public class MarshallingMessageConverter extends AbstractMessageConverter implem */ protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { try { + if (contentType != null) { + messageProperties.setContentType(contentType); + } ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(bos); marshaller.marshal(object, streamResult); diff --git a/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/MarshallingMessageConverterTests.java b/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/MarshallingMessageConverterTests.java index e978fe88..d4516f47 100644 --- a/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/MarshallingMessageConverterTests.java +++ b/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/MarshallingMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -35,9 +35,9 @@ import org.springframework.oxm.XmlMappingException; /** * @author Mark Fisher + * @author James Carr */ public class MarshallingMessageConverterTests { - @Test public void marshal() throws Exception { TestMarshaller marshaller = new TestMarshaller(); @@ -48,6 +48,30 @@ public class MarshallingMessageConverterTests { assertEquals("MARSHAL TEST", response); } + @Test + public void marshalIncludesContentType() throws Exception { + TestMarshaller marshaller = new TestMarshaller(); + MarshallingMessageConverter converter = new MarshallingMessageConverter(marshaller); + converter.setContentType(MessageProperties.CONTENT_TYPE_XML); + converter.afterPropertiesSet(); + + Message message = converter.toMessage("marshal test", new MessageProperties()); + + assertEquals("application/xml", message.getMessageProperties().getContentType()); + } + + @Test + public void dontSetNullContentType() throws Exception { + TestMarshaller marshaller = new TestMarshaller(); + MarshallingMessageConverter converter = new MarshallingMessageConverter(marshaller); + converter.afterPropertiesSet(); + final String defaultContentType = new MessageProperties().getContentType(); + + Message message = converter.toMessage("marshal test", new MessageProperties()); + + assertEquals(defaultContentType, message.getMessageProperties().getContentType()); + } + @Test public void unmarshal() { TestMarshaller marshaller = new TestMarshaller();