INT-670 added support for 'payload-expression' on <gateway> element's <method> sub-elements
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -19,16 +19,15 @@ package org.springframework.integration.config.xml;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Parser for the <gateway/> element.
|
||||
@@ -88,11 +87,13 @@ public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
|
||||
}
|
||||
for (Element methodElement : elements) {
|
||||
String methodName = methodElement.getAttribute("name");
|
||||
BeanDefinitionBuilder gatewayDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodDefinition.class);
|
||||
BeanDefinitionBuilder gatewayDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.integration.gateway.GatewayMethodDefinition");
|
||||
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"));
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(gatewayDefinitionBuilder, methodElement, "payload-expression");
|
||||
List<Element> invocationHeaders = DomUtils.getChildElementsByTagName(methodElement, "header");
|
||||
if (!CollectionUtils.isEmpty(invocationHeaders)){
|
||||
this.setMethodInvocationHeaders(gatewayDefinitionBuilder, invocationHeaders);
|
||||
@@ -101,20 +102,13 @@ public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
|
||||
}
|
||||
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"));
|
||||
}
|
||||
methodInvocationHeaders.put(headerElement.getAttribute("name"), headerElement.getAttribute("value"));
|
||||
}
|
||||
gatewayDefinitionBuilder.addPropertyValue("staticHeaders", methodInvocationHeaders);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -13,55 +13,80 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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>
|
||||
* <si:method name="echo" request-channel="inputA" reply-timeout="2" request-timeout="200"/>
|
||||
* Represents the definition of Gateway methods, when using multiple methods per Gateway interface.
|
||||
* <si:method name="echo" request-channel="inputA" reply-timeout="2" request-timeout="200"/>
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0.M1
|
||||
* @since 2.0
|
||||
*/
|
||||
public class GatewayMethodDefinition {
|
||||
|
||||
private String requestChannelName;
|
||||
private String replyChannelName;
|
||||
private String requestTimeout;
|
||||
private String replyTimeout;
|
||||
private Map<String, Object> staticHeaders = new HashMap<String, Object>();
|
||||
private volatile String payloadExpression;
|
||||
|
||||
private volatile String requestChannelName;
|
||||
|
||||
private volatile String replyChannelName;
|
||||
|
||||
private volatile String requestTimeout;
|
||||
|
||||
private volatile String replyTimeout;
|
||||
|
||||
private volatile Map<String, Object> staticHeaders = new HashMap<String, Object>();
|
||||
|
||||
|
||||
public String getPayloadExpression() {
|
||||
return payloadExpression;
|
||||
}
|
||||
|
||||
public void setPayloadExpression(String payloadExpression) {
|
||||
this.payloadExpression = payloadExpression;
|
||||
}
|
||||
|
||||
public Map<String, Object> getStaticHeaders() {
|
||||
return staticHeaders;
|
||||
}
|
||||
|
||||
public void setStaticHeaders(Map<String, Object> staticHeaders) {
|
||||
this.staticHeaders = staticHeaders;
|
||||
}
|
||||
|
||||
public String getRequestChannelName() {
|
||||
return requestChannelName;
|
||||
}
|
||||
|
||||
public void setRequestChannelName(String requestChannelName) {
|
||||
this.requestChannelName = requestChannelName;
|
||||
}
|
||||
|
||||
public String getReplyChannelName() {
|
||||
return replyChannelName;
|
||||
}
|
||||
|
||||
public void setReplyChannelName(String replyChannelName) {
|
||||
this.replyChannelName = replyChannelName;
|
||||
}
|
||||
|
||||
public String getRequestTimeout() {
|
||||
return requestTimeout;
|
||||
}
|
||||
|
||||
public void setRequestTimeout(String requestTimeout) {
|
||||
this.requestTimeout = requestTimeout;
|
||||
}
|
||||
|
||||
public String getReplyTimeout() {
|
||||
return replyTimeout;
|
||||
}
|
||||
|
||||
public void setReplyTimeout(String replyTimeout) {
|
||||
this.replyTimeout = replyTimeout;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -266,6 +266,7 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
|
||||
MessageChannel replyChannel = this.defaultReplyChannel;
|
||||
long requestTimeout = this.defaultRequestTimeout;
|
||||
long replyTimeout = this.defaultReplyTimeout;
|
||||
String payloadExpression = null;
|
||||
Map<String, Object> staticHeaders = null;
|
||||
if (gatewayAnnotation != null) {
|
||||
Assert.state(this.getChannelResolver() != null, "ChannelResolver is required");
|
||||
@@ -280,6 +281,7 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
|
||||
Assert.state(this.getChannelResolver() != null, "ChannelResolver is required");
|
||||
GatewayMethodDefinition gatewayDefinition = methodToChannelMap.get(method.getName());
|
||||
if (gatewayDefinition != null) {
|
||||
payloadExpression = gatewayDefinition.getPayloadExpression();
|
||||
staticHeaders = gatewayDefinition.getStaticHeaders();
|
||||
String requestChannelName = gatewayDefinition.getRequestChannelName();
|
||||
requestChannel = this.resolveChannel(requestChannel, requestChannelName);
|
||||
@@ -296,7 +298,10 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
|
||||
}
|
||||
}
|
||||
ArgumentArrayMessageMapper messageMapper = new ArgumentArrayMessageMapper(method, staticHeaders);
|
||||
messageMapper.setBeanFactory(this.getBeanFactory());
|
||||
if (StringUtils.hasText(payloadExpression)) {
|
||||
messageMapper.setPayloadExpression(payloadExpression);
|
||||
}
|
||||
messageMapper.setBeanFactory(this.getBeanFactory());
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway(messageMapper, new SimpleMessageMapper());
|
||||
gateway.setExceptionMapper(exceptionMapper);
|
||||
if (this.getTaskScheduler() != null) {
|
||||
|
||||
@@ -113,7 +113,7 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
|
||||
|
||||
private final List<MethodParameter> parameterList;
|
||||
|
||||
private final Expression payloadExpression;
|
||||
private volatile Expression payloadExpression;
|
||||
|
||||
private final Map<String, Expression> parameterPayloadExpressions = new HashMap<String, Expression>();
|
||||
|
||||
@@ -135,6 +135,10 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
|
||||
}
|
||||
|
||||
|
||||
public void setPayloadExpression(String expressionString) {
|
||||
this.payloadExpression = PARSER.parseExpression(expressionString);
|
||||
}
|
||||
|
||||
public void setBeanFactory(final BeanFactory beanFactory) {
|
||||
if (beanFactory != null) {
|
||||
this.beanResolver = new BeanResolver() {
|
||||
|
||||
@@ -462,9 +462,9 @@
|
||||
<xsd:attribute name="name" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
<![CDATA[
|
||||
The name of the method
|
||||
]]>
|
||||
<![CDATA[
|
||||
The name of the method
|
||||
]]>
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
@@ -473,12 +473,21 @@
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="payload-expression" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
<![CDATA[
|
||||
Expression that should be evaluated to generate the payload.
|
||||
]]>
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="request-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
<![CDATA[
|
||||
Identifies channel the message will be sent to upon invocation of this method
|
||||
]]>
|
||||
<![CDATA[
|
||||
Identifies channel the message will be sent to upon invocation of this method
|
||||
]]>
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<int:gateway id="gateway" service-interface="org.springframework.integration.gateway.GatewayWithPayloadExpressionTests$SampleGateway">
|
||||
<int:method name="send1" request-channel="input" payload-expression="#args[0] + 'bar'"/>
|
||||
<int:method name="send2" request-channel="input" payload-expression="@testBean.sum(#args[0])"/>
|
||||
</int:gateway>
|
||||
|
||||
<int:channel id="input">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
<bean id="testBean" class="org.springframework.integration.gateway.GatewayWithPayloadExpressionTests$TestBean"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.gateway;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class GatewayWithPayloadExpressionTests {
|
||||
|
||||
@Autowired
|
||||
private SampleGateway gateway;
|
||||
|
||||
@Autowired
|
||||
private PollableChannel input;
|
||||
|
||||
|
||||
@Test
|
||||
public void simpleExpression() throws Exception {
|
||||
gateway.send1("foo");
|
||||
Message<?> result = input.receive(0);
|
||||
assertEquals("foobar", result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanResolvingExpression() throws Exception {
|
||||
gateway.send2("foo");
|
||||
Message<?> result = input.receive(0);
|
||||
assertEquals(324, result.getPayload());
|
||||
}
|
||||
|
||||
|
||||
public static interface SampleGateway {
|
||||
|
||||
void send1(String value);
|
||||
|
||||
void send2(String value);
|
||||
}
|
||||
|
||||
|
||||
public static class TestBean {
|
||||
|
||||
public int sum(String s) {
|
||||
int sum = 0;
|
||||
for (byte b : s.getBytes()) {
|
||||
sum += b;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd"
|
||||
xmlns:int="http://www.springframework.org/schema/integration">
|
||||
|
||||
<int:gateway id="faildGateway"
|
||||
service-interface="org.springframework.integration.gateway.HeaderEnrichedGatewayTests$SampleGateway">
|
||||
<int:method name="sendString" request-channel="input">
|
||||
<int:header name="foo" value="foo"/>
|
||||
<int:header name="$bar" value="bar"/>
|
||||
</int:method>
|
||||
</int:gateway>
|
||||
|
||||
<int:channel id="input"/>
|
||||
</beans>
|
||||
@@ -13,7 +13,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.gateway;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
|
||||
@@ -22,10 +24,8 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
@@ -41,15 +41,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class HeaderEnrichedGatewayTests {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gateway")
|
||||
private SampleGateway gateway;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("input")
|
||||
private DirectChannel input;
|
||||
|
||||
|
||||
private Object testPayload;
|
||||
|
||||
|
||||
@Test
|
||||
public void validateStaticHeaderMappings() throws Exception {
|
||||
MessageHandler handler = Mockito.mock(MessageHandler.class);
|
||||
@@ -70,19 +71,13 @@ public class HeaderEnrichedGatewayTests {
|
||||
gateway.sendStringWithParameterHeaders((String) testPayload, "headerA", "headerB");
|
||||
Mockito.verify(handler, Mockito.times(1)).handleMessage(Mockito.any(Message.class));
|
||||
}
|
||||
@Test(expected=BeanDefinitionStoreException.class)
|
||||
public void validateFailedGatewayHeaders() throws Exception {
|
||||
new ClassPathXmlApplicationContext("HeaderEnrichedGatewayTests-failed-context.xml", HeaderEnrichedGatewayTests.class);
|
||||
}
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void prepareHandlerForTest(MessageHandler handler){
|
||||
|
||||
|
||||
private void prepareHandlerForTest(MessageHandler handler) {
|
||||
Mockito.reset(handler);
|
||||
Mockito.doAnswer(new Answer() {
|
||||
Mockito.doAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) {
|
||||
Message message = (Message) invocation.getArguments()[0];
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
assertEquals(testPayload, message.getPayload());
|
||||
assertEquals("foo", message.getHeaders().get("foo"));
|
||||
assertEquals("bar", message.getHeaders().get("bar"));
|
||||
@@ -95,10 +90,10 @@ public class HeaderEnrichedGatewayTests {
|
||||
}})
|
||||
.when(handler).handleMessage(Mockito.any(Message.class));
|
||||
}
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
public static interface SampleGateway {
|
||||
|
||||
public void sendString(String value);
|
||||
|
||||
public void sendInteger(Integer value);
|
||||
@@ -106,4 +101,5 @@ public class HeaderEnrichedGatewayTests {
|
||||
public void sendStringWithParameterHeaders(String value,
|
||||
@Header("headerA") String headerA, @Header("headerB") String headerB);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user