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 index b371cde1..73527ce4 100644 --- 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 @@ -1,6 +1,22 @@ package com.gitee.sop.servercommon.bean; +import org.apache.commons.lang3.time.DateUtils; + +import java.text.ParseException; import java.util.Date; +import java.util.Map; + +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; /** * 获取开放平台请求参数。 @@ -10,67 +26,108 @@ import java.util.Date; public interface OpenContext { /** - * 返回appid + * 返回所有的请求参数 * - * @return 返回appid + * @return 返回所有的请求参数 */ - String getAppId(); + Map getParameterMap(); /** - * 返回业务参数json字符串 + * 返回业务对象 * - * @return 返回字符串业务参数 + * @return 业务对象 */ - String getBizContent(); + Object getBizObject(); + /** - * 返回业务对象 + * 获取某个参数值 * - * @return 业务对象 + * @param name 参数名称 + * @return 没有返回null */ - Object getBizObject(); + default String getParameter(String name) { + Object value = getParameterMap().get(name); + return value == null ? null : value.toString(); + } + + /** + * 返回appid + * + * @return 返回appId + */ + default String getAppId() { + return getParameter(APP_KEY_NAME); + } + + /** + * 返回业务参数json字符串 + * + * @return 返回字符串业务参数 + */ + default String getBizContent() { + return getParameter(BIZ_CONTENT_NAME); + } + /** * 返回字符编码 * * @return 如UTF-8 */ - String getCharset(); + default String getCharset() { + return getParameter(CHARSET_NAME); + } /** * 返回接口名 * * @return 如:alipay.goods.get */ - String getMethod(); + default String getMethod() { + return getParameter(API_NAME); + } /** * 返回版本号 * * @return 如:1.0 */ - String getVersion(); + default String getVersion() { + return getParameter(VERSION_NAME); + } /** * 返回参数格式化 * * @return 如:json */ - String getFormat(); + default String getFormat() { + return getParameter(FORMAT_NAME); + } /** * 返回签名类型 * * @return 如:RSA2 */ - String getSignType(); + default String getSignType() { + return getParameter(SIGN_TYPE_NAME); + } /** * 返回时间戳 * * @return */ - Date getTimestamp(); + default Date getTimestamp() { + String timestampStr = getParameter(TIMESTAMP_NAME); + try { + return DateUtils.parseDate(timestampStr, TIMESTAMP_PATTERN); + } catch (ParseException e) { + return null; + } + } /** @@ -78,13 +135,17 @@ public interface OpenContext { * * @return 返回token */ - String getAppAuthToken(); + default String getAppAuthToken() { + return getParameter(APP_AUTH_TOKEN_NAME); + } /** * 返回回调地址 * * @return 返回回调地址 */ - String getNotifyUrl(); + default String getNotifyUrl() { + return getParameter(NOTIFY_URL_NAME); + } } 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 index 344286b1..e272eb18 100644 --- 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 @@ -1,31 +1,15 @@ 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; +import java.util.Map; /** * @author tanghc */ public class OpenContextImpl implements OpenContext { - private JSONObject requestParams; + private final Map requestParams; private Object bizObject; - public OpenContextImpl(JSONObject requestParams) { + public OpenContextImpl(Map requestParams) { this.requestParams = requestParams; } @@ -34,8 +18,8 @@ public class OpenContextImpl implements OpenContext { } @Override - public String getAppId() { - return requestParams.getString(APP_KEY_NAME); + public Map getParameterMap() { + return requestParams; } @Override @@ -43,57 +27,6 @@ public class OpenContextImpl implements OpenContext { 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();