Added namespace support for http outbound-gateway (INT-616).

This commit is contained in:
Mark Fisher
2009-03-21 13:53:47 +00:00
parent d206a84e68
commit 901589bdd5
7 changed files with 320 additions and 5 deletions

View File

@@ -189,7 +189,7 @@ public class CommonsHttpRequestExecutor extends AbstractHttpRequestExecutor {
/**
* Set the given byte stream as the request body.
* <p>The default implementation simply sets the byte stream as the
* <p>This implementation simply sets the byte stream as the
* EntityEnclosingMethod's request body. This can be overridden, for
* example, to write a specific encoding and potentially set appropriate
* HTTP request headers.
@@ -201,7 +201,7 @@ public class CommonsHttpRequestExecutor extends AbstractHttpRequestExecutor {
* @see org.apache.commons.httpclient.methods.PostMethod#setRequestEntity
* @see org.apache.commons.httpclient.methods.InputStreamRequestEntity
*/
protected void setRequestBody(
private void setRequestBody(
EntityEnclosingMethod httpMethod, ByteArrayOutputStream baos, String contentType)
throws IOException {
httpMethod.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), contentType));

View File

@@ -29,6 +29,7 @@ public class HttpNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
this.registerBeanDefinitionParser("inbound-channel-adapter", new HttpInboundEndpointParser(false));
this.registerBeanDefinitionParser("inbound-gateway", new HttpInboundEndpointParser(true));
this.registerBeanDefinitionParser("outbound-gateway", new HttpOutboundGatewayParser());
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2002-2009 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.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.StringUtils;
/**
* Parser for the 'outbound-gateway' element of the http namespace.
*
* @author Mark Fisher
*/
public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
private static final String PACKAGE_PATH = "org.springframework.integration.http";
@Override
protected String getInputChannelAttributeName() {
return "request-channel";
}
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
String defaultUrl = element.getAttribute("default-url");
String charset = element.getAttribute("charset");
String extractPayload = element.getAttribute("extract-request-payload");
String requestMapperRef = element.getAttribute("request-mapper");
if (StringUtils.hasText(requestMapperRef)) {
if (StringUtils.hasText(defaultUrl)) {
this.requestMapperConflictError("default-url", parserContext, element);
return null;
}
else if (StringUtils.hasText(charset)) {
this.requestMapperConflictError("charset", parserContext, element);
return null;
}
else if (StringUtils.hasText(extractPayload)) {
this.requestMapperConflictError("extract-request-payload", parserContext, element);
return null;
}
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
PACKAGE_PATH + ".HttpOutboundEndpoint");
if (!StringUtils.hasText(requestMapperRef)) {
BeanDefinitionBuilder mapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(
PACKAGE_PATH + ".DefaultOutboundRequestMapper");
if (StringUtils.hasText(defaultUrl)) {
mapperBuilder.addConstructorArgValue(defaultUrl);
}
if (StringUtils.hasText(charset)) {
mapperBuilder.addPropertyValue("charset", charset);
}
if (StringUtils.hasText(extractPayload)) {
mapperBuilder.addPropertyValue("extractPayload", extractPayload);
}
requestMapperRef = BeanDefinitionReaderUtils.registerWithGeneratedName(
mapperBuilder.getBeanDefinition(), parserContext.getRegistry());
}
builder.addPropertyReference("requestMapper", requestMapperRef);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "request-timeout", "sendTimeout");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-executor");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel", "outputChannel");
return builder;
}
private void requestMapperConflictError(String nameForGateway, ParserContext parserContext, Element element) {
parserContext.getReaderContext().error("The '" + nameForGateway + "' and 'request-mapper' are mutually exclusive. " +
"When providing an OutboundRequestMapper, set any corresponding property on the mapper directly.", element);
}
}

View File

@@ -69,6 +69,7 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="gatewayType">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="extract-reply-payload" type="xsd:string" default="true"/>
<xsd:attribute name="supported-methods" type="xsd:string"/>
<xsd:attribute name="view" type="xsd:string">
@@ -91,6 +92,42 @@
</xsd:attribute>
<xsd:attribute name="request-key" type="xsd:string"/>
<xsd:attribute name="reply-key" type="xsd:string"/>
<xsd:attribute name="reply-timeout" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="outbound-gateway">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an outbound HTTP-based Messaging Gateway.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="gatewayType">
<xsd:attribute name="default-url" type="xsd:string"/>
<xsd:attribute name="extract-request-payload" type="xsd:string"/>
<xsd:attribute name="charset" type="xsd:string"/>
<xsd:attribute name="request-mapper" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.http.OutboundRequestMapper"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="request-executor" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.http.HttpRequestExecutor"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -103,7 +140,6 @@
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="request-channel" type="xsd:string" use="required">
<xsd:annotation>
<xsd:appinfo>
@@ -123,7 +159,6 @@
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="request-timeout" type="xsd:string"/>
<xsd:attribute name="reply-timeout" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd">
<si:channel id="requests"/>
<outbound-gateway id="minimalConfig" request-channel="requests"/>
<si:channel id="replies">
<si:queue/>
</si:channel>
<outbound-gateway id="fullConfigWithMapper"
request-channel="requests"
request-mapper="mapper"
request-executor="executor"
request-timeout="1234"
reply-channel="replies"/>
<outbound-gateway id="fullConfigWithoutMapper"
request-channel="requests"
default-url="http://localhost/test"
extract-request-payload="false"
charset="UTF-8"
request-executor="executor"
request-timeout="1234"
reply-channel="replies"/>
<beans:bean id="mapper" class="org.springframework.integration.http.DefaultOutboundRequestMapper">
<beans:property name="defaultUrl" value="http://localhost/test"/>
<beans:property name="charset" value="UTF-8"/>
<beans:property name="extractPayload" value="false"/>
</beans:bean>
<beans:bean id="executor" class="org.springframework.integration.http.SimpleHttpRequestExecutor"/>
</beans:beans>

