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>