Skip to content

Commit

Permalink
Issue #7891 - Improved PathSpec usage
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed May 2, 2022
1 parent 23c7b18 commit c664d49
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 232 deletions.
Expand Up @@ -20,10 +20,20 @@

public interface MatchedPath
{
/**
* @return the raw input path as provided.
*/
String getPath();
MatchedPath EMPTY = new MatchedPath()
{
@Override
public String getPathMatch()
{
return null;
}

@Override
public String getPathInfo()
{
return null;
}
};

/**
* Return the portion of the path that matches a path spec.
Expand Down
@@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.http.pathmap;

public class MatchedResource<E>
{
private final MappedResource<E> mappedResource;
private final MatchedPath matchedPath;

public MatchedResource(MappedResource<E> resource, MatchedPath matchedPath)
{
this.mappedResource = resource;
this.matchedPath = matchedPath;
}

public MappedResource<E> getMappedResource()
{
return this.mappedResource;
}

public PathSpec getPathSpec()
{
return mappedResource.getPathSpec();
}

public E getResource()
{
return mappedResource.getResource();
}

public MatchedPath getMatchedPath()
{
return matchedPath;
}
}
Expand Up @@ -120,8 +120,9 @@ public List<MappedResource<E>> getMatches(String path)
return ret;
}

public MappedResource<E> getMatch(String path)
public MatchedResource<E> getMatched(String path)
{
MatchedPath matchedPath = null;
PathSpecGroup lastGroup = null;

// Search all the mappings
Expand All @@ -142,8 +143,10 @@ public MappedResource<E> getMatch(String path)
MappedResource<E> candidate = exact_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
Expand All @@ -158,8 +161,10 @@ public MappedResource<E> getMatch(String path)
MappedResource<E> candidate = prefix_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
Expand All @@ -172,8 +177,12 @@ public MappedResource<E> getMatch(String path)
while ((i = path.indexOf('.', i + 1)) > 0)
{
MappedResource<E> candidate = suffix_map.get(path, i + 1, path.length() - i - 1);
if (candidate != null && candidate.getPathSpec().matches(path))
return candidate;
if (candidate == null)
break;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate, matchedPath);
}
break;
}
Expand All @@ -182,15 +191,25 @@ public MappedResource<E> getMatch(String path)
}
}

if (mr.getPathSpec().matches(path))
return mr;
matchedPath = mr.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(mr, matchedPath);

lastGroup = group;
}

return null;
}

/**
* @deprecated use {@link #getMatched(String)} instead
*/
@Deprecated
public MappedResource<E> getMatch(String path)
{
return getMatched(path).getMappedResource();
}

@Override
public Iterator<MappedResource<E>> iterator()
{
Expand Down
Expand Up @@ -20,6 +20,7 @@

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;

/**
Expand All @@ -34,7 +35,7 @@ public class PathSpecSet extends AbstractSet<String> implements Predicate<String
@Override
public boolean test(String s)
{
return specs.getMatch(s) != null;
return specs.getMatches(s) != null;
}

@Override
Expand All @@ -53,11 +54,8 @@ private PathSpec asPathSpec(Object o)
{
return (PathSpec)o;
}
if (o instanceof String)
{
return PathMappings.asPathSpec((String)o);
}
return PathMappings.asPathSpec(o.toString());

return PathMappings.asPathSpec(Objects.toString(o));
}

@Override
Expand Down
Expand Up @@ -229,12 +229,6 @@ public RegexMatchedPath(RegexPathSpec regexPathSpec, String path, Matcher matche
this.matcher = matcher;
}

@Override
public String getPath()
{
return this.path;
}

@Override
public String getPathMatch()
{
Expand Down
Expand Up @@ -291,7 +291,7 @@ public MatchedPath matched(String path)
{
case EXACT:
if (_declaration.equals(path))
return new ServletMatchedPath(path, path, null);
return new ServletMatchedPath(path, null); // TODO: return final matchedpath
break;
case PREFIX_GLOB:
if (isWildcardMatch(path))
Expand All @@ -303,21 +303,21 @@ public MatchedPath matched(String path)
pathMatch = path.substring(0, _specLength - 2);
pathInfo = path.substring(_specLength - 2);
}
return new ServletMatchedPath(path, pathMatch, pathInfo);
return new ServletMatchedPath(pathMatch, pathInfo);
}
break;
case SUFFIX_GLOB:
if (path.regionMatches((path.length() - _specLength) + 1, _declaration, 1, _specLength - 1))
return new ServletMatchedPath(path, path, null);
return new ServletMatchedPath(path, null);
break;
case ROOT:
// Only "/" matches
if ("/".equals(path))
return new ServletMatchedPath(path, "", path);
return new ServletMatchedPath("", path); // TODO: review this
break;
case DEFAULT:
// If we reached this point, then everything matches
return new ServletMatchedPath(path, path, null);
return new ServletMatchedPath(path, null);
}
return null;
}
Expand Down Expand Up @@ -346,23 +346,15 @@ public boolean matches(String path)

public static class ServletMatchedPath implements MatchedPath
{
private final String path;
private final String servletName;
private final String pathInfo;

public ServletMatchedPath(String inputPath, String servletName, String pathInfo)
public ServletMatchedPath(String servletName, String pathInfo)
{
this.path = inputPath;
this.servletName = servletName;
this.pathInfo = pathInfo;
}

@Override
public String getPath()
{
return this.path;
}

@Override
public String getPathMatch()
{
Expand Down
Expand Up @@ -454,12 +454,6 @@ public UriTemplateMatchedPath(UriTemplatePathSpec uriTemplatePathSpec, String pa
this.matcher = matcher;
}

@Override
public String getPath()
{
return this.path;
}

@Override
public String getPathMatch()
{
Expand Down

0 comments on commit c664d49

Please sign in to comment.