|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
1 | 7 | package modelengine.fit.http.util.i18n; |
2 | 8 |
|
3 | 9 | import modelengine.fit.http.server.*; |
|
8 | 14 | import java.util.List; |
9 | 15 | import java.util.Locale; |
10 | 16 |
|
| 17 | +/** |
| 18 | + * 地区解析过滤器。 |
| 19 | + * |
| 20 | + * @author 阮睿 |
| 21 | + * @since 2025-08-01 |
| 22 | + */ |
11 | 23 | public class LocaleResolveFilter implements HttpServerFilter { |
12 | 24 |
|
13 | 25 | private LocaleResolver localeResolver = null; |
@@ -46,13 +58,21 @@ public List<String> mismatchPatterns() { |
46 | 58 | return mismatchPatterns; |
47 | 59 | } |
48 | 60 |
|
| 61 | + /** |
| 62 | + * 过滤请求,从请求中解析地区值并放入线程上下文和cookie。 |
| 63 | + * |
| 64 | + * @param request 表示服务器请求 {@link HttpClassicServerRequest}。 |
| 65 | + * @param response 表示服务器响应 {@link HttpClassicServerResponse}。 |
| 66 | + * @param chain 过滤链 {@link HttpServerFilterChain}。 |
| 67 | + */ |
49 | 68 | @Override |
50 | | - public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse response, HttpServerFilterChain chain) throws DoHttpServerFilterException { |
| 69 | + public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse response, |
| 70 | + HttpServerFilterChain chain) throws DoHttpServerFilterException { |
51 | 71 | try { |
52 | 72 | // 如果参数中带有地区,则直接设置地区 |
53 | | - String paramLocale = request.queries().first("locale").orElse( null); |
| 73 | + String paramLocale = request.queries().first("locale").orElse(null); |
54 | 74 | Locale responseLocale = null; |
55 | | - if(paramLocale != null && !paramLocale.trim().isEmpty()){ |
| 75 | + if (paramLocale != null && !paramLocale.trim().isEmpty()) { |
56 | 76 | responseLocale = Locale.forLanguageTag(paramLocale); |
57 | 77 | LocaleContextHolder.setLocaleContext(new LocaleContext(responseLocale)); |
58 | 78 | } |
|
0 commit comments