Method parameter matching for Map or Properties will resolve to the payload if its type matches. Otherwise, the header attributes or properties will be passed respectively (INT-192).
This commit is contained in:
@@ -99,14 +99,17 @@ public class AnnotationMethodMessageMapper implements MessageMapper {
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
if (message.getPayload() == null) {
|
||||
throw new IllegalArgumentException("Message payload must not be null.");
|
||||
}
|
||||
if (!this.initialized) {
|
||||
this.initialize();
|
||||
}
|
||||
Object[] args = new Object[this.parameterMetadata.length];
|
||||
for (int i = 0; i < this.parameterMetadata.length; i++) {
|
||||
MethodParameterMetadata metadata = this.parameterMetadata[i];
|
||||
Class<?> type = metadata.type;
|
||||
if (type.equals(HeaderAttribute.class)) {
|
||||
Class<?> expectedType = metadata.type;
|
||||
if (expectedType.equals(HeaderAttribute.class)) {
|
||||
Object value = message.getHeader().getAttribute(metadata.key);
|
||||
if (value == null && metadata.required) {
|
||||
throw new MessageHandlingException(message,
|
||||
@@ -114,7 +117,7 @@ public class AnnotationMethodMessageMapper implements MessageMapper {
|
||||
}
|
||||
args[i] = value;
|
||||
}
|
||||
else if (type.equals(HeaderProperty.class)) {
|
||||
else if (expectedType.equals(HeaderProperty.class)) {
|
||||
Object value = message.getHeader().getProperty(metadata.key);
|
||||
if (value == null && metadata.required) {
|
||||
throw new MessageHandlingException(message,
|
||||
@@ -122,13 +125,16 @@ public class AnnotationMethodMessageMapper implements MessageMapper {
|
||||
}
|
||||
args[i] = value;
|
||||
}
|
||||
else if (Message.class.isAssignableFrom(type)) {
|
||||
else if (expectedType.isAssignableFrom(message.getClass())) {
|
||||
args[i] = message;
|
||||
}
|
||||
else if (Map.class.isAssignableFrom(type)) {
|
||||
else if (expectedType.isAssignableFrom(message.getPayload().getClass())) {
|
||||
args[i] = message.getPayload();
|
||||
}
|
||||
else if (expectedType.equals(Map.class)) {
|
||||
args[i] = this.getHeaderAttributes(message);
|
||||
}
|
||||
else if (Properties.class.isAssignableFrom(type)) {
|
||||
else if (expectedType.equals(Properties.class)) {
|
||||
args[i] = this.getHeaderProperties(message);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -20,13 +20,18 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.annotation.Handler;
|
||||
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
@@ -88,6 +93,70 @@ public class AnnotationMethodMessageMapperTests {
|
||||
assertEquals("bar", args[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesMethodWithNonPropertiesPayload() throws Exception {
|
||||
Method method = TestHandler.class.getMethod("propertiesMethod", Properties.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
Message<?> message = new StringMessage("test");
|
||||
message.getHeader().setProperty("prop1", "foo");
|
||||
message.getHeader().setProperty("prop2", "bar");
|
||||
Object[] args = (Object[]) mapper.mapMessage(message);
|
||||
Properties result = (Properties) args[0];
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.getProperty("prop1"));
|
||||
assertEquals("bar", result.getProperty("prop2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesMethodWithPropertiesPayload() throws Exception {
|
||||
Method method = TestHandler.class.getMethod("propertiesMethod", Properties.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
Properties payload = new Properties();
|
||||
payload.setProperty("prop1", "foo");
|
||||
payload.setProperty("prop2", "bar");
|
||||
Message<?> message = new GenericMessage<Properties>(payload);
|
||||
message.getHeader().setProperty("prop1", "not");
|
||||
message.getHeader().setProperty("prop2", "these");
|
||||
Object[] args = (Object[]) mapper.mapMessage(message);
|
||||
Properties result = (Properties) args[0];
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("foo", result.getProperty("prop1"));
|
||||
assertEquals("bar", result.getProperty("prop2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMapMethodWithNonMapPayload() throws Exception {
|
||||
Method method = TestHandler.class.getMethod("mapMethod", Map.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
Message<?> message = new StringMessage("test");
|
||||
message.getHeader().setAttribute("attrib1", new Integer(123));
|
||||
message.getHeader().setAttribute("attrib2", new Integer(456));
|
||||
Object[] args = (Object[]) mapper.mapMessage(message);
|
||||
Map<String, Integer> result = (HashMap<String, Integer>) args[0];
|
||||
assertEquals(2, result.size());
|
||||
assertEquals(new Integer(123), result.get("attrib1"));
|
||||
assertEquals(new Integer(456), result.get("attrib2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMapMethodWithMapPayload() throws Exception {
|
||||
Method method = TestHandler.class.getMethod("mapMethod", Map.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
Map<String, Integer> payload = new HashMap<String, Integer>();
|
||||
payload.put("attrib1", new Integer(123));
|
||||
payload.put("attrib2", new Integer(456));
|
||||
Message<?> message = new GenericMessage<Map>(payload);
|
||||
message.getHeader().setAttribute("attrib1", "not");
|
||||
message.getHeader().setProperty("attrib2", "these");
|
||||
Object[] args = (Object[]) mapper.mapMessage(message);
|
||||
Map<String, Integer> result = (Map<String, Integer>) args[0];
|
||||
assertEquals(2, result.size());
|
||||
assertEquals(new Integer(123), result.get("attrib1"));
|
||||
assertEquals(new Integer(456), result.get("attrib2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageOnlyWithAdapter() throws Exception {
|
||||
TestHandler handler = new TestHandler();
|
||||
@@ -101,6 +170,45 @@ public class AnnotationMethodMessageMapperTests {
|
||||
assertEquals("foo", result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPayloadWithAdapter() throws Exception {
|
||||
TestHandler handler = new TestHandler();
|
||||
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter();
|
||||
adapter.setObject(handler);
|
||||
adapter.setMethod(method);
|
||||
adapter.setMessageMapper(mapper);
|
||||
Message<?> result = adapter.handle(new GenericMessage<Integer>(new Integer(123)));
|
||||
assertEquals(new Integer(123), result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertedPayloadWithAdapter() throws Exception {
|
||||
TestHandler handler = new TestHandler();
|
||||
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter();
|
||||
adapter.setObject(handler);
|
||||
adapter.setMethod(method);
|
||||
adapter.setMessageMapper(mapper);
|
||||
Message<?> result = adapter.handle(new StringMessage("456"));
|
||||
assertEquals(new Integer(456), result.getPayload());
|
||||
}
|
||||
|
||||
@Test(expected=MessagingException.class)
|
||||
public void testConversionFailureWithAdapter() throws Exception {
|
||||
TestHandler handler = new TestHandler();
|
||||
Method method = handler.getClass().getMethod("integerMethod", Integer.class);
|
||||
AnnotationMethodMessageMapper mapper = new AnnotationMethodMessageMapper(method);
|
||||
DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter();
|
||||
adapter.setObject(handler);
|
||||
adapter.setMethod(method);
|
||||
adapter.setMessageMapper(mapper);
|
||||
Message<?> result = adapter.handle(new StringMessage("foo"));
|
||||
assertEquals(new Integer(123), result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageAndHeaderWithAdapter() throws Exception {
|
||||
TestHandler handler = new TestHandler();
|
||||
@@ -170,6 +278,22 @@ public class AnnotationMethodMessageMapperTests {
|
||||
return prop;
|
||||
}
|
||||
|
||||
@Handler
|
||||
public Properties propertiesMethod(Properties properties) {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Handler
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map mapMethod(Map map) {
|
||||
return map;
|
||||
}
|
||||
|
||||
@Handler
|
||||
public Integer integerMethod(Integer i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user