|
2 | 2 |
|
3 | 3 | import org.juv25d.filter.Filter; |
4 | 4 | import org.juv25d.filter.FilterChainImpl; |
| 5 | +import org.juv25d.filter.FilterMatcher; |
5 | 6 | import org.juv25d.http.HttpRequest; |
6 | | -import org.juv25d.router.Router; // New import |
| 7 | +import org.juv25d.router.Router; |
7 | 8 |
|
8 | | -import java.util.ArrayList; |
9 | | -import java.util.Collections; |
10 | 9 | import java.util.List; |
11 | | -import java.util.Map; |
12 | | -import java.util.concurrent.ConcurrentHashMap; |
13 | | -import java.util.concurrent.CopyOnWriteArrayList; |
14 | | -import java.util.stream.Collectors; |
15 | 10 |
|
16 | 11 | public class Pipeline { |
17 | 12 |
|
18 | | - private final List<FilterRegistration> globalFilters = new CopyOnWriteArrayList<>(); |
19 | | - private final Map<String, List<FilterRegistration>> routeFilters = new ConcurrentHashMap<>(); |
20 | | - private volatile List<Filter> sortedGlobalFilters = List.of(); |
21 | | - private volatile Router router; // Changed from Plugin plugin; |
| 13 | + private final FilterMatcher matcher; |
| 14 | + private final Router router; |
22 | 15 |
|
23 | | - public void addGlobalFilter(Filter filter, int order) { |
24 | | - globalFilters.add(new FilterRegistration(filter, order, null)); |
25 | | - sortedGlobalFilters = globalFilters.stream() |
26 | | - .sorted() |
27 | | - .map(FilterRegistration::filter) |
28 | | - .collect(Collectors.toUnmodifiableList()); |
29 | | - } |
30 | | - |
31 | | - public void addRouteFilter(Filter filter, int order, String pattern) { |
32 | | - routeFilters.computeIfAbsent(pattern, k -> new CopyOnWriteArrayList<>()) |
33 | | - .add(new FilterRegistration(filter, order, pattern)); |
34 | | - } |
35 | | - |
36 | | - public void setRouter(Router router) { |
37 | | - if (router == null) { |
38 | | - throw new IllegalArgumentException("Router cannot be null"); |
39 | | - } |
| 16 | + public Pipeline(FilterMatcher matcher, Router router) { |
| 17 | + this.matcher = matcher; |
40 | 18 | this.router = router; |
41 | 19 | } |
42 | 20 |
|
43 | 21 | public FilterChainImpl createChain(HttpRequest request) { |
44 | | - List<Filter> filters = new ArrayList<>(); |
45 | | - filters.addAll(sortedGlobalFilters); |
46 | | - String path = request.path(); |
47 | | - List<FilterRegistration> exactMatches = routeFilters.get(path); |
48 | | - if (exactMatches != null) { |
49 | | - exactMatches.stream() |
50 | | - .sorted() |
51 | | - .map(FilterRegistration::filter) |
52 | | - .forEach(filters::add); |
53 | | - } |
54 | | - |
55 | | - for (Map.Entry<String, List<FilterRegistration>> entry : routeFilters.entrySet()) { |
56 | | - String pattern = entry.getKey(); |
57 | | - if (pattern.endsWith("*") && path.startsWith(pattern.substring(0, pattern.length() - 1))) { |
58 | | - entry.getValue().stream() |
59 | | - .sorted() |
60 | | - .map(FilterRegistration::filter) |
61 | | - .forEach(filters::add); |
62 | | - } |
63 | | - } |
64 | | - return new FilterChainImpl(filters, router); // Pass router instead of plugin |
65 | | - } |
66 | | - |
67 | | - public List<Filter> getFilters() { |
68 | | - return Collections.unmodifiableList(sortedGlobalFilters); |
69 | | - } |
70 | | - |
71 | | - public Router getRouter() { // Renamed from getPlugin |
72 | | - return router; |
| 22 | + List<Filter> filters = matcher.match(request); |
| 23 | + return new FilterChainImpl(filters, router); |
73 | 24 | } |
74 | 25 | } |
0 commit comments