View File

@@ -0,0 +1,142 @@
/*
* Copyright 2002-2009 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.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.net.URL;
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.integration.core.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.http.DefaultOutboundRequestMapper;
import org.springframework.integration.http.HttpOutboundEndpoint;
import org.springframework.integration.http.HttpRequestExecutor;
import org.springframework.integration.http.OutboundRequestMapper;
import org.springframework.integration.http.SimpleHttpRequestExecutor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HttpOutboundGatewayParserTests {
@Autowired @Qualifier("minimalConfig")
private AbstractEndpoint minimalConfigEndpoint;
@Autowired @Qualifier("fullConfigWithMapper")
private AbstractEndpoint fullConfigWithMapperEndpoint;
@Autowired @Qualifier("fullConfigWithoutMapper")
private AbstractEndpoint fullConfigWithoutMapperEndpoint;
@Autowired
private ApplicationContext applicationContext;
@Test
public void minimalConfig() {
HttpOutboundEndpoint gateway = (HttpOutboundEndpoint) new DirectFieldAccessor(
this.minimalConfigEndpoint).getPropertyValue("handler");
MessageChannel requestChannel = (MessageChannel) new DirectFieldAccessor(
this.minimalConfigEndpoint).getPropertyValue("inputChannel");
assertEquals(this.applicationContext.getBean("requests"), requestChannel);
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
Object replyChannel = accessor.getPropertyValue("outputChannel");
assertNull(replyChannel);
OutboundRequestMapper mapper = (OutboundRequestMapper) accessor.getPropertyValue("requestMapper");
HttpRequestExecutor executor = (HttpRequestExecutor) accessor.getPropertyValue("requestExecutor");
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
assertTrue(executor instanceof SimpleHttpRequestExecutor);
Object mapperBean = this.applicationContext.getBean("mapper");
assertNotSame(mapperBean, mapper);
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
assertNull(mapperAccessor.getPropertyValue("defaultUrl"));
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
assertEquals(true, mapperAccessor.getPropertyValue("extractPayload"));
}
@Test
public void fullConfigWithMapper() throws Exception {
HttpOutboundEndpoint gateway = (HttpOutboundEndpoint) new DirectFieldAccessor(
this.fullConfigWithMapperEndpoint).getPropertyValue("handler");
MessageChannel requestChannel = (MessageChannel) new DirectFieldAccessor(
this.fullConfigWithMapperEndpoint).getPropertyValue("inputChannel");
assertEquals(this.applicationContext.getBean("requests"), requestChannel);
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
Object replyChannel = accessor.getPropertyValue("outputChannel");
assertNotNull(replyChannel);
assertEquals(this.applicationContext.getBean("replies"), replyChannel);
OutboundRequestMapper mapper = (OutboundRequestMapper) accessor.getPropertyValue("requestMapper");
HttpRequestExecutor executor = (HttpRequestExecutor) accessor.getPropertyValue("requestExecutor");
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
assertTrue(executor instanceof SimpleHttpRequestExecutor);
Object mapperBean = this.applicationContext.getBean("mapper");
assertEquals(mapperBean, mapper);
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
assertEquals(new URL("http://localhost/test"), mapperAccessor.getPropertyValue("defaultUrl"));
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
Object executorBean = this.applicationContext.getBean("executor");
assertEquals(executorBean, executor);
Object sendTimeout = new DirectFieldAccessor(
accessor.getPropertyValue("channelTemplate")).getPropertyValue("sendTimeout");
assertEquals(new Long("1234"), sendTimeout);
}
@Test
public void fullConfigWithoutMapper() throws Exception {
HttpOutboundEndpoint gateway = (HttpOutboundEndpoint) new DirectFieldAccessor(
this.fullConfigWithoutMapperEndpoint).getPropertyValue("handler");
MessageChannel requestChannel = (MessageChannel) new DirectFieldAccessor(
this.fullConfigWithoutMapperEndpoint).getPropertyValue("inputChannel");
assertEquals(this.applicationContext.getBean("requests"), requestChannel);
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
Object replyChannel = accessor.getPropertyValue("outputChannel");
assertNotNull(replyChannel);
assertEquals(this.applicationContext.getBean("replies"), replyChannel);
OutboundRequestMapper mapper = (OutboundRequestMapper) accessor.getPropertyValue("requestMapper");
HttpRequestExecutor executor = (HttpRequestExecutor) accessor.getPropertyValue("requestExecutor");
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
assertTrue(executor instanceof SimpleHttpRequestExecutor);
Object mapperBean = this.applicationContext.getBean("mapper");
assertNotSame(mapperBean, mapper);
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
assertEquals(new URL("http://localhost/test"), mapperAccessor.getPropertyValue("defaultUrl"));
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
Object executorBean = this.applicationContext.getBean("executor");
assertEquals(executorBean, executor);
Object sendTimeout = new DirectFieldAccessor(
accessor.getPropertyValue("channelTemplate")).getPropertyValue("sendTimeout");
assertEquals(new Long("1234"), sendTimeout);
}
}