INT-1349 Added support for <uri-variable name="x" expression="y"/> sub-elements within the HTTP outbound adapters. Map payloads no longer provide values for the URI template placeholder replacements.

This commit is contained in:
Mark Fisher
2010-08-16 20:43:30 +00:00
parent 3128010981
commit 2955ef989e
12 changed files with 217 additions and 229 deletions

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2008 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.http;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.springframework.integration.core.GenericMessage;
/**
* @author Dave Syer
* @since 2.0
*
*/
public class DefaultParameterExtractorTests {
@Test
public void testFromMessage() throws Exception {
DefaultParameterExtractor mapper = new DefaultParameterExtractor();
Map<String, ?> params = mapper.fromMessage(new GenericMessage<Object>(Collections.singletonMap("foo", "bar")));
assertEquals(1, params.size());
assertEquals("bar", params.get("foo"));
}
@Test
public void testFromMessageWithExpressions() throws Exception {
DefaultParameterExtractor mapper = new DefaultParameterExtractor();
mapper.setDynamicParameterExpressions(Collections.singletonMap("foo", "payload"));
Map<String, ?> params = mapper.fromMessage(new GenericMessage<Object>("bar"));
assertEquals(1, params.size());
assertEquals("bar", params.get("foo"));
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.http;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.integration.Message;
import org.springframework.integration.core.GenericMessage;
/**
* @author Dave Syer
* @author Mark Fisher
* @since 2.0
*/
public class UriVariableExpressionTests {
@Test
public void testFromMessageWithExpressions() throws Exception {
final AtomicReference<URI> uriHolder = new AtomicReference<URI>();
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://test/{foo}");
handler.setUriVariableExpressions(Collections.singletonMap("foo", "payload"));
handler.setRequestFactory(new SimpleClientHttpRequestFactory() {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
uriHolder.set(uri);
throw new RuntimeException("intentional");
}
});
Message<?> message = new GenericMessage<Object>("bar");
Exception exception = null;
try {
handler.handleMessage(message);
}
catch (Exception e) {
exception = e;
}
assertEquals("intentional", exception.getCause().getMessage());
assertEquals("http://test/bar", uriHolder.get().toString());
}
}

View File

@@ -15,22 +15,21 @@
<outbound-channel-adapter id="minimalConfig" url="http://localhost/test1" channel="requests"/>
<outbound-channel-adapter id="fullConfig"
url="http://localhost/test2"
parameter-extractor="testParameterMapper"
http-method="GET"
channel="requests"
charset="UTF-8"
message-converters="converterList"
extract-payload="false"
expected-response-type="java.lang.Boolean"
request-factory="testRequestFactory"
order="77"
auto-startup="false"/>
url="http://localhost/test2/{foo}"
http-method="GET"
channel="requests"
charset="UTF-8"
message-converters="converterList"
extract-payload="false"
expected-response-type="java.lang.Boolean"
request-factory="testRequestFactory"
order="77"
auto-startup="false">
<uri-variable name="foo" expression="headers.bar"/>
</outbound-channel-adapter>
<beans:bean id="testRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory"/>
<beans:bean id="testParameterMapper" class="org.springframework.integration.http.DefaultParameterExtractor"/>
<util:list id="converterList">
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>

View File

@@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -27,6 +29,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -34,7 +37,6 @@ import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.http.DefaultOutboundRequestMapper;
import org.springframework.integration.http.HttpRequestExecutingMessageHandler;
import org.springframework.integration.http.OutboundRequestMapper;
import org.springframework.integration.http.ParameterExtractor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -54,9 +56,6 @@ public class HttpOutboundChannelAdapterParserTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ParameterExtractor parameterExtractor;
@Test
public void minimalConfig() {
@@ -80,6 +79,7 @@ public class HttpOutboundChannelAdapterParserTests {
}
@Test
@SuppressWarnings("unchecked")
public void fullConfig() {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.fullConfigEndpoint);
HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
@@ -101,11 +101,14 @@ public class HttpOutboundChannelAdapterParserTests {
Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory");
assertEquals(requestFactoryBean, requestFactory);
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
assertEquals("http://localhost/test2", handlerAccessor.getPropertyValue("uri"));
assertEquals("http://localhost/test2/{foo}", handlerAccessor.getPropertyValue("uri"));
assertEquals(HttpMethod.GET, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
assertEquals(parameterExtractor, handlerAccessor.getPropertyValue("parameterExtractor"));
Map<String, Expression> uriVariableExpressions =
(Map<String, Expression>) handlerAccessor.getPropertyValue("uriVariableExpressions");
assertEquals(1, uriVariableExpressions.size());
assertEquals("headers.bar", uriVariableExpressions.get("foo").getExpressionString());
}
}

View File

@@ -21,24 +21,23 @@
</si:channel>
<outbound-gateway id="fullConfig"
url="http://localhost/test2"
parameter-extractor="testParameterMapper"
http-method="PUT"
request-channel="requests"
request-factory="testRequestFactory"
request-timeout="1234"
message-converters="converterList"
extract-request-payload="false"
expected-response-type="java.lang.String"
reply-channel="replies"
charset="UTF-8"
order="77"
auto-startup="false"/>
url="http://localhost/test2"
http-method="PUT"
request-channel="requests"
request-factory="testRequestFactory"
request-timeout="1234"
message-converters="converterList"
extract-request-payload="false"
expected-response-type="java.lang.String"
reply-channel="replies"
charset="UTF-8"
order="77"
auto-startup="false">
<uri-variable name="foo" expression="headers.bar"/>
</outbound-gateway>
<beans:bean id="testRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory"/>
<beans:bean id="testParameterMapper" class="org.springframework.integration.http.DefaultParameterExtractor"/>
<util:list id="converterList">
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>

View File

@@ -21,6 +21,8 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -28,6 +30,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -36,7 +39,6 @@ import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.http.DefaultOutboundRequestMapper;
import org.springframework.integration.http.HttpRequestExecutingMessageHandler;
import org.springframework.integration.http.OutboundRequestMapper;
import org.springframework.integration.http.ParameterExtractor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -56,8 +58,6 @@ public class HttpOutboundGatewayParserTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ParameterExtractor parameterExtractor;
@Test
public void minimalConfig() {
@@ -83,6 +83,7 @@ public class HttpOutboundGatewayParserTests {
}
@Test
@SuppressWarnings("unchecked")
public void fullConfig() throws Exception {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.fullConfigEndpoint);
HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
@@ -114,7 +115,10 @@ public class HttpOutboundGatewayParserTests {
Object sendTimeout = new DirectFieldAccessor(
handlerAccessor.getPropertyValue("messagingTemplate")).getPropertyValue("sendTimeout");
assertEquals(new Long("1234"), sendTimeout);
assertEquals(parameterExtractor, handlerAccessor.getPropertyValue("parameterExtractor"));
Map<String, Expression> uriVariableExpressions =
(Map<String, Expression>) handlerAccessor.getPropertyValue("uriVariableExpressions");
assertEquals(1, uriVariableExpressions.size());
assertEquals("headers.bar", uriVariableExpressions.get("foo").getExpressionString());
}
}