INT-3184: Test HTTP Matrix Variables

JIRA: https://jira.springsource.org/browse/INT-3184

* Change `IntegrationRequestMappingHandlerMapping` to postpone
Handlers detection as late as possible using `ApplicationListener<ContextRefreshedEvent>`
This commit is contained in:
Artem Bilan
2013-12-17 13:58:44 +02:00
committed by Gary Russell
parent 3d5a53d5d3
commit b607b6d3c9
4 changed files with 42 additions and 9 deletions

View File

@@ -422,9 +422,8 @@ public abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewa
evaluationContext.setVariable("pathVariables", pathVariables);
}
//TODO change it to HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE after upgrade to Spring 4.0
Map<String, MultiValueMap<String, String>> matrixVariables =
(Map<String, MultiValueMap<String, String>>) servletRequest.getAttribute(HandlerMapping.class.getName() + ".matrixVariables");
(Map<String, MultiValueMap<String, String>>) servletRequest.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
if (!CollectionUtils.isEmpty(matrixVariables)) {
if (logger.isDebugEnabled()) {

View File

@@ -18,10 +18,13 @@ package org.springframework.integration.http.inbound;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.HttpRequestHandler;
@@ -59,11 +62,14 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
*
* @since 3.0
*/
public final class IntegrationRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
public final class IntegrationRequestMappingHandlerMapping extends RequestMappingHandlerMapping
implements ApplicationListener<ContextRefreshedEvent> {
private static final Method HANDLE_REQUEST_METHOD = ReflectionUtils.findMethod(HttpRequestHandler.class,
"handleRequest", HttpServletRequest.class, HttpServletResponse.class);
private final AtomicBoolean initialized = new AtomicBoolean();
@Override
protected final boolean isHandler(Class<?> beanType) {
return HttpRequestHandlingEndpointSupport.class.isAssignableFrom(beanType);
@@ -142,4 +148,22 @@ public final class IntegrationRequestMappingHandlerMapping extends RequestMappin
return this.createRequestMappingInfo(requestMappingAnnotation, this.getCustomTypeCondition(endpoint.getClass()));
}
@Override
public void afterPropertiesSet() {
// No-op in favor of onApplicationEvent
}
/**
* {@link HttpRequestHandlingEndpointSupport}s may depend on auto-created
* {@code requestChannel}s, so MVC Handlers detection should be postponed
* as late as possible.
*
* @see RequestMappingHandlerMapping#afterPropertiesSet()
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (!this.initialized.getAndSet(true)) {
super.afterPropertiesSet();
}
}
}

View File

@@ -13,6 +13,12 @@
http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<!-- Allow Matrix Variables -->
<beans:bean id="integrationRequestMappingHandlerMapping"
class="org.springframework.integration.http.inbound.IntegrationRequestMappingHandlerMapping">
<beans:property name="removeSemicolonContent" value="false"/>
</beans:bean>
<int-http:inbound-gateway path="/path1,/path2"
request-channel="multiplePathsChannel"/>

View File

@@ -19,6 +19,7 @@ package org.springframework.integration.http.inbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
@@ -27,6 +28,7 @@ import java.util.Map;
import javax.servlet.http.Cookie;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -100,9 +102,7 @@ public class Int2312RequestMappingIntegrationTests extends AbstractHttpInboundTe
public void testURIVariablesAndHeaders() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
String testRequest = "aBc";
// TODO test it after upgrade to Spring 4.0
// String testRequest = "aBc;q1=1;q2=2";
String testRequest = "aBc;q1=1;q2=2";
String requestURI = "/test/" + testRequest;
request.setRequestURI(requestURI);
request.setContentType("text/plain");
@@ -129,8 +129,12 @@ public class Int2312RequestMappingIntegrationTests extends AbstractHttpInboundTe
assertNotNull(requestParams);
assertEquals(params, ((MultiValueMap<String, String>) requestParams).toSingleValueMap());
// TODO test it after upgrade to Spring 4.0
// assertEquals(matrixVariables, headers.get("matrixVariables"));
Object matrixVariables = headers.get("matrixVariables");
assertThat(matrixVariables, Matchers.instanceOf(Map.class));
Object value = ((Map) matrixVariables).get("value");
assertThat(value, Matchers.instanceOf(MultiValueMap.class));
assertEquals("1", ((MultiValueMap) value).getFirst("q1"));
assertEquals("2", ((MultiValueMap) value).getFirst("q2"));
Object requestHeaders = headers.get("requestHeaders");
assertNotNull(requestParams);
@@ -149,7 +153,7 @@ public class Int2312RequestMappingIntegrationTests extends AbstractHttpInboundTe
Object handler = this.handlerMapping.getHandler(request).getHandler();
this.handlerAdapter.handle(request, response, handler);
final String testResponse = response.getContentAsString();
assertEquals(testRequest.toLowerCase(), testResponse);
assertEquals(testRequest.split(";")[0].toLowerCase(), testResponse);
RequestContextHolder.resetRequestAttributes();
}