Repository: wicket Updated Branches: refs/heads/wicket-6.x a01a674b5 -> 6ce317cf9
WICKET-5673 Improve BookmarkableMapper and BasicResourceReferenceMapper to not match when the last segment is empty (cherry picked from commit bfffed1260a37cd6c338c2495a03377c76fe99d8) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6ce317cf Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6ce317cf Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6ce317cf Branch: refs/heads/wicket-6.x Commit: 6ce317cf9414c550e0e66fbae5e35936ee20763d Parents: a01a674 Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Authored: Tue Aug 12 12:10:53 2014 +0200 Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Committed: Tue Aug 12 12:12:55 2014 +0200 ---------------------------------------------------------------------- .../mapper/BasicResourceReferenceMapper.java | 7 ++- .../core/request/mapper/BookmarkableMapper.java | 49 ++++++++++++++++++-- .../BasicResourceReferenceMapperTest.java | 13 ++++++ .../request/mapper/BookmarkableMapperTest.java | 14 ++++++ .../wicket/request/mapper/AbstractMapper.java | 7 ++- 5 files changed, 82 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/6ce317cf/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java index 665dafe..e7d14b7 100755 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java @@ -278,8 +278,11 @@ public class BasicResourceReferenceMapper extends AbstractResourceReferenceMappe */ protected boolean canBeHandled(final Url url) { - return (url.getSegments().size() >= 4 && - urlStartsWith(url, getContext().getNamespace(), getContext().getResourceIdentifier())); + List<String> segments = url.getSegments(); + return (segments.size() >= 4 && + urlStartsWith(url, getContext().getNamespace(), getContext().getResourceIdentifier()) && + Strings.isEmpty(segments.get(3)) == false + ); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/6ce317cf/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BookmarkableMapper.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BookmarkableMapper.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BookmarkableMapper.java index a6823df..284f003 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BookmarkableMapper.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BookmarkableMapper.java @@ -30,6 +30,7 @@ import org.apache.wicket.request.mapper.parameter.IPageParametersEncoder; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.request.mapper.parameter.PageParametersEncoder; import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; /** * Decodes and encodes the following URLs: @@ -206,26 +207,66 @@ public class BookmarkableMapper extends AbstractBookmarkableMapper String bookmarkableIdentifier = getContext().getBookmarkableIdentifier(); String pageIdentifier = getContext().getPageIdentifier(); - if (url.getSegments().size() >= 3 && urlStartsWith(url, namespace, bookmarkableIdentifier)) + List<String> segments = url.getSegments(); + int segmentsSize = segments.size(); + + if (segmentsSize >= 3 && urlStartsWithAndHasPageClass(url, namespace, bookmarkableIdentifier)) { matches = true; } // baseUrl = 'wicket/bookmarkable/com.example.SomePage[?...]', requestUrl = 'bookmarkable/com.example.SomePage' - else if (baseUrl.getSegments().size() == 3 && urlStartsWith(baseUrl, namespace, bookmarkableIdentifier) && url.getSegments().size() >= 2 && urlStartsWith(url, bookmarkableIdentifier)) + else if (baseUrl.getSegments().size() == 3 && urlStartsWith(baseUrl, namespace, bookmarkableIdentifier) + && segmentsSize >= 2 && urlStartsWithAndHasPageClass(url, bookmarkableIdentifier)) { matches = true; } // baseUrl = 'bookmarkable/com.example.SomePage', requestUrl = 'bookmarkable/com.example.SomePage' - else if (baseUrl.getSegments().size() == 2 && urlStartsWith(baseUrl, bookmarkableIdentifier) && url.getSegments().size() == 2 && urlStartsWith(url, bookmarkableIdentifier)) + else if (baseUrl.getSegments().size() == 2 && urlStartsWith(baseUrl, bookmarkableIdentifier) + && segmentsSize == 2 && urlStartsWithAndHasPageClass(url, bookmarkableIdentifier)) { matches = true; } // baseUrl = 'wicket/page[?...]', requestUrl = 'bookmarkable/com.example.SomePage' - else if (baseUrl.getSegments().size() == 2 && urlStartsWith(baseUrl, namespace, pageIdentifier) && url.getSegments().size() >= 2 && urlStartsWith(url, bookmarkableIdentifier)) + else if (baseUrl.getSegments().size() == 2 && urlStartsWith(baseUrl, namespace, pageIdentifier) + && segmentsSize >= 2 && urlStartsWithAndHasPageClass(url, bookmarkableIdentifier)) { matches = true; } return matches; } + + /** + * Checks whether the url starts with the given segments and additionally + * checks whether the following segment is non-empty + * + * @param url + * The url to be checked + * @param segments + * The expected leading segments + * @return {@code true} if the url starts with the given segments and there is non-empty segment after them + */ + protected boolean urlStartsWithAndHasPageClass(Url url, String... segments) + { + boolean result = urlStartsWith(url, segments); + + if (result) + { + List<String> urlSegments = url.getSegments(); + if (urlSegments.size() == segments.length) + { + result = false; + } + else + { + String pageClassSegment = urlSegments.get(segments.length); + if (Strings.isEmpty(pageClassSegment)) + { + result = false; + } + } + } + + return result; + } } http://git-wip-us.apache.org/repos/asf/wicket/blob/6ce317cf/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java b/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java index 7dcf36e..ce34761 100644 --- a/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java @@ -16,6 +16,8 @@ */ package org.apache.wicket.core.request.mapper; +import static org.hamcrest.Matchers.is; + import java.io.Serializable; import java.util.Locale; @@ -320,6 +322,17 @@ public class BasicResourceReferenceMapperTest extends AbstractResourceReferenceM } /** + * https://issues.apache.org/jira/browse/WICKET-5673 + */ + @Test + public void decode11() + { + Url url = Url.parse("wicket/resource/com.example.Scope/"); + int score = encoder.getCompatibilityScore(getRequest(url)); + assertThat(score, is(-1)); + } + + /** * */ @Test http://git-wip-us.apache.org/repos/asf/wicket/blob/6ce317cf/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BookmarkableMapperTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BookmarkableMapperTest.java b/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BookmarkableMapperTest.java index 9138bfa..6606a76 100644 --- a/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BookmarkableMapperTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/core/request/mapper/BookmarkableMapperTest.java @@ -16,6 +16,8 @@ */ package org.apache.wicket.core.request.mapper; +import static org.hamcrest.Matchers.is; + import java.nio.charset.Charset; import java.util.Locale; @@ -442,6 +444,18 @@ public class BookmarkableMapperTest extends AbstractMapperTest } /** + * https://issues.apache.org/jira/browse/WICKET-5673 + */ + @Test + public void decode14() + { + Url url = Url.parse("wicket/bookmarkable/"); + int score = encoder.getCompatibilityScore(getRequest(url)); + + assertThat(score, is(0)); + } + + /** * */ @Test http://git-wip-us.apache.org/repos/asf/wicket/blob/6ce317cf/wicket-request/src/main/java/org/apache/wicket/request/mapper/AbstractMapper.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/AbstractMapper.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/AbstractMapper.java index 1857818..2810b3f 100644 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/AbstractMapper.java +++ b/wicket-request/src/main/java/org/apache/wicket/request/mapper/AbstractMapper.java @@ -16,6 +16,8 @@ */ package org.apache.wicket.request.mapper; +import java.util.List; + import org.apache.wicket.request.IRequestMapper; import org.apache.wicket.request.Request; import org.apache.wicket.request.Url; @@ -100,7 +102,8 @@ public abstract class AbstractMapper implements IRequestMapper } else { - if (url.getSegments().size() < segments.length) + List<String> urlSegments = url.getSegments(); + if (urlSegments.size() < segments.length) { return false; } @@ -108,7 +111,7 @@ public abstract class AbstractMapper implements IRequestMapper { for (int i = 0; i < segments.length; ++i) { - if ((segments[i].equals(url.getSegments().get(i)) == false) && + if ((segments[i].equals(urlSegments.get(i)) == false) && (getPlaceholder(segments[i]) == null)) { return false;