- restore generic signatures, scoped variables and missing code from original bytecode - remove decompiler artifacts and document classes, methods, fields and packages - restore resources, add Batik/BouncyCastle dependencies and build/runtime docs
180 lines
6.1 KiB
Java
180 lines
6.1 KiB
Java
package com.midm.controller;
|
||
|
||
import com.google.common.base.Strings;
|
||
import com.midm.model.org.User;
|
||
import com.midm.security.UserPrincipal;
|
||
import com.midm.service.org.SiteService;
|
||
import com.midm.service.org.UserService;
|
||
import com.midm.service.system.LocaleService;
|
||
import com.mitac.cell.utils.McStringUtils;
|
||
import java.util.Locale;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.security.core.Authentication;
|
||
import org.springframework.web.servlet.LocaleResolver;
|
||
|
||
/**
|
||
* 【Java 类型】{@code BaseController}
|
||
*
|
||
* 类职责:
|
||
* <ul>
|
||
* <li>提供该包内的可复用能力。</li>
|
||
* <li>通过字段/依赖注入持有运行期状态,字段含义见下方“核心字段”。</li>
|
||
* </ul>
|
||
*
|
||
* 核心字段(按声明顺序):
|
||
* <ul>
|
||
* <li>{@code localeResolver}(LocaleResolver):语言区域解析器</li>
|
||
* <li>{@code localeService}(LocaleService):语言区域服务</li>
|
||
* <li>{@code userService}(UserService):用户服务</li>
|
||
* <li>{@code siteService}(SiteService):站点服务</li>
|
||
* <li>{@code user}(User):用户</li>
|
||
* <li>{@code theme}(String):字段 theme</li>
|
||
* </ul>
|
||
*
|
||
* 协作对象:{@code LocaleResolver、LocaleService、UserService、SiteService、User}。
|
||
*
|
||
* 学习提示:先看类注释和 public 方法,再结合调用方理解它在整体流程中的位置。
|
||
*/
|
||
public class BaseController {
|
||
/**
|
||
* 语言区域解析器;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
protected LocaleResolver localeResolver;
|
||
/**
|
||
* 语言区域服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
protected LocaleService localeService;
|
||
/**
|
||
* 用户服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
protected UserService userService;
|
||
/**
|
||
* 站点服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
protected SiteService siteService;
|
||
/**
|
||
* 用户。
|
||
*/
|
||
protected User user = null;
|
||
/**
|
||
* 字段 theme。
|
||
*/
|
||
protected String theme = "default";
|
||
|
||
/**
|
||
* 初始化对象状态(补齐 UUID/时间戳/默认字段)。
|
||
*
|
||
* @param request HTTP 请求({@code HttpServletRequest})
|
||
* @param response HTTP 响应({@code HttpServletResponse})
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
*/
|
||
protected void init(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
|
||
this.init(request, response, authentication, (String)null);
|
||
}
|
||
|
||
/**
|
||
* 初始化对象状态(补齐 UUID/时间戳/默认字段)。
|
||
*
|
||
* 实现要点:包含 3 个条件分支、0 个循环。
|
||
*
|
||
* @param request HTTP 请求({@code HttpServletRequest})
|
||
* @param response HTTP 响应({@code HttpServletResponse})
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
* @param localeTag 参数 {@code localeTag}({@code String})
|
||
*/
|
||
protected void init(HttpServletRequest request, HttpServletResponse response, Authentication authentication, String localeTag) {
|
||
this.determineUser(request, authentication);
|
||
String userLocale = !Strings.isNullOrEmpty(localeTag) ? localeTag : (this.user != null ? this.user.getSettingAsString("language") : request.getLocale().toLanguageTag());
|
||
if (McStringUtils.isNullOrEmpty(userLocale)) {
|
||
userLocale = request.getLocale().toLanguageTag();
|
||
}
|
||
|
||
Locale altLocale = this.localeService.findAltLocale(userLocale);
|
||
if (this.user != null) {
|
||
String altLocaleTag = altLocale.toLanguageTag();
|
||
if (userLocale == null || !this.user.isSameLocale(altLocaleTag)) {
|
||
this.userService.changeLocale(this.user, altLocaleTag);
|
||
}
|
||
}
|
||
|
||
this.localeResolver.setLocale(request, response, altLocale);
|
||
}
|
||
|
||
/**
|
||
* 获取View。
|
||
*
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
* @param viewName 参数 {@code viewName}({@code String})
|
||
* @return 字符串结果
|
||
*/
|
||
protected String getView(Authentication authentication, String viewName) {
|
||
return this.getResource(authentication, viewName + ".html");
|
||
}
|
||
|
||
/**
|
||
* 获取Resource。
|
||
*
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
* @param fileName 文件名({@code String})
|
||
* @return 字符串结果
|
||
*/
|
||
protected String getResource(Authentication authentication, String fileName) {
|
||
User user = UserPrincipal.getUser(authentication);
|
||
String theme = user == null ? "default" : user.getTheme();
|
||
return theme + fileName;
|
||
}
|
||
|
||
/**
|
||
* 获取模板Resource。
|
||
*
|
||
* 实现要点:包含 1 个条件分支、0 个循环。
|
||
*
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
* @param fileName 文件名({@code String})
|
||
* @return 字符串结果
|
||
*/
|
||
protected String getTemplateResource(Authentication authentication, String fileName) {
|
||
if (this.user == null) {
|
||
this.user = UserPrincipal.getUser(authentication);
|
||
}
|
||
|
||
String theme = this.user == null ? "default" : this.user.getTheme();
|
||
return "templates/" + theme + fileName;
|
||
}
|
||
|
||
/**
|
||
* 处理determine用户。
|
||
*
|
||
* 实现要点:包含同步块;包含 1 个条件分支、0 个循环。
|
||
*
|
||
* @param request HTTP 请求({@code HttpServletRequest})
|
||
* @param authentication Spring Security 认证信息({@code Authentication})
|
||
*/
|
||
private void determineUser(HttpServletRequest request, Authentication authentication) {
|
||
if (authentication != null) {
|
||
synchronized(authentication) {
|
||
this.user = UserPrincipal.getUser(authentication);
|
||
this.theme = this.user != null ? this.user.getTheme() : "default";
|
||
}
|
||
} else {
|
||
this.user = null;
|
||
this.theme = "default";
|
||
}
|
||
|
||
}
|
||
}
|