Merge pull request #22 from jamescarr/AMQP-222

This commit is contained in:
Gary Russell
2012-04-06 13:05:15 -04:00
3 changed files with 47 additions and 6 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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();