mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
RouteContext新增isv对象
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.gitee.sop.gateway.common;
|
||||
|
||||
import com.gitee.sop.gateway.request.ApiRequestContext;
|
||||
import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -13,5 +14,6 @@ public class RouteContext {
|
||||
|
||||
private ApiRequestContext apiRequestContext;
|
||||
private ApiInfoDTO apiInfo;
|
||||
private IsvDTO isv;
|
||||
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
package com.gitee.sop.gateway.service;
|
||||
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
import com.gitee.sop.gateway.request.ApiRequestContext;
|
||||
import com.gitee.sop.gateway.response.ApiResponse;
|
||||
import com.gitee.sop.gateway.common.RouteContext;
|
||||
import com.gitee.sop.gateway.response.Response;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 结果包裹
|
||||
*
|
||||
@@ -12,10 +12,6 @@ import com.gitee.sop.gateway.response.Response;
|
||||
*/
|
||||
public interface ResultWrapper {
|
||||
|
||||
Response wrap(ApiRequestContext context, ApiInfoDTO apiInfoDTO, Object result);
|
||||
|
||||
default Response wrap(ApiRequestContext context, ApiResponse apiResponse) {
|
||||
return wrap(context, null, apiResponse);
|
||||
}
|
||||
Response wrap(Optional<RouteContext> routeContextOpt, Object result);
|
||||
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package com.gitee.sop.gateway.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
import com.gitee.sop.gateway.request.ApiRequestContext;
|
||||
import com.gitee.sop.gateway.service.validate.ValidateReturn;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
|
||||
public class RestRouteServiceImpl extends RouteServiceImpl {
|
||||
|
||||
@Override
|
||||
protected ApiInfoDTO validate(ApiRequestContext apiRequestContext) {
|
||||
protected ValidateReturn validate(ApiRequestContext apiRequestContext) {
|
||||
return validator.validateRest(apiRequestContext);
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package com.gitee.sop.gateway.service.impl;
|
||||
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
import com.gitee.sop.gateway.common.RouteContext;
|
||||
import com.gitee.sop.gateway.common.enums.YesOrNoEnum;
|
||||
import com.gitee.sop.gateway.config.ApiConfig;
|
||||
import com.gitee.sop.gateway.request.ApiRequestContext;
|
||||
import com.gitee.sop.gateway.response.ApiResponse;
|
||||
import com.gitee.sop.gateway.response.NoCommonResponse;
|
||||
import com.gitee.sop.gateway.response.Response;
|
||||
@@ -11,6 +11,8 @@ import com.gitee.sop.gateway.service.ResultWrapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@@ -21,11 +23,11 @@ public class ResultWrapperImpl implements ResultWrapper {
|
||||
private ApiConfig apiConfig;
|
||||
|
||||
@Override
|
||||
public Response wrap(ApiRequestContext context, ApiInfoDTO apiInfoDTO, Object result) {
|
||||
Integer hasCommonResponse = apiInfoDTO == null ?
|
||||
YesOrNoEnum.YES.getValue()
|
||||
: apiInfoDTO.getHasCommonResponse();
|
||||
boolean needNotWrap = YesOrNoEnum.of(hasCommonResponse) == YesOrNoEnum.NO;
|
||||
public Response wrap(Optional<RouteContext> routeContextOpt, Object result) {
|
||||
boolean needNotWrap = routeContextOpt.map(RouteContext::getApiInfo)
|
||||
.map(ApiInfoDTO::getHasCommonResponse)
|
||||
.map(YesOrNoEnum::of)
|
||||
.orElse(YesOrNoEnum.YES) == YesOrNoEnum.NO;
|
||||
if (result instanceof ApiResponse) {
|
||||
ApiResponse apiResponse = (ApiResponse) result;
|
||||
return executeApiResponse(apiResponse, needNotWrap);
|
||||
|
@@ -18,6 +18,7 @@ import com.gitee.sop.gateway.service.GenericServiceInvoker;
|
||||
import com.gitee.sop.gateway.service.ResultWrapper;
|
||||
import com.gitee.sop.gateway.service.RouteService;
|
||||
import com.gitee.sop.gateway.service.Serde;
|
||||
import com.gitee.sop.gateway.service.validate.ValidateReturn;
|
||||
import com.gitee.sop.gateway.service.validate.Validator;
|
||||
import com.gitee.sop.gateway.util.ClassUtil;
|
||||
import com.gitee.sop.support.constant.SopConstants;
|
||||
@@ -87,28 +88,34 @@ public class RouteServiceImpl implements RouteService {
|
||||
log.info("收到客户端请求, ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest);
|
||||
try {
|
||||
// 接口校验
|
||||
ApiInfoDTO apiInfoDTO = validate(apiRequestContext);
|
||||
RouteContext routeContext = new RouteContext(apiRequestContext, apiInfoDTO);
|
||||
ValidateReturn validateReturn = validate(apiRequestContext);
|
||||
RouteContext routeContext = new RouteContext(
|
||||
apiRequestContext,
|
||||
validateReturn.getApiInfoDTO(),
|
||||
validateReturn.getIsvDTO()
|
||||
);
|
||||
// 执行拦截器前置动作
|
||||
this.doPreRoute(routeContext);
|
||||
// 执行路由,返回微服务结果
|
||||
Object result = doRoute(apiRequestContext, apiInfoDTO);
|
||||
Object result = doRoute(routeContext);
|
||||
// 执行拦截器后置动作
|
||||
result = this.doAfterRoute(routeContext, result);
|
||||
// 结果处理
|
||||
return resultWrapper.wrap(apiRequestContext, apiInfoDTO, result);
|
||||
return resultWrapper.wrap(Optional.of(routeContext), result);
|
||||
} catch (Exception e) {
|
||||
log.error("接口请求报错, , ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest, e);
|
||||
ApiResponse apiResponse = exceptionExecutor.executeException(apiRequestContext, e);
|
||||
return resultWrapper.wrap(apiRequestContext, apiResponse);
|
||||
return resultWrapper.wrap(Optional.empty(), apiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
protected ApiInfoDTO validate(ApiRequestContext apiRequestContext) {
|
||||
protected ValidateReturn validate(ApiRequestContext apiRequestContext) {
|
||||
return validator.validate(apiRequestContext);
|
||||
}
|
||||
|
||||
protected Object doRoute(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfo) {
|
||||
protected Object doRoute(RouteContext routeContext) {
|
||||
ApiRequestContext apiRequestContext = routeContext.getApiRequestContext();
|
||||
ApiInfoDTO apiInfo = routeContext.getApiInfo();
|
||||
String tag = apiRequestContext.getTag();
|
||||
// 设置隔离环境
|
||||
RpcContextAttachment clientAttachment = RpcContext.getClientAttachment();
|
||||
|
@@ -83,7 +83,7 @@ public class ApiValidator implements Validator {
|
||||
private DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
@Override
|
||||
public ApiInfoDTO validate(ApiRequestContext apiRequestContext) {
|
||||
public ValidateReturn validate(ApiRequestContext apiRequestContext) {
|
||||
ApiRequest apiRequest = apiRequestContext.getApiRequest();
|
||||
ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion());
|
||||
// 检查接口信息
|
||||
@@ -106,11 +106,11 @@ public class ApiValidator implements Validator {
|
||||
checkUploadFile(apiRequestContext);
|
||||
// 检查token
|
||||
checkToken(apiRequestContext, apiInfo);
|
||||
return apiInfo;
|
||||
return new ValidateReturn(apiInfo, isvDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiInfoDTO validateRest(ApiRequestContext apiRequestContext) {
|
||||
public ValidateReturn validateRest(ApiRequestContext apiRequestContext) {
|
||||
ApiRequest apiRequest = apiRequestContext.getApiRequest();
|
||||
ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion());
|
||||
// 检查接口信息
|
||||
@@ -126,7 +126,7 @@ public class ApiValidator implements Validator {
|
||||
checkUploadFile(apiRequestContext);
|
||||
// 检查token
|
||||
checkToken(apiRequestContext, apiInfo);
|
||||
return apiInfo;
|
||||
return new ValidateReturn(apiInfo, null);
|
||||
}
|
||||
|
||||
public void checkApiInfo(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfoDTO) {
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package com.gitee.sop.gateway.service.validate;
|
||||
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class ValidateReturn {
|
||||
private ApiInfoDTO apiInfoDTO;
|
||||
private IsvDTO isvDTO;
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
package com.gitee.sop.gateway.service.validate;
|
||||
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
import com.gitee.sop.gateway.request.ApiRequestContext;
|
||||
|
||||
/**
|
||||
@@ -15,7 +14,7 @@ public interface Validator {
|
||||
* @param apiRequestContext 请求内容
|
||||
* @return 校验通过返回路由信息
|
||||
*/
|
||||
ApiInfoDTO validate(ApiRequestContext apiRequestContext);
|
||||
ValidateReturn validate(ApiRequestContext apiRequestContext);
|
||||
|
||||
/**
|
||||
* 校验rest
|
||||
@@ -23,6 +22,6 @@ public interface Validator {
|
||||
* @param apiRequestContext 请求内容
|
||||
* @return 校验通过返回路由信息
|
||||
*/
|
||||
ApiInfoDTO validateRest(ApiRequestContext apiRequestContext);
|
||||
ValidateReturn validateRest(ApiRequestContext apiRequestContext);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user