Removed hardcoded "restTemplate" references.

This commit is contained in:
Mark Fisher
2011-09-26 07:55:49 -04:00
parent 09e053e95f
commit 331e503e9d
5 changed files with 29 additions and 30 deletions

View File

@@ -26,12 +26,12 @@ import org.springframework.beans.factory.xml.ParserContext;
*/ */
abstract class HttpAdapterParsingUtils { abstract class HttpAdapterParsingUtils {
private static final String[] REST_TEMPLATE_ATTRIBUTES = { static final String[] REST_TEMPLATE_REFERENCE_ATTRIBUTES = {
"request-factory", "error-handler", "message-converters" "request-factory", "error-handler", "message-converters"
}; };
static void verifyNoRestTemplateAttributes(Element element, ParserContext parserContext) { static void verifyNoRestTemplateAttributes(Element element, ParserContext parserContext) {
for (String attributeName : REST_TEMPLATE_ATTRIBUTES) { for (String attributeName : REST_TEMPLATE_REFERENCE_ATTRIBUTES) {
if (element.hasAttribute(attributeName)) { if (element.hasAttribute(attributeName)) {
parserContext.getReaderContext().error("When providing a 'rest-template' reference, the '" parserContext.getReaderContext().error("When providing a 'rest-template' reference, the '"
+ attributeName + "' attribute is not allowed.", + attributeName + "' attribute is not allowed.",

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -49,14 +49,14 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
String restTemplate = element.getAttribute("rest-template"); String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){ if (StringUtils.hasText(restTemplate)) {
HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext); HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addConstructorArgReference("restTemplate"); builder.addConstructorArgReference(restTemplate);
} }
else { else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters"); for (String referenceAttributeName : HttpAdapterParsingUtils.REST_TEMPLATE_REFERENCE_ATTRIBUTES) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, referenceAttributeName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory"); }
} }
String headerMapper = element.getAttribute("header-mapper"); String headerMapper = element.getAttribute("header-mapper");

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -51,14 +51,14 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "http-method");
String restTemplate = element.getAttribute("rest-template"); String restTemplate = element.getAttribute("rest-template");
if (StringUtils.hasText(restTemplate)){ if (StringUtils.hasText(restTemplate)) {
HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext); HttpAdapterParsingUtils.verifyNoRestTemplateAttributes(element, parserContext);
builder.addConstructorArgReference("restTemplate"); builder.addConstructorArgReference(restTemplate);
} }
else { else {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters"); for (String referenceAttributeName : HttpAdapterParsingUtils.REST_TEMPLATE_REFERENCE_ATTRIBUTES) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-handler"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, referenceAttributeName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory"); }
} }
String headerMapper = element.getAttribute("header-mapper"); String headerMapper = element.getAttribute("header-mapper");

View File

@@ -14,9 +14,9 @@
<outbound-channel-adapter id="minimalConfig" url="http://localhost/test1" channel="requests"/> <outbound-channel-adapter id="minimalConfig" url="http://localhost/test1" channel="requests"/>
<outbound-channel-adapter id="restTemplateConfig" url="http://localhost/test1" channel="requests" rest-template="restTemplate"/> <outbound-channel-adapter id="restTemplateConfig" url="http://localhost/test1" channel="requests" rest-template="customRestTemplate"/>
<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/> <beans:bean id="customRestTemplate" class="org.springframework.web.client.RestTemplate"/>
<outbound-channel-adapter id="fullConfig" <outbound-channel-adapter id="fullConfig"
url="http://localhost/test2/{foo}" url="http://localhost/test2/{foo}"

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@@ -55,27 +54,27 @@ import org.springframework.web.client.RestTemplate;
public class HttpOutboundChannelAdapterParserTests { public class HttpOutboundChannelAdapterParserTests {
@Autowired @Qualifier("minimalConfig") @Autowired @Qualifier("minimalConfig")
private AbstractEndpoint minimalConfigEndpoint; private AbstractEndpoint minimalConfig;
@Autowired @Qualifier("fullConfig") @Autowired @Qualifier("fullConfig")
private AbstractEndpoint fullConfigEndpoint; private AbstractEndpoint fullConfig;
@Autowired @Qualifier("restTemplateConfig") @Autowired @Qualifier("restTemplateConfig")
private AbstractEndpoint restTemplateConfig; private AbstractEndpoint restTemplateConfig;
@Autowired @Qualifier("customRestTemplate")
private RestTemplate customRestTemplate;
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Autowired
private RestTemplate restTemplate;
@Test @Test
public void minimalConfig() { public void minimalConfig() {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.minimalConfigEndpoint); DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.minimalConfig);
RestTemplate rTemplate = RestTemplate restTemplate =
TestUtils.getPropertyValue(this.minimalConfigEndpoint, "handler.restTemplate", RestTemplate.class); TestUtils.getPropertyValue(this.minimalConfig, "handler.restTemplate", RestTemplate.class);
assertNotSame(restTemplate, rTemplate); assertNotSame(customRestTemplate, restTemplate);
HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler"); HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(false, handlerAccessor.getPropertyValue("expectReply")); assertEquals(false, handlerAccessor.getPropertyValue("expectReply"));
@@ -94,7 +93,7 @@ public class HttpOutboundChannelAdapterParserTests {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void fullConfig() { public void fullConfig() {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.fullConfigEndpoint); DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.fullConfig);
HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler"); HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(false, handlerAccessor.getPropertyValue("expectReply")); assertEquals(false, handlerAccessor.getPropertyValue("expectReply"));
@@ -132,9 +131,9 @@ public class HttpOutboundChannelAdapterParserTests {
@Test @Test
public void restTemplateConfig() { public void restTemplateConfig() {
RestTemplate rTemplate = RestTemplate restTemplate =
TestUtils.getPropertyValue(this.restTemplateConfig, "handler.restTemplate", RestTemplate.class); TestUtils.getPropertyValue(this.restTemplateConfig, "handler.restTemplate", RestTemplate.class);
assertEquals(restTemplate, rTemplate); assertEquals(customRestTemplate, restTemplate);
} }
@Test(expected=BeanDefinitionParsingException.class) @Test(expected=BeanDefinitionParsingException.class)