Skip to content

INT-4448, INT-4449: Fix Gateway for no-arg method #2420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ addons:
- mongodb-3.0-precise
packages:
- mongodb-org-server
- oracle-java8-installer
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 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.
Expand Down Expand Up @@ -100,28 +100,33 @@ public BeanDefinitionHolder parse(Map<String, Object> gatewayAttributes) {
if (hasDefaultHeaders || hasDefaultPayloadExpression) {
BeanDefinitionBuilder methodMetadataBuilder =
BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class);

if (hasDefaultPayloadExpression) {
methodMetadataBuilder.addPropertyValue("payloadExpression", defaultPayloadExpression);
}
Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
for (Map<String, Object> header : defaultHeaders) {
String headerValue = (String) header.get("value");
String headerExpression = (String) header.get("expression");
boolean hasValue = StringUtils.hasText(headerValue);

if (hasValue == StringUtils.hasText(headerExpression)) {
throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " +
"is required on a gateway's header.");
}

BeanDefinition expressionDef =
new RootBeanDefinition(hasValue ? LiteralExpression.class : ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues()
.addGenericArgumentValue(hasValue ? headerValue : headerExpression);
if (hasDefaultHeaders) {
Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
for (Map<String, Object> header : defaultHeaders) {
String headerValue = (String) header.get("value");
String headerExpression = (String) header.get("expression");
boolean hasValue = StringUtils.hasText(headerValue);

if (hasValue == StringUtils.hasText(headerExpression)) {
throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " +
"is required on a gateway's header.");
}

headerExpressions.put((String) header.get("name"), expressionDef);
BeanDefinition expressionDef =
new RootBeanDefinition(hasValue ? LiteralExpression.class : ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues()
.addGenericArgumentValue(hasValue ? headerValue : headerExpression);

headerExpressions.put((String) header.get("name"), expressionDef);
}
methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);
}
methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);

gatewayProxyBuilder.addPropertyValue("globalMethodMetadata", methodMetadataBuilder.getBeanDefinition());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -487,10 +487,15 @@ private Object invokeGatewayMethod(MethodInvocation invocation, boolean runningO
int paramCount = method.getParameterTypes().length;
Object response = null;
boolean hasPayloadExpression = method.isAnnotationPresent(Payload.class);
if (!hasPayloadExpression && this.methodMetadataMap != null) {
if (!hasPayloadExpression) {
// check for the method metadata next
GatewayMethodMetadata metadata = this.methodMetadataMap.get(method.getName());
hasPayloadExpression = (metadata != null) && StringUtils.hasText(metadata.getPayloadExpression());
if (this.methodMetadataMap != null) {
GatewayMethodMetadata metadata = this.methodMetadataMap.get(method.getName());
hasPayloadExpression = (metadata != null) && StringUtils.hasText(metadata.getPayloadExpression());
}
else if (this.globalMethodMetadata != null) {
hasPayloadExpression = StringUtils.hasText(this.globalMethodMetadata.getPayloadExpression());
}
}
if (paramCount == 0 && !hasPayloadExpression) {
Long receiveTimeout = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</int:channel>

<int:gateway id="sampleGateway"
service-interface="org.springframework.integration.gateway.GatewayInterfaceTests.Bar"
service-interface="org.springframework.integration.gateway.GatewayInterfaceTests$Bar"
default-request-channel="requestChannelBaz"
error-channel="errorChannel">
<int:default-header name="name" expression="#gatewayMethod.name"/>
Expand All @@ -34,9 +34,14 @@
<int:channel id="requestChannelBaz"/>

<int:gateway id="customMappedGateway"
service-interface="org.springframework.integration.gateway.GatewayInterfaceTests.Baz"
service-interface="org.springframework.integration.gateway.GatewayInterfaceTests$Baz"
default-request-channel="requestChannelBaz" mapper="mapper"/>

<bean id="mapper" class="org.springframework.integration.gateway.GatewayInterfaceTests$BazMapper"/>

<int:gateway id="sampleGateway2"
service-interface="org.springframework.integration.gateway.GatewayInterfaceTests$NoArgumentsGateway"
default-request-channel="requestChannelBar"
default-payload-expression="'foo'"/>

</beans>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -75,6 +75,7 @@
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.context.IntegrationProperties;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
Expand Down Expand Up @@ -487,6 +488,26 @@ public void testIgnoredHeader() {
((SubscribableChannel) this.errorChannel).unsubscribe(messageHandler);
}

@Test
public void testGatewayWithNoArgsMethod() {
ConfigurableApplicationContext ac =
new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass());

DirectChannel channel = ac.getBean("requestChannelBar", DirectChannel.class);
channel.subscribe(new AbstractReplyProducingMessageHandler() {

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
assertEquals("foo", requestMessage.getPayload());
return "FOO";
}

});

NoArgumentsGateway noArgumentsGateway = ac.getBean(NoArgumentsGateway.class);
assertEquals("FOO", noArgumentsGateway.pullData());
ac.close();
}

public interface Foo {

Expand Down Expand Up @@ -520,6 +541,11 @@ public interface Baz {
void baz(String payload);
}

public interface NoArgumentsGateway {

String pullData();
}

public static class BazMapper implements MethodArgsMessageMapper {

@Override
Expand Down