diff --git a/sonic-server-common/src/main/java/org/cloud/sonic/common/config/CommonResultControllerAdvice.java b/sonic-server-common/src/main/java/org/cloud/sonic/common/config/CommonResultControllerAdvice.java index 6fbe46d4..bfabd4fc 100644 --- a/sonic-server-common/src/main/java/org/cloud/sonic/common/config/CommonResultControllerAdvice.java +++ b/sonic-server-common/src/main/java/org/cloud/sonic/common/config/CommonResultControllerAdvice.java @@ -19,7 +19,6 @@ import org.cloud.sonic.common.http.RespModel; import org.springframework.context.MessageSource; -import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -37,7 +36,7 @@ * @author JayWenStar, Eason * @date 2022/4/11 1:59 上午 */ -@ControllerAdvice({"org.cloud.sonic.controller.controller", "org.cloud.sonic.folder.controller"}) +@ControllerAdvice public class CommonResultControllerAdvice implements ResponseBodyAdvice { @Resource private MessageSource messageSource; @@ -50,8 +49,20 @@ public boolean supports(MethodParameter returnType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { MappingJacksonValue container = getOrCreateContainer(body); - String language = "zh_CN"; + String l = request.getHeaders().getFirst("Accept-Language"); + // Get return body + Object returnBody = container.getValue(); + + if (returnBody instanceof RespModel) { + RespModel baseResponse = (RespModel) returnBody; + process(l, baseResponse, messageSource); + } + return container; + } + + public static RespModel process(String l, RespModel respModel,MessageSource messageSource) { + String language = "zh_CN"; if (l != null) { language = l; } @@ -62,17 +73,11 @@ public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType } else { locale = new Locale("zh", "CN"); } - // Get return body - Object returnBody = container.getValue(); - - if (returnBody instanceof RespModel) { - RespModel baseResponse = (RespModel) returnBody; - baseResponse.setMessage(messageSource.getMessage(baseResponse.getMessage(), new Object[]{}, locale)); - } - return container; + respModel.setMessage(messageSource.getMessage(respModel.getMessage(), new Object[]{}, locale)); + return respModel; } private MappingJacksonValue getOrCreateContainer(Object body) { return (body instanceof MappingJacksonValue ? (MappingJacksonValue) body : new MappingJacksonValue(body)); } -} \ No newline at end of file +} diff --git a/sonic-server-common/src/main/java/org/cloud/sonic/common/config/GlobalWebException.java b/sonic-server-common/src/main/java/org/cloud/sonic/common/config/GlobalWebException.java index e8f85b20..e8bde4d7 100644 --- a/sonic-server-common/src/main/java/org/cloud/sonic/common/config/GlobalWebException.java +++ b/sonic-server-common/src/main/java/org/cloud/sonic/common/config/GlobalWebException.java @@ -22,6 +22,7 @@ import org.cloud.sonic.common.http.RespModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.Order; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; @@ -36,6 +37,7 @@ * @date 2021/8/15 18:26 */ @RestControllerAdvice +@Order(1) public class GlobalWebException { private final Logger logger = LoggerFactory.getLogger(GlobalWebException.class); @@ -57,4 +59,4 @@ public RespModel ErrHandler(Exception exception) { return new RespModel(RespEnum.UNKNOWN_ERROR); } } -} \ No newline at end of file +} diff --git a/sonic-server-common/src/main/java/org/cloud/sonic/common/exception/SonicException.java b/sonic-server-common/src/main/java/org/cloud/sonic/common/exception/SonicException.java index 888b980b..79b9d75a 100644 --- a/sonic-server-common/src/main/java/org/cloud/sonic/common/exception/SonicException.java +++ b/sonic-server-common/src/main/java/org/cloud/sonic/common/exception/SonicException.java @@ -17,6 +17,11 @@ public SonicException(String message) { super(message); } + public SonicException(String message, Object errorData) { + super(message); + this.errorData = errorData; + } + public SonicException(String message, Throwable cause) { super(message, cause); } diff --git a/sonic-server-common/src/main/resources/i18n/sonic_en_US.properties b/sonic-server-common/src/main/resources/i18n/sonic_en_US.properties index 5008e03e..6c3630ad 100644 --- a/sonic-server-common/src/main/resources/i18n/sonic_en_US.properties +++ b/sonic-server-common/src/main/resources/i18n/sonic_en_US.properties @@ -46,4 +46,5 @@ register.disable=Registration portal is closed! password.change.ok=Password modification succeeded! password.auth.fail=Old password error! device.not.offline=Device is Online! -permission.denied=Permission denied! \ No newline at end of file +permission.denied=Permission denied! +not.found.resource=No current uri found, please restart or actively refresh! diff --git a/sonic-server-common/src/main/resources/i18n/sonic_ja_JP.properties b/sonic-server-common/src/main/resources/i18n/sonic_ja_JP.properties index 571c9861..fae9171f 100644 --- a/sonic-server-common/src/main/resources/i18n/sonic_ja_JP.properties +++ b/sonic-server-common/src/main/resources/i18n/sonic_ja_JP.properties @@ -46,4 +46,5 @@ register.disable=登録機能が利用できません! password.change.ok=パスワード変更成功! password.auth.fail=旧パスワードが間違っています! device.not.offline=デバイスがまだオンライン! -permission.denied=現在のユーザーには権限がありません! \ No newline at end of file +permission.denied=現在のユーザーには権限がありません! +not.found.resource=現在の URI が見つかりません。再起動するか、積極的に更新してください! diff --git a/sonic-server-common/src/main/resources/i18n/sonic_zh_CN.properties b/sonic-server-common/src/main/resources/i18n/sonic_zh_CN.properties index 9ee3c788..08153f5f 100644 --- a/sonic-server-common/src/main/resources/i18n/sonic_zh_CN.properties +++ b/sonic-server-common/src/main/resources/i18n/sonic_zh_CN.properties @@ -46,4 +46,5 @@ register.disable=注册入口已关闭! password.change.ok=修改密码成功! password.auth.fail=旧密码错误! device.not.offline=设备未离线! -permission.denied=当前用户暂无权限! \ No newline at end of file +permission.denied=当前用户暂无权限! +not.found.resource=没有找到当前 uri, 请重新启动或主动刷新! diff --git a/sonic-server-common/src/main/resources/i18n/sonic_zh_TW.properties b/sonic-server-common/src/main/resources/i18n/sonic_zh_TW.properties index d59d5d2b..0497f931 100644 --- a/sonic-server-common/src/main/resources/i18n/sonic_zh_TW.properties +++ b/sonic-server-common/src/main/resources/i18n/sonic_zh_TW.properties @@ -46,4 +46,5 @@ register.disable=註册入口已關閉! password.change.ok=修改密碼成功! password.auth.fail=舊密碼錯誤! device.not.offline=設備未離線! -permission.denied=當前用戶暫無許可權! \ No newline at end of file +permission.denied=當前用戶暫無許可權! +not.found.resource=沒有找到當前 uri, 請重新啟動或主動刷新! diff --git a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/config/PermissionFilter.java b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/config/PermissionFilter.java index 73fec5d6..b1b48703 100644 --- a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/config/PermissionFilter.java +++ b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/config/PermissionFilter.java @@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; +import org.cloud.sonic.common.config.CommonResultControllerAdvice; import org.cloud.sonic.common.http.RespEnum; import org.cloud.sonic.common.http.RespModel; import org.cloud.sonic.common.tools.JWTTokenTool; @@ -28,9 +29,11 @@ import org.cloud.sonic.controller.services.RolesServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.MessageSource; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; +import javax.annotation.Resource; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -64,6 +67,9 @@ public class PermissionFilter extends OncePerRequestFilter { public static final String TOKEN = "SonicToken"; + @Resource + private MessageSource messageSource; + @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String token = request.getHeader(TOKEN); @@ -84,7 +90,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse if (resources == null) { response.setContentType("text/plain;charset=UTF-8"); - JSONObject re = (JSONObject) JSONObject.toJSON(new RespModel(RespEnum.RESOURCE_NOT_FOUND)); + JSONObject re = (JSONObject) JSONObject.toJSON(process(request, new RespModel(RespEnum.RESOURCE_NOT_FOUND))); response.getWriter().write(re.toJSONString()); return; } @@ -96,7 +102,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse if (!rolesServices.checkUserHasResourceAuthorize(userName, resourceName, method)) { response.setContentType("text/plain;charset=UTF-8"); - JSONObject re = (JSONObject) JSONObject.toJSON(new RespModel(RespEnum.PERMISSION_DENIED)); + JSONObject re = (JSONObject) JSONObject.toJSON(process(request, new RespModel(RespEnum.PERMISSION_DENIED))); response.getWriter().write(re.toJSONString()); return; } @@ -106,4 +112,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } + private RespModel process(HttpServletRequest request, RespModel respModel) { + String l = request.getHeader("Accept-Language"); + CommonResultControllerAdvice.process(l, respModel, messageSource); + + return respModel; + } + + }