Skip to content

Commit

Permalink
Fix routing for duplicated path
Browse files Browse the repository at this point in the history
In case where the same combination of "method:path" match the routing
it was selected always the first.

E.g.: POST:{} and POST:retrieve from KRA `KeyServlet` was not working
because always the first was used.

Additionally, the routing of servlets and filters are the same and it is
not supported anymore filter routing to multiple method using "*" which
has never been used.
  • Loading branch information
fmarco76 committed Aug 27, 2024
1 parent c4091ff commit 2deebb1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.security.Principal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -163,6 +164,7 @@ public Method getActionMethod(HttpMethod met, String path) {
String keyRegex = key.replace("{}", "([^/]+)");
return reqMethod.matches(keyRegex);
} ).
sorted(Comparator.naturalOrder()).
findFirst().
orElse(null);
return keyPath == null ? null : webActions.get(keyPath);
Expand All @@ -187,7 +189,9 @@ public String getAllowedMethods(String path) {
}
StringBuilder methods = new StringBuilder();
for (String k: keyPaths) {
methods.append(k.substring(0, k.indexOf(":"))).append(", ");
if (methods.indexOf(k) == -1) {
methods.append(k.substring(0, k.indexOf(":"))).append(", ");
}
}
return methods.substring(0, methods.lastIndexOf(","));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.security.Principal;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import javax.servlet.FilterChain;
Expand Down Expand Up @@ -55,8 +54,7 @@
*
* key= <method>:<path>
*
* The method is one of the HTTP method as defined in Java servlet request (e.g. GET, POST, etc.). If the ACL has to be applied for all
* the methods then it can be replaced with the symbol '*'.
* The method is one of the HTTP method as defined in Java servlet request (e.g. GET, POST, etc.).
* The path is the endpoint in the associated servlet where the ACL has to be applied. If there is a REST path param this can be indicated
* with the sequence "{}".
*
Expand Down Expand Up @@ -97,15 +95,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
path = req.getPathInfo() != null ? req.getPathInfo().substring(1) : "";
final String aclSearch = method + ":" + path;
if (aclMap!=null) {
Optional<String> aclKey = aclMap.keySet().stream().
String aclKey = aclMap.keySet().stream().
filter( key -> {
String keyRegex = key.replaceFirst("\\*", ".*").replace("{}", "([^/]+)");
String keyRegex = key.replace("{}", "([^/]+)");
return aclSearch.matches(keyRegex);
} ).
sorted(Comparator.reverseOrder()).
findFirst();
if (aclKey.isPresent()) {
acl = aclMap.get(aclKey.get());
findFirst().
orElse(null);
if (aclKey != null) {
acl = aclMap.get(aclKey);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Comparator;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import javax.servlet.FilterChain;
Expand Down Expand Up @@ -47,8 +46,7 @@
*
* key= <method>:<path>
*
* The method is one of the HTTP method as defined in Java servlet request (e.g. GET, POST, etc.). If the ACL has to be applied for all
* the methods then it can be replaced with the symbol '*'.
* The method is one of the HTTP method as defined in Java servlet request (e.g. GET, POST, etc.).
* The path is the endpoint in the associated servlet where the ACL has to be applied. If there is a REST path param this can be indicated
* with the sequence "{}".
*
Expand Down Expand Up @@ -84,15 +82,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
path = req.getPathInfo() != null ? req.getPathInfo().substring(1) : "";
final String authMethodSearch = method + ":" + path;
if (authMethodMap!=null) {
Optional<String> autMethodKey = authMethodMap.keySet().stream().
String autMethodKey = authMethodMap.keySet().stream().
filter( key -> {
String keyRegex = key.replaceFirst("\\*", ".*").replace("{}", "([^/]+)");
String keyRegex = key.replace("{}", "([^/]+)");
return authMethodSearch.matches(keyRegex);
} ).
sorted(Comparator.reverseOrder()).
findFirst();
if (autMethodKey.isPresent()) {
authMethod = authMethodMap.get(autMethodKey.get());
sorted(Comparator.naturalOrder()).
findFirst().
orElse(null);
if (autMethodKey != null) {
authMethod = authMethodMap.get(autMethodKey);
}
}
try {
Expand Down

0 comments on commit 2deebb1

Please sign in to comment.