|
| 1 | +package org.juv25d.filter; |
| 2 | + |
| 3 | +import java.util.regex.Pattern; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a URL redirect rule. |
| 7 | + * |
| 8 | + * A redirect rule consists of: |
| 9 | + * - sourcePath: The path to match (supports exact match or wildcards with *) |
| 10 | + * - targetUrl: The URL to redirect to |
| 11 | + * - statusCode: HTTP status code (301 for permanent, 302 for temporary) |
| 12 | + * |
| 13 | + * Examples: |
| 14 | + * - new RedirectRule("/old-page", "/new-page", 301) |
| 15 | + * - new RedirectRule("/temp", "https://example.com", 302) |
| 16 | + * - new RedirectRule("/docs/*", "/documentation/", 301) |
| 17 | + */ |
| 18 | +public class RedirectRule { |
| 19 | + private final String sourcePath; |
| 20 | + private final String targetUrl; |
| 21 | + private final int statusCode; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a redirect rule with exact path matching. |
| 25 | + * |
| 26 | + * @param sourcePath The path to match (e.g., "/old-page" or "/docs/*" for wildcard) |
| 27 | + * @param targetUrl The URL to redirect to |
| 28 | + * @param statusCode HTTP status code (301 or 302) |
| 29 | + */ |
| 30 | + public RedirectRule(String sourcePath, String targetUrl, int statusCode) { |
| 31 | + validateStatusCode(statusCode); |
| 32 | + this.sourcePath = sourcePath; |
| 33 | + this.targetUrl = targetUrl; |
| 34 | + this.statusCode = statusCode; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks if the given request path matches this rule. |
| 39 | + * |
| 40 | + * Supports: |
| 41 | + * - Exact matching: "/old-page" matches exactly "/old-page" |
| 42 | + * - Wildcard matching: "/docs/*" matches "/docs/api", "/docs/guide", etc. |
| 43 | + * |
| 44 | + * @param requestPath The request path to check |
| 45 | + * @return true if the path matches this rule |
| 46 | + */ |
| 47 | + public boolean matches(String requestPath) { |
| 48 | + if (requestPath == null) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Check for wildcard matching |
| 53 | + if (sourcePath.contains("*")) { |
| 54 | + // Split on wildcard, escape each literal segment, then rejoin with ".*" |
| 55 | + String[] parts = sourcePath.split("\\*", -1); |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + for (int i = 0; i < parts.length; i++) { |
| 58 | + sb.append(Pattern.quote(parts[i])); |
| 59 | + if (i < parts.length - 1) { |
| 60 | + sb.append(".*"); |
| 61 | + } |
| 62 | + } |
| 63 | + return requestPath.matches(sb.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + // Exact match |
| 67 | + return requestPath.equals(sourcePath); |
| 68 | + } |
| 69 | + |
| 70 | + public String getSourcePath() { |
| 71 | + return sourcePath; |
| 72 | + } |
| 73 | + |
| 74 | + public String getTargetUrl() { |
| 75 | + return targetUrl; |
| 76 | + } |
| 77 | + |
| 78 | + public int getStatusCode() { |
| 79 | + return statusCode; |
| 80 | + } |
| 81 | + |
| 82 | + private void validateStatusCode(int statusCode) { |
| 83 | + if (statusCode != 301 && statusCode != 302) { |
| 84 | + throw new IllegalArgumentException( |
| 85 | + "Status code must be 301 (Moved Permanently) or 302 (Found). Got: " + statusCode |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String toString() { |
| 92 | + return "RedirectRule{" + |
| 93 | + "sourcePath='" + sourcePath + '\'' + |
| 94 | + ", targetUrl='" + targetUrl + '\'' + |
| 95 | + ", statusCode=" + statusCode + |
| 96 | + '}'; |
| 97 | + } |
| 98 | +} |
0 commit comments