Skip to content

Commit 251a3a8

Browse files
authored
applied order filter annotations to existing filters (#107)
* applied order filter annotations to existing filters * put secfilter first * fixed orders
1 parent 7556101 commit 251a3a8

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

src/main/java/org/juv25d/App.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.juv25d.filter.*;
44
import org.juv25d.logging.ServerLogging;
55
import org.juv25d.http.HttpParser;
6-
import org.juv25d.plugin.NotFoundPlugin; // New import
6+
import org.juv25d.plugin.NotFoundPlugin;
77
import org.juv25d.plugin.StaticFilesPlugin;
8-
import org.juv25d.router.SimpleRouter; // New import
8+
import org.juv25d.router.SimpleRouter;
99
import org.juv25d.util.ConfigLoader;
1010

1111
import java.util.List;
@@ -18,42 +18,32 @@ public static void main(String[] args) {
1818
ConfigLoader config = ConfigLoader.getInstance();
1919
Logger logger = ServerLogging.getLogger();
2020
HttpParser httpParser = new HttpParser();
21-
2221
Pipeline pipeline = new Pipeline();
2322

2423
pipeline.addGlobalFilter(new SecurityHeadersFilter(), 0);
2524

26-
// Configure redirect rules
25+
pipeline.addGlobalFilter(new LoggingFilter(), 1);
26+
27+
pipeline.addGlobalFilter(new IpFilter(Set.of(), Set.of()), 2);
28+
29+
if (config.isRateLimitingEnabled()) {pipeline.addGlobalFilter(new RateLimitingFilter(
30+
config.getRequestsPerMinute(), config.getBurstCapacity()), 3);}
31+
2732
List<RedirectRule> redirectRules = List.of(
2833
new RedirectRule("/old-page", "/new-page", 301),
2934
new RedirectRule("/temp", "https://example.com/temporary", 302),
3035
new RedirectRule("/docs/*", "/documentation/", 301)
3136
);
32-
pipeline.addGlobalFilter(new RedirectFilter(redirectRules), 0);
33-
34-
// IP filter is enabled but configured with open access during development
35-
// White/blacklist can be tightened when specific IP restrictions are decided
36-
pipeline.addGlobalFilter(new IpFilter(
37-
Set.of(),
38-
Set.of()
39-
), 0);
4037

41-
pipeline.addGlobalFilter(new LoggingFilter(), 0);
38+
pipeline.addGlobalFilter(new RedirectFilter(redirectRules), 4);
4239

43-
if (config.isRateLimitingEnabled()) {
44-
pipeline.addGlobalFilter(new RateLimitingFilter(
45-
config.getRequestsPerMinute(),
46-
config.getBurstCapacity()
47-
), 0);
48-
}
4940

50-
// Initialize and configure SimpleRouter
5141
SimpleRouter router = new SimpleRouter();
52-
router.registerPlugin("/", new StaticFilesPlugin()); // Register StaticFilesPlugin for the root path
53-
router.registerPlugin("/*", new StaticFilesPlugin()); // Register StaticFilesPlugin for all paths
54-
router.registerPlugin("/notfound", new NotFoundPlugin()); // Example: Register NotFoundPlugin for a specific path
42+
router.registerPlugin("/", new StaticFilesPlugin());
43+
router.registerPlugin("/*", new StaticFilesPlugin());
44+
router.registerPlugin("/notfound", new NotFoundPlugin());
5545

56-
pipeline.setRouter(router); // Set the router in the pipeline
46+
pipeline.setRouter(router);
5747

5848
DefaultConnectionHandlerFactory handlerFactory =
5949
new DefaultConnectionHandlerFactory(httpParser, logger, pipeline);
@@ -64,7 +54,6 @@ public static void main(String[] args) {
6454
handlerFactory,
6555
pipeline
6656
);
67-
6857
server.start();
6958
}
7059
}

src/main/java/org/juv25d/filter/IpFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.juv25d.filter;
22

3+
import org.juv25d.filter.annotation.Global;
34
import org.juv25d.http.HttpRequest;
45
import org.juv25d.http.HttpResponse;
56

67
import java.io.IOException;
78
import java.nio.charset.StandardCharsets;
89
import java.util.Set;
910

11+
@Global(order = 2)
1012
public class IpFilter implements Filter {
1113

1214
private final Set<String> whitelist;

src/main/java/org/juv25d/filter/LoggingFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.juv25d.filter;
22

3+
import org.juv25d.filter.annotation.Global;
34
import org.juv25d.http.HttpRequest;
45
import org.juv25d.http.HttpResponse;
56

67
import java.io.IOException;
78

9+
@Global(order = 1)
810
public class LoggingFilter implements Filter {
911

1012
@Override

src/main/java/org/juv25d/filter/RateLimitingFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.bucket4j.Bandwidth;
44
import io.github.bucket4j.Bucket;
55
import io.github.bucket4j.Refill;
6+
import org.juv25d.filter.annotation.Global;
67
import org.juv25d.http.HttpRequest;
78
import org.juv25d.http.HttpResponse;
89
import org.juv25d.logging.ServerLogging;
@@ -18,6 +19,7 @@
1819
* A filter that implements rate limiting for incoming HTTP requests.
1920
* It uses a token bucket algorithm via Bucket4J to limit the number of requests per client IP.
2021
*/
22+
@Global(order = 3)
2123
public class RateLimitingFilter implements Filter {
2224

2325
private static final Logger logger = ServerLogging.getLogger();

src/main/java/org/juv25d/filter/RedirectFilter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.juv25d.filter;
22

3+
import org.juv25d.filter.annotation.Global;
34
import org.juv25d.http.HttpRequest;
45
import org.juv25d.http.HttpResponse;
56

@@ -27,6 +28,8 @@
2728
* pipeline.addFilter(new RedirectFilter(rules));
2829
* </pre>
2930
*/
31+
32+
@Global(order = 4)
3033
public class RedirectFilter implements Filter {
3134
private final List<RedirectRule> rules;
3235
private final Logger logger;

0 commit comments

Comments
 (0)