diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpNamespaceHandler.java b/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpNamespaceHandler.java index 193116e914..28931f891a 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpNamespaceHandler.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpNamespaceHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * 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. @@ -29,6 +29,7 @@ public class HttpNamespaceHandler extends AbstractIntegrationNamespaceHandler { public void init() { this.registerBeanDefinitionParser("inbound-channel-adapter", new HttpInboundEndpointParser(false)); this.registerBeanDefinitionParser("inbound-gateway", new HttpInboundEndpointParser(true)); + this.registerBeanDefinitionParser("outbound-channel-adapter", new HttpOutboundChannelAdapterParser()); this.registerBeanDefinitionParser("outbound-gateway", new HttpOutboundGatewayParser()); } diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParser.java b/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParser.java new file mode 100644 index 0000000000..eb3ac18004 --- /dev/null +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParser.java @@ -0,0 +1,56 @@ +/* + * 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.config; + +import org.w3c.dom.Element; + +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.util.StringUtils; + +/** + * Parser for the 'outbound-channel-adapter' element of the http namespace. + * + * @author Mark Fisher + * @since 2.0 + */ +public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { + + private static final String PACKAGE_PATH = "org.springframework.integration.http"; + + @Override + protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + PACKAGE_PATH + ".HttpRequestExecutingMessageHandler"); + builder.addPropertyValue("expectReply", false); + String url = element.getAttribute("url"); + if (StringUtils.hasText(url)) { + builder.addConstructorArgValue(url); + } + BeanDefinitionBuilder mapperBuilder = BeanDefinitionBuilder.genericBeanDefinition( + PACKAGE_PATH + ".DefaultOutboundRequestMapper"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(mapperBuilder, element, "charset"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(mapperBuilder, element, "extract-payload"); + builder.addPropertyValue("requestMapper", mapperBuilder.getBeanDefinition()); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-factory"); + return builder.getBeanDefinition(); + } + +} diff --git a/spring-integration-http/src/main/resources/org/springframework/integration/http/config/spring-integration-http-2.0.xsd b/spring-integration-http/src/main/resources/org/springframework/integration/http/config/spring-integration-http-2.0.xsd index 7e2e7ba5d9..90cf080d5b 100644 --- a/spring-integration-http/src/main/resources/org/springframework/integration/http/config/spring-integration-http-2.0.xsd +++ b/spring-integration-http/src/main/resources/org/springframework/integration/http/config/spring-integration-http-2.0.xsd @@ -96,6 +96,59 @@ + + + + + Defines an outbound HTTP-based Channel Adapter. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Specifies whether this adapter should start automatically. + + + + + + diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests-context.xml b/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests-context.xml new file mode 100644 index 0000000000..1545d4a9c7 --- /dev/null +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests-context.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests.java new file mode 100644 index 0000000000..8934450558 --- /dev/null +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests.java @@ -0,0 +1,100 @@ +/* + * 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.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; + +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.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +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.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class HttpOutboundChannelAdapterParserTests { + + @Autowired @Qualifier("minimalConfig") + private AbstractEndpoint minimalConfigEndpoint; + + @Autowired @Qualifier("fullConfig") + private AbstractEndpoint fullConfigEndpoint; + + @Autowired + private ApplicationContext applicationContext; + + + @Test + public void minimalConfig() { + DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.minimalConfigEndpoint); + HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler"); + DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler); + assertEquals(false, handlerAccessor.getPropertyValue("expectReply")); + assertEquals(this.applicationContext.getBean("requests"), endpointAccessor.getPropertyValue("inputChannel")); + assertNull(handlerAccessor.getPropertyValue("outputChannel")); + OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper"); + DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate")); + ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory) + templateAccessor.getPropertyValue("requestFactory"); + assertTrue(mapper instanceof DefaultOutboundRequestMapper); + assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory); + DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper); + assertEquals("http://localhost/test1", handlerAccessor.getPropertyValue("defaultUri")); + assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset")); + assertEquals(true, mapperAccessor.getPropertyValue("extractPayload")); + } + + @Test + public void fullConfig() { + DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.fullConfigEndpoint); + HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler"); + DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler); + assertEquals(false, handlerAccessor.getPropertyValue("expectReply")); + assertEquals(this.applicationContext.getBean("requests"), endpointAccessor.getPropertyValue("inputChannel")); + assertNull(handlerAccessor.getPropertyValue("outputChannel")); + assertEquals(77, handlerAccessor.getPropertyValue("order")); + assertEquals(Boolean.FALSE, endpointAccessor.getPropertyValue("autoStartup")); + OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper"); + DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate")); + ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory) + templateAccessor.getPropertyValue("requestFactory"); + assertTrue(mapper instanceof DefaultOutboundRequestMapper); + assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory); + Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory"); + assertEquals(requestFactoryBean, requestFactory); + DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper); + assertEquals("http://localhost/test2", handlerAccessor.getPropertyValue("defaultUri")); + assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset")); + assertEquals(false, mapperAccessor.getPropertyValue("extractPayload")); + } + +}