Skip to content

Commit

Permalink
Reinstate removal of semicolon content
Browse files Browse the repository at this point in the history
Commit 5b1165 was an attempt to leave semicolon content in the URL path
while ignoring it for request mapping purposes. However, it becomes
quite difficult to manage and semicolon content should not always be
ignored (sometimes a semicolon is used as a separator of multiple items
in a path segment, rather than for matrix variables).

This change effectively reverts back to the original approach in 3.2
where a flag on AbstractHandlerMapping can be used to have semicolon
content removed or kept. If kept, path segments with matrix variables
must be represented with a path segment.

The main difference is that by default it is removed everywhere
including the MVC namespace and Java config.

Issue: SPR-10427, SPR-10234
  • Loading branch information
rstoyanchev committed May 15, 2013
1 parent db596d2 commit d23de28
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
handlerMappingDef.getPropertyValues().add("order", 0);
handlerMappingDef.getPropertyValues().add("contentNegotiationManager", contentNegotiationManager);
String methodMappingName = parserContext.getReaderContext().registerWithGeneratedName(handlerMappingDef);
if (element.hasAttribute("enableMatrixVariables")) {
Boolean enableMatrixVariables = Boolean.valueOf(element.getAttribute("enableMatrixVariables"));
handlerMappingDef.getPropertyValues().add("removeSemicolonContent", !enableMatrixVariables);
}

RuntimeBeanReference conversionService = getConversionService(element, source, parserContext);
RuntimeBeanReference validator = getValidator(element, source, parserContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void setUrlDecode(boolean urlDecode) {

/**
* Set if ";" (semicolon) content should be stripped from the request URI.
* <p>The default value is {@code false}.
* <p>The default value is {@code true}.
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
*/
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

Expand All @@ -37,7 +38,6 @@
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.HandlerMethodSelector;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper;

/**
* Abstract base class for {@link HandlerMapping} implementations that define a
Expand All @@ -62,12 +62,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
private final MultiValueMap<String, T> urlMap = new LinkedMultiValueMap<String, T>();


public AbstractHandlerMethodMapping() {
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(false);
setUrlPathHelper(pathHelper);
}

/**
* Whether to detect handler methods in beans in ancestor ApplicationContexts.
* <p>Default is "false": Only beans in the current ApplicationContext are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
*/
public final class PatternsRequestCondition extends AbstractRequestCondition<PatternsRequestCondition> {

private static UrlPathHelper pathHelperNoSemicolonContent;

static {
pathHelperNoSemicolonContent = new UrlPathHelper();
pathHelperNoSemicolonContent.setRemoveSemicolonContent(true);
}

private final Set<String> patterns;

private final UrlPathHelper pathHelper;
Expand Down Expand Up @@ -217,15 +210,10 @@ public PatternsRequestCondition getMatchingCondition(HttpServletRequest request)
}

String lookupPath = this.pathHelper.getLookupPathForRequest(request);
String lookupPathNoSemicolonContent = (lookupPath.indexOf(';') != -1) ?
pathHelperNoSemicolonContent.getLookupPathForRequest(request) : null;

List<String> matches = new ArrayList<String>();
for (String pattern : patterns) {
String match = getMatchingPattern(pattern, lookupPath);
if (match == null && lookupPathNoSemicolonContent != null) {
match = getMatchingPattern(pattern, lookupPathNoSemicolonContent);
}
if (match != null) {
matches.add(match);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="enableMatrixVariables" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[
Matrix variables can appear in any path segment, each matrix variable separated with a ";" (semicolon).
For example "/cars;color=red;year=2012". By default they're removed from the URL. If this property
is set to true, matrix variables are not removed from the URL, and the request mapping pattern
must use URI variable in path segments where matrix variables are expected. For example "/{cars}".
Matrix variables can then be injected into a controller method with @MatrixVariable.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="enableMatrixVariables" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[
Matrix variables can appear in any path segment, each matrix variable separated with a ";" (semicolon).
For example "/cars;color=red;year=2012". By default they're removed from the URL. If this property
is set to true, matrix variables are not removed from the URL, and the request mapping pattern
must use URI variable in path segments where matrix variables are expected. For example "/{cars}".
Matrix variables can then be injected into a controller method with @MatrixVariable.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@

package org.springframework.web.servlet.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.validation.constraints.NotNull;

Expand Down Expand Up @@ -86,6 +80,8 @@
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;

import static org.junit.Assert.*;

/**
* @author Keith Donald
* @author Arjen Poutsma
Expand Down Expand Up @@ -117,6 +113,7 @@ public void testDefaultConfig() throws Exception {
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertNotNull(mapping);
assertEquals(0, mapping.getOrder());
assertTrue(mapping.getUrlPathHelper().shouldRemoveSemicolonContent());
mapping.setDefaultHandler(handlerMethod);

MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
Expand Down Expand Up @@ -182,6 +179,10 @@ public void testCustomConversionService() throws Exception {
public void testCustomValidator() throws Exception {
loadBeanDefinitions("mvc-config-custom-validator.xml", 12);

RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertNotNull(mapping);
assertFalse(mapping.getUrlPathHelper().shouldRemoveSemicolonContent());

RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
assertNotNull(adapter);
assertEquals(true, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@

package org.springframework.web.servlet.mvc.condition;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.web.util.UrlPathHelper;

import static org.junit.Assert.*;

/**
* @author Rossen Stoyanchev
Expand Down Expand Up @@ -186,16 +183,6 @@ public void matchPatternContainsExtension() {
assertNull(match);
}

@Test
public void matchIgnorePathParams() {
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(false);
PatternsRequestCondition condition = new PatternsRequestCondition(new String[] {"/foo/bar"}, pathHelper, null, true, true);
PatternsRequestCondition match = condition.getMatchingCondition(new MockHttpServletRequest("GET", "/foo;q=1/bar;s=1"));

assertNotNull(match);
}

@Test
public void compareEqualPatterns() {
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

package org.springframework.web.servlet.mvc.method;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -61,6 +54,8 @@
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.util.UrlPathHelper;

import static org.junit.Assert.*;

/**
* Test fixture with {@link RequestMappingInfoHandlerMapping}.
*
Expand Down Expand Up @@ -90,6 +85,7 @@ public void setUp() throws Exception {

this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
this.handlerMapping.registerHandler(testController);
this.handlerMapping.setRemoveSemicolonContent(false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<mvc:annotation-driven validator="validator" ignoreDefaultModelOnRedirect="true" />
<mvc:annotation-driven validator="validator"
ignoreDefaultModelOnRedirect="true" enableMatrixVariables="true" />

<bean id="validator" class="org.springframework.web.servlet.config.MvcNamespaceTests$TestValidator" />

Expand Down
4 changes: 1 addition & 3 deletions src/reference/docbook/mvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,7 @@ public void findPet(
<para>Note that to enable the use of matrix variables, you must set the
<classname>removeSemicolonContent</classname> property of
<classname>RequestMappingHandlerMapping</classname> to <code>false</code>.
By default it is set to <code>true</code> with the exception of the
MVC namespace and the MVC Java config both of which automatically enable
the use of matrix variables.</para>
By default it is set to <code>false</code>.</para>

</section>

Expand Down

0 comments on commit d23de28

Please sign in to comment.