INT-1232: added ParameterMapper

This commit is contained in:
David Syer
2010-07-15 14:35:04 +00:00
parent 5db5f5fb7a
commit 8311a10975
12 changed files with 209 additions and 19 deletions

View File

@@ -0,0 +1,76 @@
/*
* 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 java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.core.Message;
/**
* @author Dave Syer
* @since 2.0
*
*/
public class DefaultParameterMapper implements ParameterMapper {
private static Log logger = LogFactory.getLog(DefaultParameterMapper.class);
private SpelExpressionParser parser = new SpelExpressionParser();
private Map<String, Expression> dynamicParameterExpressions = new HashMap<String, Expression>();
/**
* A map of parameter name to SpEL expressions on the message.
*
* @param dynamicParameterExpressions the dynamic parameter expressions to set
*/
public void setDynamicParameterExpressions(Map<String, String> dynamicParameterExpressions) {
this.dynamicParameterExpressions.clear();
for (String key : dynamicParameterExpressions.keySet()) {
this.dynamicParameterExpressions.put(key, parser.parseExpression(dynamicParameterExpressions.get(key)));
}
}
public Map<String, ?> fromMessage(Message<?> requestMessage) {
Map<String, Object> params = new HashMap<String, Object>();
for (String key : dynamicParameterExpressions.keySet()) {
Object value = dynamicParameterExpressions.get(key).getValue(requestMessage);
params.put(key, value);
}
if (requestMessage.getPayload() instanceof Map<?,?>) {
Map<?,?> payloadMap = (Map<?,?>) requestMessage.getPayload();
for (Object key : payloadMap.keySet()) {
if (key instanceof String) {
params.put((String) key, payloadMap.get(key).toString());
}
else if (logger.isDebugEnabled()) {
logger.debug("ignoring Map value for non-String key: " + key);
}
}
}
return params;
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.integration.http;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -57,6 +56,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private final RestTemplate restTemplate = new RestTemplate();
private ParameterMapper parameterMapper = new DefaultParameterMapper();
/**
* Create a handler that will send requests to the provided URI.
@@ -142,12 +142,21 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
this.restTemplate.setRequestFactory(requestFactory);
}
/**
* Set the {@link ParameterMapper} for creating URI parameters from the outbound message.
*
* @param parameterMapper the parameter mapper to set
*/
public void setParameterMapper(ParameterMapper parameterMapper) {
this.parameterMapper = parameterMapper;
}
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
try {
// TODO: allow a boolean flag for treating Map as queryParams vs. uriVariables?
Map<String, ?> uriVariables = this.determineUriVariables(requestMessage);
Map<String, ?> uriVariables = this.parameterMapper.fromMessage(requestMessage);
HttpEntity<?> httpRequest = this.requestMapper.fromMessage(requestMessage);
ResponseEntity<?> httpResponse = this.restTemplate.exchange(this.uri, this.httpMethod, httpRequest, this.expectedResponseType, uriVariables);
if (this.expectReply) {
@@ -171,20 +180,4 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
}
}
private Map<String, ?> determineUriVariables(Message<?> requestMessage) {
Map<String, Object> uriVariables = new HashMap<String, Object>();
if (requestMessage.getPayload() instanceof Map<?,?>) {
Map<?,?> payloadMap = (Map<?,?>) requestMessage.getPayload();
for (Object key : payloadMap.keySet()) {
if (key instanceof String) {
uriVariables.put((String) key, payloadMap.get(key).toString());
}
else if (logger.isDebugEnabled()) {
logger.debug("ignoring Map value for non-String key: " + key);
}
}
}
return uriVariables;
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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 java.util.Map;
import org.springframework.integration.core.Message;
/**
* @author Dave Syer
*
* @since 2.0
*
*/
public interface ParameterMapper {
/**
* @param requestMessage
* @return a map of parameters
*/
Map<String, ?> fromMessage(Message<?> requestMessage);
}

View File

@@ -42,6 +42,7 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
builder.addConstructorArgValue(element.getAttribute("url"));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "parameter-mapper");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "charset");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expected-response-type");

View File

@@ -45,6 +45,7 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
builder.addConstructorArgValue(element.getAttribute("url"));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "parameter-mapper");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "charset");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-request-payload", "extractPayload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expected-response-type");

View File

@@ -112,6 +112,15 @@
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="parameter-mapper" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.http.ParameterMapper"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="http-method">
<xsd:annotation>
<xsd:documentation>
@@ -195,6 +204,15 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="parameter-mapper" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.http.ParameterMapper"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="http-method">
<xsd:annotation>
<xsd:documentation>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.http.mapper;
package org.springframework.integration.http;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

View File

@@ -0,0 +1,50 @@
/*
* 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.message.GenericMessage;
/**
* @author Dave Syer
* @since 2.0
*
*/
public class DefaultParameterMapperTests {
@Test
public void testFromMessage() throws Exception {
DefaultParameterMapper mapper = new DefaultParameterMapper();
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 {
DefaultParameterMapper mapper = new DefaultParameterMapper();
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

@@ -16,6 +16,7 @@
<outbound-channel-adapter id="fullConfig"
url="http://localhost/test2"
parameter-mapper="testParameterMapper"
http-method="GET"
channel="requests"
charset="UTF-8"
@@ -28,6 +29,8 @@
<beans:bean id="testRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory"/>
<beans:bean id="testParameterMapper" class="org.springframework.integration.http.DefaultParameterMapper"/>
<util:list id="converterList">
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>

View File

@@ -34,6 +34,7 @@ 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.ParameterMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -53,6 +54,9 @@ public class HttpOutboundChannelAdapterParserTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ParameterMapper parameterMapper;
@Test
public void minimalConfig() {
@@ -101,6 +105,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertEquals(HttpMethod.GET, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
assertEquals(parameterMapper, handlerAccessor.getPropertyValue("parameterMapper"));
}
}

View File

@@ -22,6 +22,7 @@
<outbound-gateway id="fullConfig"
url="http://localhost/test2"
parameter-mapper="testParameterMapper"
http-method="PUT"
request-channel="requests"
request-factory="testRequestFactory"
@@ -36,6 +37,8 @@
<beans:bean id="testRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory"/>
<beans:bean id="testParameterMapper" class="org.springframework.integration.http.DefaultParameterMapper"/>
<util:list id="converterList">
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>
<beans:bean class="org.springframework.integration.http.config.StubHttpMessageConverter"/>

View File

@@ -36,6 +36,7 @@ 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.ParameterMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -55,6 +56,8 @@ public class HttpOutboundGatewayParserTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ParameterMapper parameterMapper;
@Test
public void minimalConfig() {
@@ -111,6 +114,7 @@ public class HttpOutboundGatewayParserTests {
Object sendTimeout = new DirectFieldAccessor(
handlerAccessor.getPropertyValue("channelTemplate")).getPropertyValue("sendTimeout");
assertEquals(new Long("1234"), sendTimeout);
assertEquals(parameterMapper, handlerAccessor.getPropertyValue("parameterMapper"));
}
}