From faa9310759500bdc0a659312ac4743bfb22f1c24 Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 25 Sep 2020 13:35:37 +0800 Subject: [PATCH] 4.0.3 --- doc/docs/files/90100_常见问题.md | 4 +- .../servercommon/bean/OpenBeanFactory.java | 16 --- .../sop/servercommon/bean/OpenContext.java | 90 ++++++++++++++++ .../servercommon/bean/OpenContextImpl.java | 101 ++++++++++++++++++ .../sop/servercommon/bean/ServiceContext.java | 13 ++- .../param/ApiArgumentResolver.java | 16 ++- .../Example1001_BaseController.java | 15 +++ .../java/com/gitee/sop/test/AllInOneTest.java | 11 ++ 8 files changed, 246 insertions(+), 20 deletions(-) delete mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenBeanFactory.java create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md index 7ab7489a..0995c8a4 100644 --- a/doc/docs/files/90100_常见问题.md +++ b/doc/docs/files/90100_常见问题.md @@ -10,7 +10,9 @@ ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); ## 微服务端如何获取appId等参数 ```java -String app_id = request.getParameter("app_id"); +OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); +System.out.println("app_id:" + openContext.getAppId()); +System.out.println("token:" + openContext.getAppAuthToken()); ``` diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenBeanFactory.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenBeanFactory.java deleted file mode 100644 index 4e05abd9..00000000 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenBeanFactory.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.gitee.sop.servercommon.bean; - -/** - * @author tanghc - */ -public interface OpenBeanFactory { - - /** - * 返回业务参数对象 - * - * @param clazz 业务参数类class - * @param 业务参数对象 - * @return 返回业务参数对象 - */ - T getBizObject(Class clazz); -} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java new file mode 100644 index 00000000..b371cde1 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java @@ -0,0 +1,90 @@ +package com.gitee.sop.servercommon.bean; + +import java.util.Date; + +/** + * 获取开放平台请求参数。 + * + * @author tanghc + */ +public interface OpenContext { + + /** + * 返回appid + * + * @return 返回appid + */ + String getAppId(); + + /** + * 返回业务参数json字符串 + * + * @return 返回字符串业务参数 + */ + String getBizContent(); + + /** + * 返回业务对象 + * + * @return 业务对象 + */ + Object getBizObject(); + + /** + * 返回字符编码 + * + * @return 如UTF-8 + */ + String getCharset(); + + /** + * 返回接口名 + * + * @return 如:alipay.goods.get + */ + String getMethod(); + + /** + * 返回版本号 + * + * @return 如:1.0 + */ + String getVersion(); + + /** + * 返回参数格式化 + * + * @return 如:json + */ + String getFormat(); + + /** + * 返回签名类型 + * + * @return 如:RSA2 + */ + String getSignType(); + + /** + * 返回时间戳 + * + * @return + */ + Date getTimestamp(); + + + /** + * 返回token + * + * @return 返回token + */ + String getAppAuthToken(); + + /** + * 返回回调地址 + * + * @return 返回回调地址 + */ + String getNotifyUrl(); + +} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java new file mode 100644 index 00000000..344286b1 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -0,0 +1,101 @@ +package com.gitee.sop.servercommon.bean; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.time.DateUtils; + +import java.text.ParseException; +import java.util.Date; + +import static com.gitee.sop.servercommon.bean.ParamNames.API_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.APP_AUTH_TOKEN_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.APP_KEY_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.BIZ_CONTENT_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.CHARSET_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.FORMAT_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.NOTIFY_URL_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.SIGN_TYPE_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_PATTERN; +import static com.gitee.sop.servercommon.bean.ParamNames.VERSION_NAME; + +/** + * @author tanghc + */ +public class OpenContextImpl implements OpenContext { + private JSONObject requestParams; + private Object bizObject; + + public OpenContextImpl(JSONObject requestParams) { + this.requestParams = requestParams; + } + + public void setBizObject(Object bizObject) { + this.bizObject = bizObject; + } + + @Override + public String getAppId() { + return requestParams.getString(APP_KEY_NAME); + } + + @Override + public Object getBizObject() { + return bizObject; + } + + @Override + public String getBizContent() { + return requestParams.getString(BIZ_CONTENT_NAME); + } + + @Override + public String getCharset() { + return requestParams.getString(CHARSET_NAME); + } + + @Override + public String getMethod() { + return requestParams.getString(API_NAME); + } + + @Override + public String getVersion() { + return requestParams.getString(VERSION_NAME); + } + + @Override + public String getFormat() { + return requestParams.getString(FORMAT_NAME); + } + + @Override + public String getSignType() { + return requestParams.getString(SIGN_TYPE_NAME); + } + + @Override + public Date getTimestamp() { + String timestampStr = requestParams.getString(TIMESTAMP_NAME); + try { + return DateUtils.parseDate(timestampStr, TIMESTAMP_PATTERN); + } catch (ParseException e) { + return null; + } + } + + @Override + public String getAppAuthToken() { + return requestParams.getString(APP_AUTH_TOKEN_NAME); + } + + @Override + public String getNotifyUrl() { + return requestParams.getString(NOTIFY_URL_NAME); + } + + + @Override + public String toString() { + return requestParams.toString(); + } +} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java index 0ccca740..749a9142 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java @@ -10,8 +10,9 @@ import java.util.concurrent.ConcurrentHashMap; */ public class ServiceContext extends ConcurrentHashMap { - public static final String REQUEST_KEY = "request"; - public static final String RESPONSE_KEY = "response"; + public static final String REQUEST_KEY = "sop-request"; + public static final String RESPONSE_KEY = "sop-response"; + public static final String OPEN_CONTEXT_KEY = "sop-open-context"; protected static Class contextClass = ServiceContext.class; @@ -31,6 +32,14 @@ public class ServiceContext extends ConcurrentHashMap { super(); } + public void setOpenContext(OpenContext openContext) { + set(OPEN_CONTEXT_KEY, openContext); + } + + public OpenContext getOpenContext() { + return (OpenContext) get(OPEN_CONTEXT_KEY); + } + public Locale getLocale() { HttpServletRequest request = getRequest(); if (request == null) { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java index 71052dbd..bfc4e264 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java @@ -4,7 +4,11 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONValidator; import com.gitee.sop.servercommon.annotation.Open; +import com.gitee.sop.servercommon.bean.OpenContext; +import com.gitee.sop.servercommon.bean.OpenContextImpl; import com.gitee.sop.servercommon.bean.ParamNames; +import com.gitee.sop.servercommon.bean.ServiceConfig; +import com.gitee.sop.servercommon.bean.ServiceContext; import com.gitee.sop.servercommon.util.OpenUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.core.MethodParameter; @@ -133,12 +137,18 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { } HandlerMethodArgumentResolver resolver = getOtherArgumentResolver(methodParameter); if (resolver != null) { - return resolver.resolveArgument( + Object param = resolver.resolveArgument( methodParameter , modelAndViewContainer , nativeWebRequest , webDataBinderFactory ); + OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); + if (openContext instanceof OpenContextImpl) { + OpenContextImpl openContextImpl = (OpenContextImpl) openContext; + openContextImpl.setBizObject(param); + } + return param; } return null; } @@ -153,7 +163,10 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { */ protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); + ServiceContext currentContext = ServiceContext.getCurrentContext(); JSONObject requestParams = OpenUtil.getRequestParams(request); + OpenContextImpl openContext = new OpenContextImpl(requestParams); + currentContext.setOpenContext(openContext); String bizContent = requestParams.getString(ParamNames.BIZ_CONTENT_NAME); if (bizContent == null) { return null; @@ -169,6 +182,7 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { Map query = OpenUtil.parseQueryToMap(bizContent); param = new JSONObject(query).toJavaObject(parameterType); } + openContext.setBizObject(param); this.bindUploadFile(param, request); return param; } diff --git a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java index 31452397..f2401ecb 100644 --- a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java +++ b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.gitee.sop.servercommon.annotation.BizCode; import com.gitee.sop.servercommon.annotation.Open; +import com.gitee.sop.servercommon.bean.OpenContext; +import com.gitee.sop.servercommon.bean.ServiceContext; import com.gitee.sop.servercommon.exception.ServiceException; import com.gitee.sop.storyweb.controller.param.CategoryParam; import com.gitee.sop.storyweb.controller.param.LargeTextParam; @@ -16,6 +18,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -81,6 +84,18 @@ public class Example1001_BaseController { return story; } + @Open(value = "story.system.param.get") + @GetMapping("/get/system/param/v1") + public StoryResult systemParam(StoryParam param) { + StoryResult result = new StoryResult(); + OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); + System.out.println(param == openContext.getBizObject()); + System.out.println("app_id:" + openContext.getAppId()); + System.out.println("token:" + openContext.getAppAuthToken()); + result.setName("系统参数:" + openContext); + return result; + } + // 参数绑定 @Open(value = "story.param.bind", mergeResult = false) @RequestMapping("/get/param/v1") diff --git a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java index aa03b55c..8f171a8c 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java @@ -55,6 +55,17 @@ public class AllInOneTest extends TestBase { client.execute(requestBuilder); } + public void testSystemParam() { + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("story.system.param.get") + .version("1.0") + .appAuthToken("123123") + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); + } + /** * 以json方式提交(application/json) */