Skip to content

Commit

Permalink
feat: 优化响应信息处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yaming116 committed Dec 12, 2022
1 parent af30982 commit 4850c8b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object> {
@Resource
private MessageSource messageSource;
Expand All @@ -50,8 +49,20 @@ public boolean supports(MethodParameter returnType, Class<? extends HttpMessageC
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> 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;
}
Expand All @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@
* @date 2021/8/15 18:26
*/
@RestControllerAdvice
@Order(1)
public class GlobalWebException {
private final Logger logger = LoggerFactory.getLogger(GlobalWebException.class);

Expand All @@ -57,4 +59,4 @@ public RespModel ErrHandler(Exception exception) {
return new RespModel(RespEnum.UNKNOWN_ERROR);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
permission.denied=Permission denied!
not.found.resource=No current uri found, please restart or actively refresh!
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ register.disable=登録機能が利用できません!
password.change.ok=パスワード変更成功!
password.auth.fail=旧パスワードが間違っています!
device.not.offline=デバイスがまだオンライン!
permission.denied=現在のユーザーには権限がありません!
permission.denied=現在のユーザーには権限がありません!
not.found.resource=現在の URI が見つかりません。再起動するか、積極的に更新してください!
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ register.disable=注册入口已关闭!
password.change.ok=修改密码成功!
password.auth.fail=旧密码错误!
device.not.offline=设备未离线!
permission.denied=当前用户暂无权限!
permission.denied=当前用户暂无权限!
not.found.resource=没有找到当前 uri, 请重新启动或主动刷新!
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ register.disable=註册入口已關閉!
password.change.ok=修改密碼成功!
password.auth.fail=舊密碼錯誤!
device.not.offline=設備未離線!
permission.denied=當前用戶暫無許可權!
permission.denied=當前用戶暫無許可權!
not.found.resource=沒有找到當前 uri, 請重新啟動或主動刷新!
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}


}

0 comments on commit 4850c8b

Please sign in to comment.