INT-1677 removed requirement for anID, polished schema, added more tests, tested with server

This commit is contained in:
Oleg Zhurakousky
2011-09-02 09:56:44 -04:00
committed by Mark Fisher
parent a6c85c1dd9
commit c07d62dadb
5 changed files with 84 additions and 12 deletions

View File

@@ -16,13 +16,12 @@
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.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
@@ -32,7 +31,6 @@ 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;
/**
@@ -69,8 +67,9 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
id = element.getAttribute("name");
}
if (!StringUtils.hasText(id)) {
parserContext.getReaderContext().error("The 'id' or 'name' is required.", element);
id = BeanDefinitionReaderUtils.generateBeanName(definition, parserContext.getRegistry());
}
return id;
}
@@ -96,8 +95,8 @@ public class HttpInboundEndpointParser extends AbstractSingleBeanDefinitionParse
}
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");

View File

@@ -285,7 +285,7 @@ abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewaySuppor
}
Map uriVariableMappings = null;
//
StandardEvaluationContext evaluationContext = this.getEvaluationContext();
StandardEvaluationContext evaluationContext = this.prepareAndGetEvaluationContext();
if (StringUtils.hasText(this.path)){
UriTemplate template = new UriTemplate(this.path);
@@ -295,8 +295,7 @@ abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewaySuppor
logger.debug("Mapped URI variables: " + uriVariableMappings);
}
// set the whole map
// set the whole map
evaluationContext.setVariable("uriVariables", uriVariableMappings);
for (Object key : uriVariableMappings.keySet()) {
// add individual elements
@@ -454,7 +453,7 @@ abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewaySuppor
return httpStatus;
}
private StandardEvaluationContext getEvaluationContext(){
private StandardEvaluationContext prepareAndGetEvaluationContext(){
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
evaluationContext.addPropertyAccessor(new MapAccessor());
BeanFactory beanFactory = this.getBeanFactory();

View File

@@ -24,7 +24,14 @@
<xsd:element name="header" type="headerType" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
[DEPRECATED since v2.1] Use 'path' attribute if you want to specify the path or
'id' attribute if you simply want to identify this component
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="channel" type="xsd:string" use="required">
<xsd:annotation>
<xsd:appinfo>

View File

@@ -34,5 +34,20 @@
payload-expression="#f">
<header name="lname" expression="#l"/>
</inbound-channel-adapter>
<inbound-channel-adapter name="/fname/{blah}/lname/{boo}"
path="/fname/{f}/lname/{l}"
channel="requests"
mapped-request-headers="foo,bar"
payload-expression="#f">
<header name="lname" expression="#l"/>
</inbound-channel-adapter>
<inbound-channel-adapter name="/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

@@ -35,6 +35,7 @@ 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.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.integration.Message;
@@ -75,7 +76,15 @@ public class HttpInboundChannelAdapterParserTests {
@Autowired
private HttpRequestHandlingMessagingGateway inboundAdapterWithExpressions;
@Autowired
@Qualifier("/fname/{blah}/lname/{boo}")
private HttpRequestHandlingMessagingGateway inboundAdapterWithNameAndExpressions;
@Autowired
@Qualifier("/fname/{f}/lname/{l}")
private HttpRequestHandlingMessagingGateway inboundAdapterWithNameNoPath;
@Autowired
private HttpRequestHandlingController inboundController;
@@ -118,7 +127,7 @@ public class HttpInboundChannelAdapterParserTests {
}
@Test
@SuppressWarnings("unchecked")// INT-1677
// INT-1677
public void withExpressions() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
@@ -137,6 +146,49 @@ public class HttpInboundChannelAdapterParserTests {
assertEquals("bill", payload);
assertEquals("clinton", message.getHeaders().get("lname"));
}
@Test // ensure that 'path' takes priority over name
// INT-1677
public void withNameAndExpressionsAndPath() 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();
inboundAdapterWithNameAndExpressions.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
// INT-1677
public void withNameAndExpressionsNoPath() 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();
inboundAdapterWithNameNoPath.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("hello", payload); // default payload
assertNull(message.getHeaders().get("lname"));
}
@Test
public void getRequestNotAllowed() throws Exception {