Skip to content

Commit 5b4d1dd

Browse files
joakimegregw
andauthored
Improved PathSpec handling for servletName & pathInfo (#7947)
* Introduce MatchedResource * Introduce MatchedPath * Improved group matching with optimized Tries * Deprecate old APIs * Introduced final preMatchedPath when possible Signed-off-by: Joakim Erdfelt <[email protected]> Co-authored-by: Greg Wilkins <[email protected]>
1 parent 208b150 commit 5b4d1dd

File tree

21 files changed

+1041
-355
lines changed

21 files changed

+1041
-355
lines changed

jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/AbstractPathSpec.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public int compareTo(PathSpec other)
3636
return diff;
3737

3838
// Path Spec Name (alphabetical)
39-
return getDeclaration().compareTo(other.getDeclaration());
39+
diff = getDeclaration().compareTo(other.getDeclaration());
40+
if (diff != 0)
41+
return diff;
42+
43+
// Path Implementation
44+
return getClass().getName().compareTo(other.getClass().getName());
4045
}
4146

4247
@Override
@@ -55,7 +60,7 @@ public final boolean equals(Object obj)
5560
@Override
5661
public final int hashCode()
5762
{
58-
return Objects.hash(getDeclaration());
63+
return Objects.hash(getGroup().ordinal(), getSpecLength(), getDeclaration(), getClass().getName());
5964
}
6065

6166
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4+
// ------------------------------------------------------------------------
5+
// All rights reserved. This program and the accompanying materials
6+
// are made available under the terms of the Eclipse Public License v1.0
7+
// and Apache License v2.0 which accompanies this distribution.
8+
//
9+
// The Eclipse Public License is available at
10+
// http://www.eclipse.org/legal/epl-v10.html
11+
//
12+
// The Apache License v2.0 is available at
13+
// http://www.opensource.org/licenses/apache2.0.php
14+
//
15+
// You may elect to redistribute this code under either of these licenses.
16+
// ========================================================================
17+
//
18+
19+
package org.eclipse.jetty.http.pathmap;
20+
21+
public interface MatchedPath
22+
{
23+
MatchedPath EMPTY = new MatchedPath()
24+
{
25+
@Override
26+
public String getPathMatch()
27+
{
28+
return null;
29+
}
30+
31+
@Override
32+
public String getPathInfo()
33+
{
34+
return null;
35+
}
36+
37+
@Override
38+
public String toString()
39+
{
40+
return MatchedPath.class.getSimpleName() + ".EMPTY";
41+
}
42+
};
43+
44+
static MatchedPath from(String pathMatch, String pathInfo)
45+
{
46+
return new MatchedPath()
47+
{
48+
@Override
49+
public String getPathMatch()
50+
{
51+
return pathMatch;
52+
}
53+
54+
@Override
55+
public String getPathInfo()
56+
{
57+
return pathInfo;
58+
}
59+
60+
@Override
61+
public String toString()
62+
{
63+
return MatchedPath.class.getSimpleName() + "[pathMatch=" + pathMatch + ", pathInfo=" + pathInfo + "]";
64+
}
65+
};
66+
}
67+
68+
/**
69+
* Return the portion of the path that matches a path spec.
70+
*
71+
* @return the path name portion of the match.
72+
*/
73+
String getPathMatch();
74+
75+
/**
76+
* Return the portion of the path that is after the path spec.
77+
*
78+
* @return the path info portion of the match, or null if there is no portion after the {@link #getPathMatch()}
79+
*/
80+
String getPathInfo();
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4+
// ------------------------------------------------------------------------
5+
// All rights reserved. This program and the accompanying materials
6+
// are made available under the terms of the Eclipse Public License v1.0
7+
// and Apache License v2.0 which accompanies this distribution.
8+
//
9+
// The Eclipse Public License is available at
10+
// http://www.eclipse.org/legal/epl-v10.html
11+
//
12+
// The Apache License v2.0 is available at
13+
// http://www.opensource.org/licenses/apache2.0.php
14+
//
15+
// You may elect to redistribute this code under either of these licenses.
16+
// ========================================================================
17+
//
18+
19+
package org.eclipse.jetty.http.pathmap;
20+
21+
import java.util.Map;
22+
23+
/**
24+
* The match details when using {@link PathMappings#getMatched(String)}, used to minimize return to the PathSpec or PathMappings for subsequent details
25+
* that are now provided by the {@link MatchedPath} instance.
26+
*
27+
* @param <E> the type of resource (IncludeExclude uses boolean, WebSocket uses endpoint/endpoint config, servlet uses ServletHolder, etc)
28+
*/
29+
public class MatchedResource<E>
30+
{
31+
private final E resource;
32+
private final PathSpec pathSpec;
33+
private final MatchedPath matchedPath;
34+
35+
public MatchedResource(E resource, PathSpec pathSpec, MatchedPath matchedPath)
36+
{
37+
this.resource = resource;
38+
this.pathSpec = pathSpec;
39+
this.matchedPath = matchedPath;
40+
}
41+
42+
public static <E> MatchedResource<E> of(Map.Entry<PathSpec, E> mapping, MatchedPath matchedPath)
43+
{
44+
return new MatchedResource<>(mapping.getValue(), mapping.getKey(), matchedPath);
45+
}
46+
47+
public PathSpec getPathSpec()
48+
{
49+
return this.pathSpec;
50+
}
51+
52+
public E getResource()
53+
{
54+
return this.resource;
55+
}
56+
57+
/**
58+
* Return the portion of the path that matches a path spec.
59+
*
60+
* @return the path name portion of the match.
61+
*/
62+
public String getPathMatch()
63+
{
64+
return matchedPath.getPathMatch();
65+
}
66+
67+
/**
68+
* Return the portion of the path that is after the path spec.
69+
*
70+
* @return the path info portion of the match, or null if there is no portion after the {@link #getPathMatch()}
71+
*/
72+
public String getPathInfo()
73+
{
74+
return matchedPath.getPathInfo();
75+
}
76+
}

0 commit comments

Comments
 (0)