|
| 1 | +package org.example.filter; |
| 2 | + |
| 3 | +import org.example.http.HttpResponseBuilder; |
| 4 | +import org.example.httpparser.HttpRequest; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/** |
| 9 | + * Filter that determines the preferred locale for an HTTP request using cookies and headers. |
| 10 | + * <p> |
| 11 | + * First, it checks for a locale set in a cookie named "user-lang". If the cookie is missing, |
| 12 | + * blank, or malformed, it falls back to the Accept-Language header. If neither is present |
| 13 | + * or valid, the filter defaults to "en-US". |
| 14 | + * <p> |
| 15 | + * The selected locale is stored in a ThreadLocal variable so it can be accessed throughout |
| 16 | + * the processing of the request. |
| 17 | + * <p> |
| 18 | + * This filter does not modify the response or stop the filter chain; it only sets the |
| 19 | + * current locale and forwards the request to the next filter. |
| 20 | + * <p> |
| 21 | + * ThreadLocal cleanup is performed after the filter chain completes to prevent memory leaks. |
| 22 | + */ |
| 23 | +public class LocaleFilterWithCookie implements Filter { |
| 24 | + |
| 25 | + private static final String DEFAULT_LOCALE = "en-US"; |
| 26 | + private static final String LOCALE_COOKIE_NAME = "user-lang"; |
| 27 | + private static final ThreadLocal<String> currentLocale = new ThreadLocal<>(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public void init() { |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void doFilter(HttpRequest request, HttpResponseBuilder response, FilterChain chain) { |
| 35 | + try { |
| 36 | + String locale = resolveLocale(request); |
| 37 | + currentLocale.set(locale); |
| 38 | + |
| 39 | + chain.doFilter(request, response); |
| 40 | + } finally { |
| 41 | + currentLocale.remove(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void destroy() { |
| 47 | + } |
| 48 | + |
| 49 | + public static String getCurrentLocale() { |
| 50 | + String locale = currentLocale.get(); |
| 51 | + if (locale != null) { |
| 52 | + return locale; |
| 53 | + } else { |
| 54 | + return DEFAULT_LOCALE; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private String resolveLocale(HttpRequest request) { |
| 59 | + String cookieLocale = extractLocaleFromCookie(request); |
| 60 | + if (cookieLocale != null && !cookieLocale.isBlank()) { |
| 61 | + return cookieLocale; |
| 62 | + } |
| 63 | + |
| 64 | + String headerLocale = extractLocaleFromHeader(request); |
| 65 | + if (headerLocale != null && !headerLocale.isBlank()) { |
| 66 | + return headerLocale; |
| 67 | + } |
| 68 | + |
| 69 | + return DEFAULT_LOCALE; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Extracts the locale from the "user-lang" cookie if present. |
| 74 | + * <p> |
| 75 | + * If the cookie header is missing, blank, or malformed, returns null. |
| 76 | + */ |
| 77 | + private String extractLocaleFromCookie(HttpRequest request) { |
| 78 | + Map<String, String> headers = request.getHeaders(); |
| 79 | + if (headers == null) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + String cookieHeader = headers.entrySet().stream() |
| 84 | + .filter(e -> e.getKey() != null && e.getKey().equalsIgnoreCase("Cookie")) |
| 85 | + .map(Map.Entry::getValue) |
| 86 | + .findFirst() |
| 87 | + .orElse(null); |
| 88 | + |
| 89 | + if (cookieHeader == null || cookieHeader.isBlank()) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + String[] cookies = cookieHeader.split(";"); |
| 94 | + for (String cookie : cookies) { |
| 95 | + String[] parts = cookie.trim().split("=", 2); |
| 96 | + if (parts.length == 2) { |
| 97 | + String name = parts[0].trim(); |
| 98 | + String value = parts[1].trim(); |
| 99 | + if (LOCALE_COOKIE_NAME.equals(name) && !value.isBlank()) { |
| 100 | + return value; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Extracts the preferred locale from the Accept-Language header of the request. |
| 110 | + * <p> |
| 111 | + * If the header is missing, blank, or malformed, returns null. |
| 112 | + * The first language tag is used and any optional quality value (e.g., ";q=0.9") is stripped. |
| 113 | + */ |
| 114 | + private String extractLocaleFromHeader(HttpRequest request) { |
| 115 | + Map<String, String> headers = request.getHeaders(); |
| 116 | + if (headers == null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + String acceptLanguage = headers.entrySet().stream() |
| 121 | + .filter(e -> e.getKey() != null && e.getKey().equalsIgnoreCase("Accept-Language")) |
| 122 | + .map(Map.Entry::getValue) |
| 123 | + .findFirst() |
| 124 | + .orElse(null); |
| 125 | + |
| 126 | + if (acceptLanguage == null || acceptLanguage.isBlank()) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + String[] parts = acceptLanguage.split(","); |
| 131 | + if (parts.length == 0 || parts[0].isBlank()) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + String locale = parts[0].split(";")[0].trim(); |
| 136 | + return locale.isEmpty() ? null : locale; |
| 137 | + } |
| 138 | +} |
0 commit comments