Skip to content

Commit

Permalink
Fixes to backport of #7748 (#7834)
Browse files Browse the repository at this point in the history
+ Backport of #7748
+ Fix RegexPathSpec pathInfo
+ Fix UriTemplatePathSpec pathInfo
+ Test regression option to 93 behaviour

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Apr 6, 2022
1 parent 201f9a6 commit 41ba531
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Expand Up @@ -140,7 +140,7 @@ public String getPathMatch(String path)
Matcher matcher = getMatcher(path);
if (matcher.matches())
{
if (matcher.groupCount() >= 1)
if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
{
int idx = matcher.start(1);
if (idx > 0)
Expand Down
Expand Up @@ -358,7 +358,7 @@ public String getPathMatch(String path)
Matcher matcher = getMatcher(path);
if (matcher.matches())
{
if (matcher.groupCount() >= 1)
if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1)
{
int idx = matcher.start(1);
if (idx > 0)
Expand Down
Expand Up @@ -24,7 +24,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RegexPathSpecTest
{
Expand Down Expand Up @@ -76,6 +78,20 @@ public void testMiddleSpec()
assertNotMatches(spec, "/rest/list");
}

@Test
public void testPathInfo()
{
RegexPathSpec spec = new RegexPathSpec("^/test(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("/info"));

spec = new RegexPathSpec("^/[Tt]est(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test/info"));
assertThat(spec.getPathInfo("/test/info"), nullValue());
}

@Test
public void testMiddleSpecNoGrouping()
{
Expand Down
Expand Up @@ -27,7 +27,9 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for URI Template Path Specs
Expand Down Expand Up @@ -147,6 +149,20 @@ public void testMiddleVarPathSpec()
assertEquals("b", mapped.get("var"), "Spec.pathParams[var]");
}

@Test
public void testPathInfo()
{
UriTemplatePathSpec spec = new UriTemplatePathSpec("/test/{var}");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("info"));

spec = new UriTemplatePathSpec("/{x}/test/{y}");
assertTrue(spec.matches("/try/test/info"));
assertThat(spec.getPathMatch("/try/test/info"), equalTo("/try/test/info"));
assertThat(spec.getPathInfo("/try/test/info"), nullValue());
}

@Test
public void testOneVarPathSpec()
{
Expand Down
Expand Up @@ -303,7 +303,7 @@ protected synchronized void doStop()
}
}
else
servletHolders.add(_servlets[i]); //only retain embedded
servletHolders.add(_servlets[i]); //only retain embedded
}
}

Expand All @@ -314,7 +314,7 @@ protected synchronized void doStop()
ServletMapping[] sms = servletMappings.toArray(new ServletMapping[0]);
updateBeans(_servletMappings, sms);
_servletMappings = sms;

if (_contextHandler != null)
_contextHandler.contextDestroyed();

Expand Down Expand Up @@ -1151,7 +1151,7 @@ public void addFilterMapping(FilterMapping mapping)
else
{
//there are existing entries. If this is a programmatic filtermapping, it is added at the end of the list.
//If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals),
//If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals),
//but before the first matchAfter filtermapping.
if (source == Source.JAVAX_API)
{
Expand Down Expand Up @@ -1299,7 +1299,13 @@ protected synchronized void updateNameMappings()
}
}

protected synchronized void updateMappings()
protected PathSpec asPathSpec(String pathSpec)
{
// By default only allow servlet path specs
return new ServletPathSpec(pathSpec);
}

protected void updateMappings()
{
// update filter mappings
if (_filterMappings == null)
Expand Down Expand Up @@ -1388,7 +1394,7 @@ protected synchronized void updateMappings()
finalMapping = mapping;
else
{
//already have a candidate - only accept another one
//already have a candidate - only accept another one
//if the candidate is a default, or we're allowing duplicate mappings
if (finalMapping.isDefault())
finalMapping = mapping;
Expand Down Expand Up @@ -1421,7 +1427,7 @@ else if (isAllowDuplicateMappings())
finalMapping.getServletName(),
_servletNameMap.get(finalMapping.getServletName()).getSource());

pm.put(new ServletPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName()));
pm.put(asPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName()));
}

_servletPathMap = pm;
Expand Down

0 comments on commit 41ba531

Please sign in to comment.