diff --git a/common-lib/src/main/java/cn/axzo/foundation/caller/CallerApp.java b/common-lib/src/main/java/cn/axzo/foundation/caller/CallerApp.java index 13c62ff..a5206dd 100644 --- a/common-lib/src/main/java/cn/axzo/foundation/caller/CallerApp.java +++ b/common-lib/src/main/java/cn/axzo/foundation/caller/CallerApp.java @@ -12,6 +12,7 @@ import lombok.*; @AllArgsConstructor @Setter public class CallerApp { + String appName; AppEnvEnum appEnv; /** diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/config/DefaultWebMvcConfig.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/config/DefaultWebMvcConfig.java index b32768e..6ec0a69 100644 --- a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/config/DefaultWebMvcConfig.java +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/config/DefaultWebMvcConfig.java @@ -4,8 +4,10 @@ import cn.axzo.foundation.util.FastjsonUtils; import cn.axzo.foundation.web.support.AppRuntime; import cn.axzo.foundation.web.support.context.AxContextInterceptor; import cn.axzo.foundation.web.support.exception.AbstractExceptionHandler; +import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor; import cn.axzo.foundation.web.support.interceptors.PrettyPrintInterceptor; import cn.axzo.foundation.web.support.interceptors.PrintVerboseInterceptor; +import cn.axzo.foundation.web.support.resolvers.CallerAppResolver; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.ParserConfig; @@ -180,6 +182,7 @@ public class DefaultWebMvcConfig extends DelegatingWebMvcConfiguration implement // 不建议使用DelegatingWebMvcConfiguration.addCorsMappings. 使用该方法配置之后再使用自定义拦截器时跨域相关配置就会失效 registry.addInterceptor(new CorsInterceptor(corsProcessorType)); registry.addInterceptor(new PrettyPrintInterceptor()); + registry.addInterceptor(new CallerAppInterceptor(appRuntime)); super.addInterceptors(registry); @@ -204,6 +207,7 @@ public class DefaultWebMvcConfig extends DelegatingWebMvcConfiguration implement argumentResolvers.add(e); }); } + argumentResolvers.add(new CallerAppResolver()); } /** diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/interceptors/CallerAppInterceptor.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/interceptors/CallerAppInterceptor.java new file mode 100644 index 0000000..1177993 --- /dev/null +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/interceptors/CallerAppInterceptor.java @@ -0,0 +1,42 @@ +package cn.axzo.foundation.web.support.interceptors; + +import cn.axzo.foundation.caller.CallerApp; +import cn.axzo.foundation.web.support.AppRuntime; +import com.alibaba.fastjson.JSONObject; +import com.google.common.base.Strings; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 将header中的pretty放到response. JsonCovert会根据该header选择format的方式 + */ +@Slf4j +@RequiredArgsConstructor +public class CallerAppInterceptor implements HandlerInterceptor { + public static final String HTTP_REQUEST_HEADER = "axzo-caller-app"; + public static final String HTTP_REQUEST_ATTRIBUTE = "__att_caller_app"; + public static final String NEXT_HTTP_REQUEST_ATTRIBUTE = "__att_caller_app_for_next"; + + private final AppRuntime appRuntime; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String header = request.getHeader(HTTP_REQUEST_HEADER); + CallerApp callerApp = null; + if (!Strings.isNullOrEmpty(header)) { + callerApp = JSONObject.parseObject(header, CallerApp.class); + request.setAttribute(HTTP_REQUEST_ATTRIBUTE, callerApp); + } + request.setAttribute(NEXT_HTTP_REQUEST_ATTRIBUTE, CallerApp.builder() + .appEnv(appRuntime.getEnv()) + .appName(appRuntime.getAppName()) + .previous(callerApp) + .build()); + return true; + } + +} diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/resolvers/CallerAppResolver.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/resolvers/CallerAppResolver.java new file mode 100644 index 0000000..6f8d5a9 --- /dev/null +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/resolvers/CallerAppResolver.java @@ -0,0 +1,25 @@ +package cn.axzo.foundation.web.support.resolvers; + +import cn.axzo.foundation.caller.CallerApp; +import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor; +import org.springframework.core.MethodParameter; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; + +public class CallerAppResolver implements HandlerMethodArgumentResolver { + @Override + public boolean supportsParameter(MethodParameter methodParameter) { + return methodParameter.getParameterType().isAssignableFrom(CallerApp.class); + } + + @Override + public Object resolveArgument(MethodParameter parameter, + ModelAndViewContainer mavContainer, + NativeWebRequest webRequest, + WebDataBinderFactory binderFactory) throws Exception { + return webRequest.getAttribute(CallerAppInterceptor.HTTP_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); + } +} diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClient.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClient.java index d2dda46..a22b3e2 100644 --- a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClient.java +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClient.java @@ -2,11 +2,14 @@ package cn.axzo.foundation.web.support.rpc; import cn.axzo.foundation.page.PageResp; import cn.axzo.foundation.result.ApiResult; +import cn.axzo.foundation.web.support.context.AxContext; +import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import lombok.extern.slf4j.Slf4j; @@ -53,12 +56,6 @@ public interface RpcClient { return post(url, content, typeReference, Boolean.TRUE); } - @Deprecated - default T post(String url, Object content, Class clz) { - return post(url, content, new TypeReference>(clz) { - }); - } - /* get 相关方法 */ default T get(String url, TypeReference> typeReference, RequestParams requestParams) { return execute(HttpClient.HttpMethod.GET, url, requestParams, s -> JSONObject.parseObject(s, typeReference)); @@ -118,7 +115,14 @@ public interface RpcClient { // 将axzo-开头的header复制到请求的下一跳 String AZXO_HEADER_PREFIX = "axzo-"; - List>> DEFAULT_HEADER_SUPPLIERS = ImmutableList.of(); + List>> DEFAULT_HEADER_SUPPLIERS = ImmutableList.of( + //设置callerApp + () -> AxContext.getRequest().map(e -> { + Object caller = e.getAttribute(CallerAppInterceptor.NEXT_HTTP_REQUEST_ATTRIBUTE); + return ImmutableMap.of(CallerAppInterceptor.HTTP_REQUEST_HEADER, JSONObject.toJSONString(caller)); + }).orElse(ImmutableMap.of()) + ); + /** * 使用builder模式来发起请求, 先通过request()获得builder对象. 再构建具体的请求方式