INT-1111. Added support for enriching headers when mapping gateway methods

This commit is contained in:
Oleg Zhurakousky
2010-05-01 21:45:55 +00:00
parent 4b0ab1cf2f
commit 31970fcf11
9 changed files with 273 additions and 64 deletions

View File

@@ -17,11 +17,15 @@
package org.springframework.integration.config.xml;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.gateway.GatewayMethodDefinition;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
@@ -30,6 +34,7 @@ import org.w3c.dom.Element;
* Parser for the <gateway/> element.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
@@ -76,20 +81,40 @@ public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, attributeName);
}
List<Element> elements = DomUtils.getChildElementsByTagName(element, "method");
ManagedMap<String, GatewayMethodDefinition> methodToChannelMap = null;
ManagedMap<String, BeanDefinition> methodToChannelMap = null;
if (elements != null && elements.size() > 0){
methodToChannelMap = new ManagedMap<String, GatewayMethodDefinition>();
methodToChannelMap = new ManagedMap<String, BeanDefinition>();
}
for (Element methodElement : elements) {
String methodName = methodElement.getAttribute("name");
GatewayMethodDefinition gatewayDefinition = new GatewayMethodDefinition();
gatewayDefinition.setRequestChannelName(methodElement.getAttribute("request-channel"));
gatewayDefinition.setReplyChannelName(methodElement.getAttribute("reply-channel"));
gatewayDefinition.setRequestTimeout(methodElement.getAttribute("request-timeout"));
gatewayDefinition.setReplyTimeout(methodElement.getAttribute("reply-timeout"));
methodToChannelMap.put(methodName, gatewayDefinition);
BeanDefinitionBuilder gatewayDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodDefinition.class);
gatewayDefinitionBuilder.addPropertyValue("requestChannelName", methodElement.getAttribute("request-channel"));
gatewayDefinitionBuilder.addPropertyValue("replyChannelName", methodElement.getAttribute("reply-channel"));
gatewayDefinitionBuilder.addPropertyValue("requestTimeout", methodElement.getAttribute("request-timeout"));
gatewayDefinitionBuilder.addPropertyValue("replyTimeout", methodElement.getAttribute("reply-timeout"));
List<Element> invocationHeaders = DomUtils.getChildElementsByTagName(methodElement, "header");
if (!CollectionUtils.isEmpty(invocationHeaders)){
this.setMethodInvocationHeaders(gatewayDefinitionBuilder, invocationHeaders);
}
methodToChannelMap.put(methodName, gatewayDefinitionBuilder.getBeanDefinition());
}
builder.addPropertyValue("methodToChannelMap", methodToChannelMap);
}
/*
*
*/
private void setMethodInvocationHeaders(BeanDefinitionBuilder gatewayDefinitionBuilder, List<Element> invocationHeaders){
Map<String, Object> methodInvocationHeaders = new ManagedMap<String, Object>();
for (Element headerElement : invocationHeaders) {
String name = headerElement.getAttribute("name");
if (name.startsWith(MessageHeaders.PREFIX)){
throw new IllegalArgumentException("Attempting to set header: " + name + ". Prefix: '"
+ MessageHeaders.PREFIX + "' is reservered for SI internal use");
} else {
methodInvocationHeaders.put(name, headerElement.getAttribute("value"));
}
}
gatewayDefinitionBuilder.addPropertyValue("staticHeaders", methodInvocationHeaders);
}
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.integration.gateway;
import java.util.HashMap;
import java.util.Map;
/**
* Represents the definition of Gateway methods, when using multiple methos per
* Gateway interface <br>
@@ -29,6 +32,13 @@ public class GatewayMethodDefinition {
private String replyChannelName;
private String requestTimeout;
private String replyTimeout;
private Map<String, Object> staticHeaders = new HashMap<String, Object>();
public Map<String, Object> getStaticHeaders() {
return staticHeaders;
}
public void setStaticHeaders(Map<String, Object> staticHeaders) {
this.staticHeaders = staticHeaders;
}
public String getRequestChannelName() {
return requestChannelName;
}

View File

@@ -252,17 +252,12 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
}
private SimpleMessagingGateway createGatewayForMethod(Method method) {
SimpleMessagingGateway gateway = new SimpleMessagingGateway(
new ArgumentArrayMessageMapper(method), new SimpleMessageMapper());
if (this.getTaskScheduler() != null) {
gateway.setTaskScheduler(this.getTaskScheduler());
}
gateway.setBeanName(this.getComponentName());
Gateway gatewayAnnotation = method.getAnnotation(Gateway.class);
MessageChannel requestChannel = this.defaultRequestChannel;
MessageChannel replyChannel = this.defaultReplyChannel;
long requestTimeout = this.defaultRequestTimeout;
long replyTimeout = this.defaultReplyTimeout;
Map<String, Object> staticHeaders = null;
if (gatewayAnnotation != null) {
Assert.state(this.getChannelResolver() != null, "ChannelResolver is required");
String requestChannelName = gatewayAnnotation.requestChannel();
@@ -272,10 +267,11 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
requestTimeout = gatewayAnnotation.requestTimeout();
replyTimeout = gatewayAnnotation.replyTimeout();
}
else if (methodToChannelMap != null && methodToChannelMap.size() > 0) {
else if (methodToChannelMap != null && methodToChannelMap.size() > 0) {
Assert.state(this.getChannelResolver() != null, "ChannelResolver is required");
GatewayMethodDefinition gatewayDefinition = methodToChannelMap.get(method.getName());
GatewayMethodDefinition gatewayDefinition = methodToChannelMap.get(method.getName());
if (gatewayDefinition != null) {
staticHeaders = gatewayDefinition.getStaticHeaders();
String requestChannelName = gatewayDefinition.getRequestChannelName();
requestChannel = this.resolveChannel(requestChannel, requestChannelName);
String replyChannelName = gatewayDefinition.getReplyChannelName();
@@ -290,6 +286,12 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
}
}
}
ArgumentArrayMessageMapper messageMapper = new ArgumentArrayMessageMapper(method, staticHeaders);
SimpleMessagingGateway gateway = new SimpleMessagingGateway(messageMapper, new SimpleMessageMapper());
if (this.getTaskScheduler() != null) {
gateway.setTaskScheduler(this.getTaskScheduler());
}
gateway.setBeanName(this.getComponentName());
gateway.setRequestChannel(requestChannel);
gateway.setReplyChannel(replyChannel);
gateway.setRequestTimeout(requestTimeout);

View File

@@ -18,28 +18,24 @@ package org.springframework.integration.handler;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Headers;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.message.InboundMessageMapper;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -101,34 +97,23 @@ import org.springframework.util.StringUtils;
*/
public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]> {
private static ConversionService conversionService;
static { // see INT-829
conversionService = ConversionServiceFactory.createDefaultConversionService();
ConverterRegistry registry = (ConverterRegistry) conversionService;
registry.removeConvertible(Object.class, Map.class);
registry.removeConvertible(Map.class, Object.class);
registry.removeConvertible(Object.class, String.class);
registry.addConverter(new Converter<Number, String>() {
public String convert(Number source) { return source.toString(); }
});
registry.addConverter(new Converter<Date, String>() {
public String convert(Date source) { return source.toString(); }
});
}
private Map<String, Object> staticHeaders;
private final Method method;
private final List<MethodParameter> parameterList;
public ArgumentArrayMessageMapper(Method method) {
this(method, null);
}
public ArgumentArrayMessageMapper(Method method, Map<String, Object> staticHeaders) {
Assert.notNull(method, "method must not be null");
this.method = method;
this.staticHeaders = staticHeaders;
this.parameterList = this.getMethodParameterList(method);
}
public Message<?> toMessage(Object[] arguments) {
Assert.notNull(arguments, "cannot map null arguments to Message");
if (arguments.length != this.parameterList.size()) {
@@ -163,6 +148,10 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
else if (annotation.annotationType().equals(Header.class)) {
Header headerAnnotation = (Header) annotation;
String headerName = this.determineHeaderName(headerAnnotation, methodParameter);
if (headerName.startsWith(MessageHeaders.PREFIX)){
throw new IllegalArgumentException("Attempting to set header: " + headerName + ". Prefix: '"
+ MessageHeaders.PREFIX + "' is reservered for SI internal use");
}
if (headerAnnotation.required() && argumentValue == null) {
throw new IllegalArgumentException("Received null argument value for required header: '" + headerName + "'");
}
@@ -201,6 +190,9 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
? MessageBuilder.fromMessage((Message<?>) messageOrPayload)
: MessageBuilder.withPayload(messageOrPayload);
builder.copyHeadersIfAbsent(headers);
if (!CollectionUtils.isEmpty(staticHeaders)){
builder.copyHeaders(staticHeaders);
}
return builder.build();
}
@@ -259,27 +251,4 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
}
return parameterList;
}
@SuppressWarnings("unchecked")
public void validateMessageMapppings(Message<?> message) {
// Validate against a Map with no annotations
if (message.getPayload() instanceof Map) {
boolean foundOneMatch = false;
for (MethodParameter parameter : this.parameterList) {
String name = parameter.getParameterName();
Class<?> type = parameter.getParameterType();
if (parameter.getParameterAnnotations().length == 0 &&
!(name.equals("payload") || name.equals("headers")) &&
(type.isAssignableFrom(Properties.class) || type.isAssignableFrom(Map.class))) {
if (foundOneMatch) {
throw new IllegalArgumentException("Ambiguous parameters. " +
"Cannot determine parameter mappings between Method: [" + method + "] and Message: " + message +
". Try annotating individual parameters with @Payload, @Header, or @Headers");
}
foundOneMatch = true;
}
}
}
}
}

View File

@@ -311,6 +311,14 @@
<xsd:sequence>
<xsd:element name="method" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="header" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="value" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>