INT-1752 added initial support for allowing RestTemplate to be specified on HTTP outbound adapters/gateways

This commit is contained in:
Oleg Zhurakousky
2011-01-28 15:54:28 -05:00
parent fba272adad
commit c4fe6f1c47
5 changed files with 90 additions and 7 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.util.xml.DomUtils;
* Parser for the 'outbound-channel-adapter' element of the http namespace.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@@ -46,7 +47,18 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
builder.addPropertyValue("expectReply", false);
builder.addConstructorArgValue(element.getAttribute("url"));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){
HttpParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addPropertyReference("restTemplate", restTemplate);
}
else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
}
String headerMapper = element.getAttribute("header-mapper");
String mappedRequestHeaders = element.getAttribute("mapped-request-headers");
if (StringUtils.hasText(headerMapper)) {
@@ -66,8 +78,6 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "charset");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expected-response-type");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
List<Element> uriVariableElements = DomUtils.getChildElementsByTagName(element, "uri-variable");
if (!CollectionUtils.isEmpty(uriVariableElements)) {
Map<String, String> uriVariableExpressions = new HashMap<String, String>();

View File

@@ -48,7 +48,19 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
"org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler");
builder.addConstructorArgValue(element.getAttribute("url"));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){
HttpParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addPropertyReference("restTemplate", restTemplate);
}
else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
}
String headerMapper = element.getAttribute("header-mapper");
String mappedRequestHeaders = element.getAttribute("mapped-request-headers");
String mappedResponseHeaders = element.getAttribute("mapped-response-headers");
@@ -71,8 +83,8 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-request-payload", "extractPayload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expected-response-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "request-timeout", "sendTimeout");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory");
//IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel", "outputChannel");
List<Element> uriVariableElements = DomUtils.getChildElementsByTagName(element, "uri-variable");
if (!CollectionUtils.isEmpty(uriVariableElements)) {

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2011 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.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.xml.ParserContext;
/**
* @author Oleg Zhurakousky
* @since 2.0.2
*/
public class HttpParsingUtils {
private static final String[] REST_TEMPLATE_ATTRIBUTES = {
"request-factory", "error-handler", "message-converters"
};
static void verifyNoRestTemplateAttributes(Element element, ParserContext parserContext) {
for (String attributeName : REST_TEMPLATE_ATTRIBUTES) {
if (element.hasAttribute(attributeName)) {
parserContext.getReaderContext().error("When providing a 'rest-template' reference, the '"
+ attributeName + "' attribute is not allowed", parserContext.extractSource(element));
}
}
}
}

View File

@@ -94,7 +94,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private final Map<String, Expression> uriVariableExpressions = new HashMap<String, Expression>();
private final RestTemplate restTemplate = new RestTemplate();
private volatile RestTemplate restTemplate = new RestTemplate();
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
@@ -115,6 +115,9 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
this.uri = uri;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
/**
* Specify the {@link HttpMethod} for requests. The default method will be POST.

View File

@@ -234,6 +234,15 @@ The String "HTTP_REQUEST_HEADERS" will match against any of the standard HTTP Re
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="rest-template" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.web.client.RestTemplate" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="charset" type="xsd:string" />
<xsd:attribute name="extract-payload" type="xsd:string" />
<xsd:attribute name="expected-response-type" type="xsd:string">
@@ -362,6 +371,15 @@ The String "HTTP_REQUEST_HEADERS" will match against any of the standard HTTP Re
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="rest-template" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.web.client.RestTemplate" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapped-request-headers" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[