INT-1677 finished the namespace support for payload-expression attribute and header sub-element, added parser test

This commit is contained in:
Oleg Zhurakousky
2011-09-02 08:26:13 -04:00
committed by Mark Fisher
parent 90a5936803
commit a6c85c1dd9
4 changed files with 81 additions and 0 deletions

View File

@@ -16,13 +16,22 @@
package org.springframework.integration.http.config;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.ExpressionFactoryBean;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
@@ -76,6 +85,32 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
}
builder.addPropertyReference("requestChannel", inputChannelRef);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "path");
String payloadExpression = element.getAttribute("payload-expression");
if (StringUtils.hasText(payloadExpression)){
RootBeanDefinition expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(payloadExpression);
builder.addPropertyValue("payloadExpression", expressionDef);
}
List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "header");
if (!CollectionUtils.isEmpty(headerElements)) {
ManagedMap<String, Object> headerElementsMap = new ManagedMap<String, Object>();
for (Element headerElement : headerElements) {
String name = headerElement.getAttribute("name");
String expression = headerElement.getAttribute("expression");
if (StringUtils.hasText(expression)){
RootBeanDefinition expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(expression);
headerElementsMap.put(name, expressionDef);
}
}
builder.addPropertyValue("headerExpressions", headerElementsMap);
}
if (this.expectReply) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "request-timeout");

View File

@@ -67,6 +67,13 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Allows you to specify URI path (e.g., /orderId/{order})
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="error-code" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
@@ -155,6 +162,13 @@ The String "HTTP_REQUEST_HEADERS" will match against any of the standard HTTP Re
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Allows you to specify URI path (e.g., /orderId/{order})
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="error-code" type="xsd:string">
<xsd:annotation>
<xsd:documentation>

View File

@@ -26,5 +26,13 @@
<inbound-channel-adapter id="withMappedHeaders" channel="requests"
mapped-request-headers="foo,bar"/>
<inbound-channel-adapter id="inboundAdapterWithExpressions"
path="/fname/{f}/lname/{l}"
channel="requests"
mapped-request-headers="foo,bar"
payload-expression="#f">
<header name="lname" expression="#l"/>
</inbound-channel-adapter>
</beans:beans>

View File

@@ -72,6 +72,9 @@ public class HttpInboundChannelAdapterParserTests {
@Autowired
private HttpRequestHandlingMessagingGateway withMappedHeaders;
@Autowired
private HttpRequestHandlingMessagingGateway inboundAdapterWithExpressions;
@Autowired
private HttpRequestHandlingController inboundController;
@@ -113,6 +116,27 @@ public class HttpInboundChannelAdapterParserTests {
assertEquals("foo", map.get("foo"));
assertEquals("bar", map.get("bar"));
}
@Test
@SuppressWarnings("unchecked")// INT-1677
public void withExpressions() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("text/plain");
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
request.setRequestURI("/fname/bill/lname/clinton");
MockHttpServletResponse response = new MockHttpServletResponse();
inboundAdapterWithExpressions.handleRequest(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Message<?> message = requests.receive(0);
assertNotNull(message);
Object payload = message.getPayload();
assertTrue(payload instanceof String);
assertEquals("bill", payload);
assertEquals("clinton", message.getHeaders().get("lname"));
}
@Test
public void getRequestNotAllowed() throws Exception {