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>