Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dm_project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姚本章
dm_project
Commits
d6402b13
Commit
d6402b13
authored
Feb 25, 2021
by
zhangc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化API调用代码
parent
196e03a6
Changes
26
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
1025 additions
and
655 deletions
+1025
-655
api_gateway.sql
database/zc/api_gateway.sql
+1
-0
pom.xml
jz-dm-apigateway/pom.xml
+8
-1
GatewayResultCode.java
...c/main/java/com/jz/dm/common/enums/GatewayResultCode.java
+8
-5
ResultFlowCode.java
.../src/main/java/com/jz/dm/common/enums/ResultFlowCode.java
+59
-0
LimitFlowException.java
...n/java/com/jz/dm/common/exception/LimitFlowException.java
+63
-0
MapUtil.java
...igateway/src/main/java/com/jz/dm/common/util/MapUtil.java
+10
-33
GatewayUrlBlockHandler.java
...rc/main/java/com/jz/dm/config/GatewayUrlBlockHandler.java
+47
-0
AuthFilter.java
...apigateway/src/main/java/com/jz/dm/filter/AuthFilter.java
+2
-2
CheckArgsFilter.java
...teway/src/main/java/com/jz/dm/filter/CheckArgsFilter.java
+0
-1
VerifySignFilter.java
...eway/src/main/java/com/jz/dm/filter/VerifySignFilter.java
+19
-3
ApiFunctionMapper.java
...way/src/main/java/com/jz/dm/mapper/ApiFunctionMapper.java
+10
-0
ApiInterface.java
...y/src/main/java/com/jz/dm/models/domian/ApiInterface.java
+6
-0
DataTableSelectTO.java
...c/main/java/com/jz/dm/models/enity/DataTableSelectTO.java
+49
-0
ApiQueryService.java
.../main/java/com/jz/dm/service/request/ApiQueryService.java
+149
-118
DataBagService.java
...c/main/java/com/jz/dm/service/request/DataBagService.java
+22
-0
DataSelectService.java
...ain/java/com/jz/dm/service/request/DataSelectService.java
+21
-0
ThirdSelectService.java
...in/java/com/jz/dm/service/request/ThirdSelectService.java
+22
-0
DataBagHandle.java
...in/java/com/jz/dm/service/request/impl/DataBagHandle.java
+54
-0
DataTableSelectHandle.java
...com/jz/dm/service/request/impl/DataTableSelectHandle.java
+209
-0
ThirdSelectHandle.java
...ava/com/jz/dm/service/request/impl/ThirdSelectHandle.java
+171
-0
ApiReqTest.java
...teway/src/test/java/com/jz/dm/gateway/api/ApiReqTest.java
+70
-11
TestHttpReq.java
...y/src/test/java/com/jz/dm/gateway/orther/TestHttpReq.java
+1
-1
TestMakeApi.java
...y/src/test/java/com/jz/dm/gateway/orther/TestMakeApi.java
+1
-1
ResultCode.java
...mmon/src/main/java/com/jz/common/constant/ResultCode.java
+6
-6
ResultMsg.java
...ommon/src/main/java/com/jz/common/constant/ResultMsg.java
+13
-468
HttpsUtils.java
...-common/src/main/java/com/jz/common/utils/HttpsUtils.java
+4
-5
No files found.
database/zc/api_gateway.sql
View file @
d6402b13
...
@@ -8,6 +8,7 @@ CREATE TABLE `t_api_interface` (
...
@@ -8,6 +8,7 @@ CREATE TABLE `t_api_interface` (
`target_url`
varchar
(
100
)
DEFAULT
NULL
COMMENT
'目标url'
,
`target_url`
varchar
(
100
)
DEFAULT
NULL
COMMENT
'目标url'
,
`api_type`
varchar
(
50
)
NULL
DEFAULT
''
COMMENT
'接入类型:字典表对应key值'
,
`api_type`
varchar
(
50
)
NULL
DEFAULT
''
COMMENT
'接入类型:字典表对应key值'
,
`req_type`
varchar
(
20
)
NULL
DEFAULT
NULL
COMMENT
'请求方式: GET, POST'
,
`req_type`
varchar
(
20
)
NULL
DEFAULT
NULL
COMMENT
'请求方式: GET, POST'
,
`post_type`
varchar
(
20
)
NULL
DEFAULT
NULL
COMMENT
'post请求类型:1.x-www-form-urlencoded 2.form-data 3.json'
,
`api_function`
varchar
(
200
)
DEFAULT
NULL
COMMENT
'接口功能'
,
`api_function`
varchar
(
200
)
DEFAULT
NULL
COMMENT
'接口功能'
,
`status`
varchar
(
50
)
NOT
NULL
COMMENT
'状态(DRAFT-草稿 ISSUE-发布 OUTMODED-过时 4FREEZE-冻结 EXPIRY-失效)'
,
`status`
varchar
(
50
)
NOT
NULL
COMMENT
'状态(DRAFT-草稿 ISSUE-发布 OUTMODED-过时 4FREEZE-冻结 EXPIRY-失效)'
,
`output_type`
varchar
(
50
)
NULL
DEFAULT
'JSON'
COMMENT
'输出类型:flow 流形式输出, json格式输出'
,
`output_type`
varchar
(
50
)
NULL
DEFAULT
'JSON'
COMMENT
'输出类型:flow 流形式输出, json格式输出'
,
...
...
jz-dm-apigateway/pom.xml
View file @
d6402b13
...
@@ -117,7 +117,12 @@
...
@@ -117,7 +117,12 @@
<groupId>
com.alibaba.csp
</groupId>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-core
</artifactId>
<artifactId>
sentinel-core
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-spring-webmvc-adapter
</artifactId>
<version>
1.8.0
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<dependency>
<groupId>
net.sf.json-lib
</groupId>
<groupId>
net.sf.json-lib
</groupId>
<artifactId>
json-lib
</artifactId>
<artifactId>
json-lib
</artifactId>
...
@@ -149,6 +154,8 @@
...
@@ -149,6 +154,8 @@
<artifactId>
velocity-engine-core
</artifactId>
<artifactId>
velocity-engine-core
</artifactId>
<version>
2.0
</version>
<version>
2.0
</version>
</dependency>
</dependency>
<!-- 代码自动生成依赖 end-->
<!-- 代码自动生成依赖 end-->
</dependencies>
</dependencies>
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/common/enums/GatewayResultCode.java
View file @
d6402b13
...
@@ -36,6 +36,9 @@ public enum GatewayResultCode implements ResultCode {
...
@@ -36,6 +36,9 @@ public enum GatewayResultCode implements ResultCode {
/** 无效请求 */
/** 无效请求 */
ILLEGAL_TIMETEMP
(
"ILLEGAL_TIMETEMP"
,
"无效时间戳"
),
ILLEGAL_TIMETEMP
(
"ILLEGAL_TIMETEMP"
,
"无效时间戳"
),
/** 无效授权 */
ILLEGAL_AUTHORIZATION
(
"ILLEGAL_AUTHORIZATION"
,
"无效授权"
),
/** 请求次数受限 */
/** 请求次数受限 */
REQUEST_LIMIT_EXCEPTION
(
"REQUEST_LIMIT_EXCEPTION"
,
"请求次数受限"
),
REQUEST_LIMIT_EXCEPTION
(
"REQUEST_LIMIT_EXCEPTION"
,
"请求次数受限"
),
...
@@ -53,8 +56,8 @@ public enum GatewayResultCode implements ResultCode {
...
@@ -53,8 +56,8 @@ public enum GatewayResultCode implements ResultCode {
/** 请求未授权 */
/** 请求未授权 */
REQUEST_NOT_AUTH
(
"REQUEST_NOT_AUTH"
,
"请求未授权"
),
REQUEST_NOT_AUTH
(
"REQUEST_NOT_AUTH"
,
"请求未授权"
),
/**
请求
组织不存在 */
/** 组织不存在 */
ORG_NOT_EXIST
(
"ORG_NOT_EXIST"
,
"
请求
组织不存在"
),
ORG_NOT_EXIST
(
"ORG_NOT_EXIST"
,
"组织不存在"
),
/** API状态异常 */
/** API状态异常 */
API_STATUS_EXCEPTION
(
"API_STATUS_EXCEPTION"
,
"API状态异常"
),
API_STATUS_EXCEPTION
(
"API_STATUS_EXCEPTION"
,
"API状态异常"
),
...
@@ -65,12 +68,12 @@ public enum GatewayResultCode implements ResultCode {
...
@@ -65,12 +68,12 @@ public enum GatewayResultCode implements ResultCode {
/** 文件地址不存在 */
/** 文件地址不存在 */
DATA_BIG_ADDR_UNEXIST
(
"DATA_BIG_ADDR_UNEXIST"
,
"文件地址不存在!"
),
DATA_BIG_ADDR_UNEXIST
(
"DATA_BIG_ADDR_UNEXIST"
,
"文件地址不存在!"
),
/**
请求
组织状态异常 */
/** 组织状态异常 */
ORG_STATE_EXCEPTION
(
"ORG_STATE_EXCEPTION"
,
"
请求
组织状态异常"
),
ORG_STATE_EXCEPTION
(
"ORG_STATE_EXCEPTION"
,
"组织状态异常"
),
/** 请求信息不存在 */
/** 请求信息不存在 */
REQUEST_INFO_UNEXIST
(
"REQUEST_INFO_UNEXIST"
,
"请求信息不存在"
),
REQUEST_INFO_UNEXIST
(
"REQUEST_INFO_UNEXIST"
,
"请求信息不存在"
),
/** 远程请求异常 */
/** 远程请求异常 */
DISTANCE_REQUEST_EXCEPTION
(
"DISTANCE_REQUEST_EXCEPTION"
,
"
流形式输出
异常"
),
DISTANCE_REQUEST_EXCEPTION
(
"DISTANCE_REQUEST_EXCEPTION"
,
"
远程请求
异常"
),
/** 单次调用金额不足 */
/** 单次调用金额不足 */
CALL_AMOUNT_NOT_ENOUGH
(
"CALL_AMOUNT_NOT_ENOUGH"
,
"单次调用金额不足"
),
CALL_AMOUNT_NOT_ENOUGH
(
"CALL_AMOUNT_NOT_ENOUGH"
,
"单次调用金额不足"
),
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/common/enums/ResultFlowCode.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
common
.
enums
;
import
com.alibaba.fastjson.JSON
;
import
com.jz.common.constant.ResultMsg
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.enums
* @PROJECT_NAME: jz-dm-parent
* @NAME: ResultFlowCode
* @DATE: 2021-2-25/14:40
* @DAY_NAME_SHORT: 周四
* @Description:
**/
public
enum
ResultFlowCode
{
/** 限流异常 */
FLOW_EXCEPTION
(
410
,
ResultMsg
.
LIMIT_FLOW_EXCEPTION
),
/** 降级异常 */
DEGRADE_EXCEPTION
(
411
,
ResultMsg
.
LIMIT_DEGRADE_EXCEPTION
),
/** 热点异常 */
PARAM_EXCEPTION
(
412
,
ResultMsg
.
LIMIT_PARAM_EXCEPTION
),
/** 系统规则异常 */
SYSTEM_EXCEPTION
(
413
,
ResultMsg
.
LIMIT_SYSTEM_EXCEPTION
),
/** 认证异常 */
AUTHORITY_EXCEPTION
(
414
,
ResultMsg
.
LIMIT_AUTHORITY_EXCEPTION
);
private
int
code
;
/**
* 提示信息
*/
private
ResultMsg
msg
;
private
ResultFlowCode
(
int
code
,
ResultMsg
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
@Override
public
String
toString
()
{
return
JSON
.
toJSONString
(
this
);
}
public
int
getCode
()
{
return
code
;
}
public
ResultMsg
getMsg
()
{
return
this
.
msg
;
}
public
void
setMsg
(
ResultMsg
msg
)
{
this
.
msg
=
msg
;
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/common/exception/LimitFlowException.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
common
.
exception
;
import
com.jz.common.constant.ResultMsg
;
import
com.jz.dm.common.enums.ResultFlowCode
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.exception
* @PROJECT_NAME: jz-dm-parent
* @NAME: LimitFlowException
* @DATE: 2021-2-25/14:27
* @DAY_NAME_SHORT: 周四
* @Description: 限流异常处理器
**/
@Data
@EqualsAndHashCode
(
callSuper
=
true
)
public
class
LimitFlowException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
-
3963657380514719229L
;
/**
* 错误代码
*/
protected
ResultFlowCode
code
;
/**
* 错误信息
*/
protected
String
message
;
private
Throwable
cause
;
public
static
LimitFlowException
of
(
ResultMsg
errorMsg
)
{
return
LimitFlowException
.
of
(
ResultFlowCode
.
FLOW_EXCEPTION
,
errorMsg
.
getMsg
());
}
public
static
LimitFlowException
of_error
(
String
errorMsg
)
{
return
LimitFlowException
.
of
(
ResultFlowCode
.
FLOW_EXCEPTION
,
errorMsg
);
}
public
static
LimitFlowException
of
(
ResultFlowCode
code
)
{
LimitFlowException
ufaceException
=
new
LimitFlowException
();
ufaceException
.
setCode
(
code
);
ufaceException
.
setMessage
(
code
.
getMsg
().
getMsg
());
return
ufaceException
;
}
public
static
LimitFlowException
of
(
ResultFlowCode
code
,
String
message
)
{
LimitFlowException
ufaceException
=
new
LimitFlowException
();
ufaceException
.
setCode
(
code
);
ufaceException
.
setMessage
(
message
);
return
ufaceException
;
}
public
static
LimitFlowException
of
(
ResultFlowCode
code
,
Throwable
cause
)
{
LimitFlowException
ufaceException
=
new
LimitFlowException
();
ufaceException
.
setCode
(
code
);
ufaceException
.
setCause
(
cause
);
ufaceException
.
setMessage
(
"todo need get from db/properties"
);
return
ufaceException
;
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/common/util/MapUtil.java
View file @
d6402b13
package
com
.
jz
.
dm
.
common
.
util
;
package
com
.
jz
.
dm
.
common
.
util
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.jz.dm.common.constant.LoggingConstants
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
...
@@ -29,36 +28,15 @@ public class MapUtil {
...
@@ -29,36 +28,15 @@ public class MapUtil {
* @param signType
* @param signType
* @return
* @return
*/
*/
public
static
String
getSignValue
(
String
apiKey
,
String
method
,
String
signType
)
{
//public static String getSignValue(String apiKey, String method, String signType) {
StringBuilder
builder
=
new
StringBuilder
();
// StringBuilder builder = new StringBuilder();
builder
.
append
(
"apiKey="
).
append
(
apiKey
).
append
(
LoggingConstants
.
AND_SPILT
)
// builder.append("apiKey=").append(apiKey).append(LoggingConstants.AND_SPILT)
.
append
(
"method="
).
append
(
method
).
append
(
LoggingConstants
.
AND_SPILT
)
// .append("method=").append(method).append(LoggingConstants.AND_SPILT)
.
append
(
"signType="
).
append
(
signType
);
// .append("signType=").append(signType);
return
builder
.
toString
();
// return builder.toString();
}
//}
/**
* 给map集合中的key实现字典排序
*
* @param map
* @return
*/
public
Map
<
String
,
Object
>
mapKeySort
(
Map
<
String
,
Object
>
map
)
{
ArrayList
list
=
new
ArrayList
();
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
list
.
add
(
entry
.
getKey
());
//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
HashMap
<
String
,
Object
>
newMap
=
new
HashMap
<
String
,
Object
>();
//运用Collections的sort()方法对其进行排序 sort()方法需要传 连个参数,一个是需要进行排序的Collection 另一个是一个Comparator
Collections
.
sort
(
list
,
new
SpellComparatorUtils
());
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
newMap
.
put
(
list
.
get
(
i
).
toString
(),
map
.
get
(
list
.
get
(
i
).
toString
()));
}
return
newMap
;
}
/**
/**
* 将对象转成TreeMap,属性名为key,属性值为value
* 将对象转成TreeMap,属性名为key,属性值为value
...
@@ -147,12 +125,11 @@ public class MapUtil {
...
@@ -147,12 +125,11 @@ public class MapUtil {
}
}
/**
/**
* * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* * 把数组所有元素
字典
排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* * @param params 需要排序并参与字符拼接的参数组
* * @param params 需要排序并参与字符拼接的参数组
* * @return 拼接后字符串
* * @return 拼接后字符串
*
*/
*/
public
static
String
createLinkStringByGe
t
(
Map
<
String
,
String
>
params
)
{
public
static
String
stringNormalSor
t
(
Map
<
String
,
String
>
params
)
{
List
<
String
>
keys
=
new
ArrayList
<
String
>(
params
.
keySet
());
List
<
String
>
keys
=
new
ArrayList
<
String
>(
params
.
keySet
());
Collections
.
sort
(
keys
);
Collections
.
sort
(
keys
);
String
prestr
=
""
;
String
prestr
=
""
;
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/config/GatewayUrlBlockHandler.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
config
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.authority.AuthorityException
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeException
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowException
;
import
com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException
;
import
com.alibaba.csp.sentinel.slots.system.SystemBlockException
;
import
com.jz.dm.common.enums.ResultFlowCode
;
import
com.jz.dm.common.exception.LimitFlowException
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.config
* @PROJECT_NAME: jz-dm-parent
* @NAME: GatewayUrlBlockHandler
* @DATE: 2021-2-25/14:16
* @DAY_NAME_SHORT: 周四
* @Description: sentinel限流异常处理类
**/
@Component
public
class
GatewayUrlBlockHandler
implements
BlockExceptionHandler
{
@Override
public
void
handle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
BlockException
e
)
throws
Exception
{
if
(
e
instanceof
FlowException
){
throw
LimitFlowException
.
of
(
ResultFlowCode
.
FLOW_EXCEPTION
);
}
else
if
(
e
instanceof
DegradeException
){
throw
LimitFlowException
.
of
(
ResultFlowCode
.
DEGRADE_EXCEPTION
);
}
else
if
(
e
instanceof
ParamFlowException
){
throw
LimitFlowException
.
of
(
ResultFlowCode
.
PARAM_EXCEPTION
);
}
else
if
(
e
instanceof
SystemBlockException
){
throw
LimitFlowException
.
of
(
ResultFlowCode
.
SYSTEM_EXCEPTION
);
}
else
if
(
e
instanceof
AuthorityException
){
throw
LimitFlowException
.
of
(
ResultFlowCode
.
AUTHORITY_EXCEPTION
);
}
/*// 设置返回json数据
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("Content-Type","application/json;charset=UTF-8");
httpServletResponse.getWriter().write(JSON.toJSONString(backMap));*/
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/filter/AuthFilter.java
View file @
d6402b13
...
@@ -101,7 +101,7 @@ public class AuthFilter extends AbstractFilter {
...
@@ -101,7 +101,7 @@ public class AuthFilter extends AbstractFilter {
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
}
}
if
(
"1"
.
equals
(
authAuth
.
getHandler
()))
{
//处理状态,已经调用成功后授权码失效
if
(
"1"
.
equals
(
authAuth
.
getHandler
()))
{
//处理状态,已经调用成功后授权码失效
throw
new
GatewayException
(
GatewayResultCode
.
ILLEGAL_
REQUEST
);
throw
new
GatewayException
(
GatewayResultCode
.
ILLEGAL_
AUTHORIZATION
);
}
}
//查询认证组织信息
//查询认证组织信息
ApiOrg
apiOrg
=
organizationManageService
.
getAuthOrganization
(
authAuth
.
getApiOrgId
());
ApiOrg
apiOrg
=
organizationManageService
.
getAuthOrganization
(
authAuth
.
getApiOrgId
());
...
@@ -231,7 +231,7 @@ public class AuthFilter extends AbstractFilter {
...
@@ -231,7 +231,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject
result
=
JSONObject
.
parseObject
(
respResult
);
JSONObject
result
=
JSONObject
.
parseObject
(
respResult
);
if
(
null
!=
result
)
{
if
(
null
!=
result
)
{
if
(
200
!=
result
.
getInteger
(
"code"
))
{
if
(
200
!=
result
.
getInteger
(
"code"
))
{
log
.
info
(
"~~~~~~~~~~~~~~~~~~
异常信息为
:{}"
,
result
.
getString
(
"message"
));
log
.
info
(
"~~~~~~~~~~~~~~~~~~
远程调用异常
:{}"
,
result
.
getString
(
"message"
));
throw
new
GatewayException
(
GatewayResultCode
.
CALL_AMOUNT_NOT_ENOUGH
);
throw
new
GatewayException
(
GatewayResultCode
.
CALL_AMOUNT_NOT_ENOUGH
);
}
}
}
}
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/filter/CheckArgsFilter.java
View file @
d6402b13
...
@@ -37,7 +37,6 @@ public class CheckArgsFilter extends AbstractFilter {
...
@@ -37,7 +37,6 @@ public class CheckArgsFilter extends AbstractFilter {
||
StringUtil
.
isEmpty
(
request
.
getTimestamp
())
||
StringUtil
.
isEmpty
(
request
.
getParams
()))
{
||
StringUtil
.
isEmpty
(
request
.
getTimestamp
())
||
StringUtil
.
isEmpty
(
request
.
getParams
()))
{
throw
new
GatewayException
(
GatewayResultCode
.
ILLEGAL_ARGUMENT
);
//无效参数
throw
new
GatewayException
(
GatewayResultCode
.
ILLEGAL_ARGUMENT
);
//无效参数
}
}
// 设置默认值
if
(
StringUtil
.
isEmpty
(
request
.
getFormat
()))
{
if
(
StringUtil
.
isEmpty
(
request
.
getFormat
()))
{
request
.
setFormat
(
Format
.
JSON
.
name
());
request
.
setFormat
(
Format
.
JSON
.
name
());
}
}
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/filter/VerifySignFilter.java
View file @
d6402b13
...
@@ -17,6 +17,9 @@ import org.slf4j.LoggerFactory;
...
@@ -17,6 +17,9 @@ import org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
/**
* @Description: 验签过滤器(验证签名信息)
* @Description: 验签过滤器(验证签名信息)
* @Author: Mr.zhang
* @Author: Mr.zhang
...
@@ -45,7 +48,7 @@ public class VerifySignFilter extends AbstractFilter {
...
@@ -45,7 +48,7 @@ public class VerifySignFilter extends AbstractFilter {
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
request
.
getParams
());
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
request
.
getParams
());
if
(!
jsonObject
.
getBoolean
(
"isTest"
))
{
//是否测试调用
if
(!
jsonObject
.
getBoolean
(
"isTest"
))
{
//是否测试调用
//对签约参数进行字典排序
//对签约参数进行字典排序
String
signParams
=
MapUtil
.
getSignValue
(
request
.
getApiKey
(),
request
.
getMethod
(),
request
.
getSignType
(
));
String
signParams
=
MapUtil
.
stringNormalSort
(
assembleSignMap
(
request
));
if
(
StringUtils
.
isNotBlank
(
signParams
))
{
if
(
StringUtils
.
isNotBlank
(
signParams
))
{
String
authCode
=
jsonObject
.
getString
(
"authCode"
);
String
authCode
=
jsonObject
.
getString
(
"authCode"
);
//需要传入授权码
//需要传入授权码
...
@@ -54,7 +57,7 @@ public class VerifySignFilter extends AbstractFilter {
...
@@ -54,7 +57,7 @@ public class VerifySignFilter extends AbstractFilter {
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_NOT_AUTH
);
}
}
String
sign
=
Md5
.
encrypt
(
signParams
,
apiAuthInfo
.
getSalt
());
String
sign
=
Md5
.
encrypt
(
signParams
,
apiAuthInfo
.
getSalt
());
if
(!
request
.
getSign
().
equals
(
sign
))
{
if
(!
request
.
getSign
().
equals
IgnoreCase
(
sign
))
{
throw
new
GatewayException
(
GatewayResultCode
.
SIGN_ERROR
);
throw
new
GatewayException
(
GatewayResultCode
.
SIGN_ERROR
);
}
}
}
else
{
}
else
{
...
@@ -63,7 +66,7 @@ public class VerifySignFilter extends AbstractFilter {
...
@@ -63,7 +66,7 @@ public class VerifySignFilter extends AbstractFilter {
}
}
chain
.
doFilter
(
request
,
response
);
chain
.
doFilter
(
request
,
response
);
}
catch
(
GatewayException
ex
)
{
}
catch
(
GatewayException
ex
)
{
LogUtil
.
error
(
LOGGER
,
ex
,
"sign response error. response="
+
response
.
getResponse
());
LogUtil
.
error
(
LOGGER
,
ex
,
"sign response error. response="
+
response
.
getResponse
());
response
.
clearAttributes
();
response
.
clearAttributes
();
response
.
setCode
(
ex
.
getResultCode
().
getCode
());
response
.
setCode
(
ex
.
getResultCode
().
getCode
());
response
.
setMsg
(
ex
.
getResultCode
().
getMsg
());
response
.
setMsg
(
ex
.
getResultCode
().
getMsg
());
...
@@ -74,4 +77,17 @@ public class VerifySignFilter extends AbstractFilter {
...
@@ -74,4 +77,17 @@ public class VerifySignFilter extends AbstractFilter {
}
}
}
}
/**
* 组装签名参数
* @param request
* @return
*/
private
Map
assembleSignMap
(
GatewayRequest
request
)
{
Map
<
String
,
String
>
paramsMap
=
new
HashMap
<>();
paramsMap
.
put
(
"apiKey"
,
request
.
getApiKey
());
paramsMap
.
put
(
"method"
,
request
.
getMethod
());
paramsMap
.
put
(
"signType"
,
request
.
getSignType
());
paramsMap
.
put
(
"params"
,
request
.
getParams
());
return
paramsMap
;
}
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/mapper/ApiFunctionMapper.java
View file @
d6402b13
...
@@ -2,6 +2,9 @@ package com.jz.dm.mapper;
...
@@ -2,6 +2,9 @@ package com.jz.dm.mapper;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.jz.dm.models.domian.ApiFunction
;
import
com.jz.dm.models.domian.ApiFunction
;
import
org.apache.ibatis.annotations.Select
;
import
java.util.List
;
/**
/**
* <p>
* <p>
...
@@ -13,4 +16,11 @@ import com.jz.dm.models.domian.ApiFunction;
...
@@ -13,4 +16,11 @@ import com.jz.dm.models.domian.ApiFunction;
*/
*/
public
interface
ApiFunctionMapper
extends
BaseMapper
<
ApiFunction
>
{
public
interface
ApiFunctionMapper
extends
BaseMapper
<
ApiFunction
>
{
/**
* 查询function名称列表
* @return
*/
@Select
(
"SELECT `name` FROM t_api_function WHERE `status`=1 AND is_deleted=0"
)
List
<
String
>
listFunctionNames
();
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/models/domian/ApiInterface.java
View file @
d6402b13
...
@@ -60,6 +60,12 @@ public class ApiInterface extends BaseObject implements Serializable {
...
@@ -60,6 +60,12 @@ public class ApiInterface extends BaseObject implements Serializable {
@TableField
(
"req_type"
)
@TableField
(
"req_type"
)
private
String
reqType
;
private
String
reqType
;
/**
* post请求类型:1.x-www-form-urlencoded 2.form-data 3.json
*/
@TableField
(
"post_type"
)
private
String
postType
;
/**
/**
* '输出类型:flow 流形式输出, json格式输出',
* '输出类型:flow 流形式输出, json格式输出',
*/
*/
...
...
jz-dm-apigateway/src/main/java/com/jz/dm/models/enity/DataTableSelectTO.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
models
.
enity
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.models.domian.ApiInterface
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.io.Serializable
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.enity
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataTableSelectTO
* @DATE: 2021-2-24/10:10
* @DAY_NAME_SHORT: 周三
* @Description: 数据表查询-数据传输对象
**/
@Data
@ApiModel
(
"数据表查询领域对象"
)
public
class
DataTableSelectTO
implements
Serializable
{
@ApiModelProperty
(
value
=
"api对象"
)
private
ApiInterface
apiInterface
;
@ApiModelProperty
(
value
=
"业务请求参数"
)
private
String
reqParams
;
@ApiModelProperty
(
value
=
"业务返回体对象"
)
private
OpenApiResponse
openApiResponse
;
@ApiModelProperty
(
value
=
"授权码"
)
private
String
authCode
;
public
DataTableSelectTO
(
ApiInterface
apiInterface
,
String
reqParams
,
OpenApiResponse
openApiResponse
){
this
.
apiInterface
=
apiInterface
;
this
.
reqParams
=
reqParams
;
this
.
openApiResponse
=
openApiResponse
;
}
public
DataTableSelectTO
(
ApiInterface
apiInterface
,
String
reqParams
,
OpenApiResponse
openApiResponse
,
String
authCode
){
this
.
apiInterface
=
apiInterface
;
this
.
reqParams
=
reqParams
;
this
.
openApiResponse
=
openApiResponse
;
this
.
authCode
=
authCode
;
}
public
DataTableSelectTO
(){}
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/ApiQueryService.java
View file @
d6402b13
This diff is collapsed.
Click to expand it.
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/DataBagService.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request.impl
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataBagService
* @DATE: 2021-2-24/10:55
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public
interface
DataBagService
{
/**
* 数据包查询
* @param dataBagSelectTO
* @return
*/
public
boolean
dataBagSelect
(
DataTableSelectTO
dataBagSelectTO
);
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/DataSelectService.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataSelectService
* @DATE: 2021-2-24/10:36
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public
interface
DataSelectService
{
/**
* 数据查询处理类
* @param dataTableSelectTO
*/
public
Boolean
dataTableHandle
(
DataTableSelectTO
dataTableSelectTO
);
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/ThirdSelectService.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ThirdSelectService
* @DATE: 2021-2-24/10:43
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public
interface
ThirdSelectService
{
/**
* 三方查询处理
* @param dataTableSelectTO
* @return
*/
public
boolean
thirdSelectHandle
(
DataTableSelectTO
dataTableSelectTO
);
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/impl/DataBagHandle.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
.
impl
;
import
com.alibaba.fastjson.JSONObject
;
import
com.jz.dm.common.enums.GatewayResultCode
;
import
com.jz.dm.common.exception.GatewayException
;
import
com.jz.dm.common.util.stream.HttpDownload
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
import
com.jz.dm.service.ApiLogService
;
import
com.jz.dm.service.request.DataBagService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request.impl
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataBagHandle
* @DATE: 2021-2-24/10:56
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Slf4j
@Component
public
class
DataBagHandle
implements
DataBagService
{
@Autowired
private
ApiLogService
reqLogService
;
/**
* 数据包下载 $$ 数据银行+DMP
*
* @param dataBagSelectTO
* @return
*/
@Override
public
boolean
dataBagSelect
(
DataTableSelectTO
dataBagSelectTO
)
{
JSONObject
param
=
JSONObject
.
parseObject
(
dataBagSelectTO
.
getReqParams
());
String
fileLocation
=
param
.
getString
(
"file_location"
);
String
datasourceId
=
param
.
getString
(
"datasourceId"
);
if
(
null
==
fileLocation
||
null
==
datasourceId
)
{
throw
new
GatewayException
(
GatewayResultCode
.
DATA_BIG_ADDR_UNEXIST
);
}
JSONObject
requestParams
=
new
JSONObject
();
requestParams
.
put
(
"file_location"
,
fileLocation
);
requestParams
.
put
(
"datasourceId"
,
datasourceId
);
HttpDownload
.
postDownload
(
dataBagSelectTO
.
getApiInterface
().
getTargetUrl
(),
requestParams
);
reqLogService
.
updateLog
(
dataBagSelectTO
.
getApiInterface
().
getLogId
(),
null
);
dataBagSelectTO
.
getOpenApiResponse
().
setCode
(
GatewayResultCode
.
SUCCESS
.
getCode
());
dataBagSelectTO
.
getOpenApiResponse
().
setMsg
(
GatewayResultCode
.
SUCCESS
.
getMsg
());
return
true
;
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/impl/DataTableSelectHandle.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
.
impl
;
import
com.alibaba.fastjson.JSONObject
;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.dm.common.constant.LoggingConstants
;
import
com.jz.dm.common.enums.GatewayResultCode
;
import
com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum
;
import
com.jz.dm.common.exception.GatewayException
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.common.util.stream.HttpDownload
;
import
com.jz.dm.models.domian.ApiInterface
;
import
com.jz.dm.models.domian.ApiInterfaceCustom
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
import
com.jz.dm.service.ApiInterfaceService
;
import
com.jz.dm.service.ApiLogService
;
import
com.jz.dm.service.request.DataSelectService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataTableSelectHandle
* @DATE: 2021-2-24/10:06
* @DAY_NAME_SHORT: 周三
* @Description: 数据表查询处理类
**/
@Slf4j
@Component
public
class
DataTableSelectHandle
implements
DataSelectService
{
@Autowired
private
ApiInterfaceService
apiInterfaceService
;
@Autowired
private
ApiLogService
reqLogService
;
@Autowired
private
HttpsUtils
httpsUtils
;
/**
* 数据表查询处理
* @param dataTableSelectTO
*/
@Override
public
Boolean
dataTableHandle
(
DataTableSelectTO
dataTableSelectTO
)
{
JSONObject
jsonParams
=
JSONObject
.
parseObject
(
dataTableSelectTO
.
getReqParams
());
Map
param
=
JSONObject
.
parseObject
(
jsonParams
.
getString
(
"request_fileds"
),
Map
.
class
);
//校验参数合法性
ApiInterfaceCustom
apiCustomInfo
=
checkParamLegal
(
param
,
dataTableSelectTO
.
getApiInterface
());
if
(
ApiInfoOutTypeEnum
.
FLOW
.
name
().
equals
(
dataTableSelectTO
.
getApiInterface
().
getOutputType
()))
{
//文件流形式请求
return
flowRequestMethod
(
dataTableSelectTO
.
getApiInterface
().
getTargetUrl
(),
param
,
dataTableSelectTO
.
getOpenApiResponse
(),
apiCustomInfo
,
dataTableSelectTO
.
getApiInterface
());
}
else
if
(
ApiInfoOutTypeEnum
.
JSON
.
name
().
equals
(
dataTableSelectTO
.
getApiInterface
().
getOutputType
()))
{
//json格式请求
return
jsonRequestMethod
(
dataTableSelectTO
.
getApiInterface
().
getTargetUrl
(),
param
,
jsonParams
,
dataTableSelectTO
.
getOpenApiResponse
(),
apiCustomInfo
,
dataTableSelectTO
.
getApiInterface
());
}
else
{
throw
new
GatewayException
(
GatewayResultCode
.
OUTPUT_TYPE_EXCEPTION
);
}
}
/**
* 校验参数合法性
*
* @param param
* @param apiInterface
* @return
*/
private
ApiInterfaceCustom
checkParamLegal
(
Map
<
String
,
String
>
param
,
ApiInterface
apiInterface
)
{
ApiInterfaceCustom
apiCustomInfo
=
apiInterfaceService
.
getApiCustomInfo
(
apiInterface
.
getId
());
boolean
tag
=
false
;
if
(
null
!=
apiCustomInfo
)
{
List
<
Map
>
mapList
=
JSONObject
.
parseArray
(
apiCustomInfo
.
getRequestParam
(),
Map
.
class
);
if
(
CollectionUtils
.
isNotEmpty
(
mapList
))
{
for
(
Map
map
:
mapList
)
{
if
((
Boolean
)
map
.
get
(
"required"
))
{
String
name
=
(
String
)
map
.
get
(
"name"
);
String
field
=
param
.
get
(
name
);
if
(
null
==
field
)
{
tag
=
true
;
}
}
}
if
(
tag
)
{
throw
new
GatewayException
(
GatewayResultCode
.
REQUEST_PARAM_EMPTY
);
}
}
}
return
apiCustomInfo
;
}
/**
* 数据查询---flow流请求方式
*
* @param targetUrl
* @param param
* @param response
* @return
*/
private
boolean
flowRequestMethod
(
String
targetUrl
,
Map
<
String
,
String
>
param
,
OpenApiResponse
response
,
ApiInterfaceCustom
apiCustomInfo
,
ApiInterface
apiInterface
)
{
String
dataSize
=
param
.
get
(
"dataSize"
);
Integer
valueOf
=
-
1
;
if
(
StringUtils
.
isNotBlank
(
dataSize
))
{
valueOf
=
Integer
.
valueOf
(
dataSize
);
}
net
.
sf
.
json
.
JSONObject
reqParams
=
net
.
sf
.
json
.
JSONObject
.
fromObject
(
param
);
JSONObject
requestParams
=
new
JSONObject
();
requestParams
.
put
(
"datasourceId"
,
apiCustomInfo
.
getEsDataSource
());
requestParams
.
put
(
"query_database"
,
apiCustomInfo
.
getEsDataBase
());
requestParams
.
put
(
"query_table"
,
apiCustomInfo
.
getEsTable
());
requestParams
.
put
(
"request_fileds"
,
reqParams
);
requestParams
.
put
(
"response_fields"
,
assembleResponseParams
(
apiCustomInfo
.
getResponseParam
()));
requestParams
.
put
(
"data_size"
,
valueOf
);
HttpDownload
.
postDownload
(
targetUrl
,
requestParams
);
reqLogService
.
updateLog
(
apiInterface
.
getLogId
(),
null
);
response
.
setCode
(
GatewayResultCode
.
SUCCESS
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
SUCCESS
.
getMsg
());
return
true
;
}
/**
* 组装响应参数
*
* @param respParams
* @return
*/
private
String
assembleResponseParams
(
String
respParams
)
{
StringBuilder
builder
=
new
StringBuilder
();
List
<
Map
>
mapList
=
JSONObject
.
parseArray
(
respParams
,
Map
.
class
);
if
(
CollectionUtils
.
isNotEmpty
(
mapList
))
{
for
(
Map
map
:
mapList
)
{
String
name
=
(
String
)
map
.
get
(
"name"
);
builder
.
append
(
name
).
append
(
LoggingConstants
.
SEP
);
}
}
return
builder
.
substring
(
0
,
builder
.
length
()
-
1
);
}
/**
* 数据查询--json请求方式
*
* @param targetUrl
* @param param
* @param response
* @param apiCustomInfo
* @return
*/
private
boolean
jsonRequestMethod
(
String
targetUrl
,
Map
<
String
,
String
>
param
,
JSONObject
jsonParams
,
OpenApiResponse
response
,
ApiInterfaceCustom
apiCustomInfo
,
ApiInterface
apiInterface
)
{
JSONObject
params
=
new
JSONObject
();
try
{
params
.
put
(
"datasourceId"
,
apiCustomInfo
.
getEsDataSource
());
//数据源id
params
.
put
(
"query_database"
,
apiCustomInfo
.
getEsDataBase
());
//数据源库名称
params
.
put
(
"query_table"
,
apiCustomInfo
.
getEsTable
());
//数据源库表
// net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params
.
put
(
"request_fileds"
,
jsonParams
.
getJSONObject
(
"request_fileds"
));
//请求参数
params
.
put
(
"response_fields"
,
assembleResponseParams
(
apiCustomInfo
.
getResponseParam
()));
//响应参数
params
.
put
(
"is_test"
,
jsonParams
.
getBoolean
(
"is_test"
));
//是否是测试
Integer
pageNum
=
jsonParams
.
getInteger
(
"page_num"
);
Integer
pageSize
=
jsonParams
.
getInteger
(
"page_size"
);
params
.
put
(
"page_size"
,
apiCustomInfo
.
getPageRow
());
params
.
put
(
"page_num"
,
1
);
if
(
null
!=
pageNum
)
{
params
.
put
(
"page_num"
,
pageNum
);
}
if
(
null
!=
pageSize
)
{
params
.
put
(
"page_size"
,
pageSize
);
}
}
catch
(
Exception
ex
)
{
log
.
error
(
"数据转换异常:{}"
,
ex
.
getMessage
());
ex
.
printStackTrace
();
}
String
respResult
=
httpsUtils
.
submitPost
(
targetUrl
,
params
.
toString
());
return
callMethodResponse
(
respResult
,
response
,
apiInterface
);
}
/**
* 调用方法处理结果
*
* @param result
* @param response
*/
private
boolean
callMethodResponse
(
String
result
,
OpenApiResponse
response
,
ApiInterface
apiInterface
)
{
if
(
null
==
result
)
{
throw
new
GatewayException
(
GatewayResultCode
.
DISTANCE_REQUEST_EXCEPTION
);
}
JSONObject
resp
=
JSONObject
.
parseObject
(
result
);
if
(
null
!=
resp
&&
"ESC00000"
.
equals
(
resp
.
getString
(
"return_code"
)))
{
response
.
setCode
(
GatewayResultCode
.
SUCCESS
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
SUCCESS
.
getMsg
());
response
.
setAttribute
(
"responseResult"
,
resp
);
if
(
null
!=
apiInterface
.
getLogId
()){
reqLogService
.
updateLog
(
apiInterface
.
getLogId
(),
result
);
}
return
true
;
}
else
{
log
.
error
(
"~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~"
);
reqLogService
.
updateLog
(
apiInterface
.
getLogId
(),
result
);
throw
new
GatewayException
(
GatewayResultCode
.
DISTANCE_REQUEST_EXCEPTION
);
}
}
}
jz-dm-apigateway/src/main/java/com/jz/dm/service/request/impl/ThirdSelectHandle.java
0 → 100644
View file @
d6402b13
package
com
.
jz
.
dm
.
service
.
request
.
impl
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.jz.common.utils.HttpsUtils
;
import
com.jz.dm.common.enums.GatewayResultCode
;
import
com.jz.dm.common.exception.GatewayException
;
import
com.jz.dm.common.util.OpenApiResponse
;
import
com.jz.dm.mapper.ApiFunctionMapper
;
import
com.jz.dm.models.domian.ApiFunction
;
import
com.jz.dm.models.domian.ApiInterface
;
import
com.jz.dm.models.domian.ApiInterfaceCustom
;
import
com.jz.dm.models.enity.DataTableSelectTO
;
import
com.jz.dm.service.ApiInterfaceService
;
import
com.jz.dm.service.ApiLogService
;
import
com.jz.dm.service.request.ThirdSelectService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ThirdSelectHandle
* @DATE: 2021-2-24/10:42
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Slf4j
@Component
public
class
ThirdSelectHandle
implements
ThirdSelectService
{
public
static
final
String
JSON_CONTENT_FORM
=
"application/json; charset=utf-8"
;
public
static
final
String
CONTENT_FORM
=
"application/x-www-form-urlencoded;charset=UTF-8"
;
public
static
final
String
FROM_DATA_CONTENT_FORM
=
"application/from-data;charset=UTF-8"
;
@Autowired
private
ApiLogService
reqLogService
;
@Autowired
private
HttpsUtils
httpsUtils
;
@Autowired
private
ApiFunctionMapper
apiFunctionMapper
;
@Autowired
private
ApiInterfaceService
apiInterfaceService
;
/**
* 三方查询处理
* @param dataTableSelectTO
* @return
*/
@Override
public
boolean
thirdSelectHandle
(
DataTableSelectTO
dataTableSelectTO
)
{
if
(
"POST"
.
equalsIgnoreCase
(
dataTableSelectTO
.
getApiInterface
().
getReqType
()))
{
Map
map
=
JSONObject
.
parseObject
(
dataTableSelectTO
.
getReqParams
(),
Map
.
class
);
Map
reqParam
=
assembleReqParam
(
map
,
dataTableSelectTO
.
getApiInterface
(),
dataTableSelectTO
.
getAuthCode
());
net
.
sf
.
json
.
JSONObject
fromObject
=
net
.
sf
.
json
.
JSONObject
.
fromObject
(
reqParam
);
HashMap
<
String
,
String
>
headers
=
new
HashMap
<>();
if
(
"1"
.
equals
(
dataTableSelectTO
.
getApiInterface
().
getPostType
()))
{
headers
.
put
(
"Content-type"
,
CONTENT_FORM
);
}
else
if
(
"2"
.
equals
(
dataTableSelectTO
.
getApiInterface
().
getPostType
()))
{
headers
.
put
(
"Content-type"
,
FROM_DATA_CONTENT_FORM
);
}
else
{
headers
.
put
(
"Content-type"
,
JSON_CONTENT_FORM
);
}
String
result
=
httpsUtils
.
submitPost
(
dataTableSelectTO
.
getApiInterface
().
getTargetUrl
(),
fromObject
.
toString
(),
headers
);
return
callMethodResponse
(
result
,
dataTableSelectTO
.
getOpenApiResponse
(),
dataTableSelectTO
.
getApiInterface
());
}
else
{
Map
map
=
JSONObject
.
parseObject
(
dataTableSelectTO
.
getReqParams
(),
Map
.
class
);
Map
reqParam
=
assembleReqParam
(
map
,
dataTableSelectTO
.
getApiInterface
(),
dataTableSelectTO
.
getAuthCode
());
String
result
=
httpsUtils
.
doGet
(
dataTableSelectTO
.
getApiInterface
().
getTargetUrl
(),
reqParam
);
return
callMethodResponse
(
result
,
dataTableSelectTO
.
getOpenApiResponse
(),
dataTableSelectTO
.
getApiInterface
());
}
}
/**
* 组装请求参数
*
* @param param
* @param apiInterface
* @return
*/
private
Map
assembleReqParam
(
Map
param
,
ApiInterface
apiInterface
,
String
authCode
)
{
List
<
String
>
listFuncNames
=
apiFunctionMapper
.
listFunctionNames
();
ApiInterfaceCustom
apiCustomInfo
=
apiInterfaceService
.
getApiCustomInfo
(
apiInterface
.
getId
());
if
(
null
!=
apiCustomInfo
)
{
List
<
JSONObject
>
jsonList
=
JSONObject
.
parseArray
(
apiCustomInfo
.
getInboxParam
(),
JSONObject
.
class
);
if
(
CollectionUtils
.
isNotEmpty
(
jsonList
))
{
for
(
JSONObject
paObject
:
jsonList
)
{
String
functionName
=
paObject
.
getString
((
"type"
));
if
(
listFuncNames
.
contains
(
functionName
))
{
String
name
=
paObject
.
getString
(
"name"
);
if
(
functionName
.
equals
(
"function03"
))
{
String
functionVal
=
getFunctionReqData
(
functionName
,
authCode
,
null
);
if
(
StringUtils
.
isNotBlank
(
functionVal
))
{
param
.
put
(
name
,
functionVal
);
}
}
else
{
String
functionVal
=
getFunctionReqData
(
functionName
,
null
,
param
);
if
(
StringUtils
.
isNotBlank
(
functionVal
))
{
param
.
put
(
name
,
functionVal
);
}
}
}
}
}
}
return
param
;
}
private
String
getFunctionReqData
(
String
name
,
String
authCode
,
Map
<
String
,
String
>
signMap
)
{
String
resultStr
=
""
;
QueryWrapper
<
ApiFunction
>
queryWra
=
new
QueryWrapper
<>();
queryWra
.
eq
(
"name"
,
name
);
queryWra
.
eq
(
"status"
,
true
);
queryWra
.
eq
(
"is_deleted"
,
0
);
ApiFunction
apiFunction
=
apiFunctionMapper
.
selectOne
(
queryWra
);
if
(
null
!=
apiFunction
)
{
JSONObject
reqParameter
=
new
JSONObject
();
if
(
"name"
.
equals
(
"function03"
))
{
//根据不同模板组装不同请求参数
reqParameter
.
put
(
"authCode"
,
authCode
);
}
else
{
reqParameter
.
put
(
"signParams"
,
signMap
);
}
String
result
=
httpsUtils
.
submitPost
(
apiFunction
.
getTemplateUrl
(),
reqParameter
.
toString
());
if
(
null
!=
result
)
{
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
result
);
if
(
200
==
jsonObject
.
getInteger
(
"code"
))
{
if
(
"name"
.
equals
(
"function03"
))
{
resultStr
=
jsonObject
.
getString
(
"data"
);
}
else
{
JSONObject
data
=
jsonObject
.
getJSONObject
(
"data"
);
resultStr
=
data
.
getString
(
"signStr"
);
}
}
}
}
return
resultStr
;
}
/**
* 调用方法处理结果
*
* @param result
* @param response
*/
private
boolean
callMethodResponse
(
String
result
,
OpenApiResponse
response
,
ApiInterface
apiInterface
)
{
if
(
null
==
result
)
{
throw
new
GatewayException
(
GatewayResultCode
.
DISTANCE_REQUEST_EXCEPTION
);
}
JSONObject
resp
=
JSONObject
.
parseObject
(
result
);
if
(
null
!=
resp
&&
"ESC00000"
.
equals
(
resp
.
getString
(
"return_code"
)))
{
response
.
setCode
(
GatewayResultCode
.
SUCCESS
.
getCode
());
response
.
setMsg
(
GatewayResultCode
.
SUCCESS
.
getMsg
());
response
.
setAttribute
(
"responseResult"
,
resp
);
if
(
null
!=
apiInterface
.
getLogId
())
{
reqLogService
.
updateLog
(
apiInterface
.
getLogId
(),
result
);
}
return
true
;
}
else
{
log
.
error
(
"~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~"
);
reqLogService
.
updateLog
(
apiInterface
.
getLogId
(),
result
);
throw
new
GatewayException
(
GatewayResultCode
.
DISTANCE_REQUEST_EXCEPTION
);
}
}
}
jz-dm-apigateway/src/test/java/com/jz/dm/gateway/api/ApiReqTest.java
View file @
d6402b13
...
@@ -8,6 +8,9 @@ import com.jz.dm.gateway.SpringTestCase;
...
@@ -8,6 +8,9 @@ import com.jz.dm.gateway.SpringTestCase;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
/**
* @author ZC
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.api
* @PACKAGE_NAME: com.jz.dm.gateway.api
...
@@ -23,12 +26,16 @@ public class ApiReqTest extends SpringTestCase {
...
@@ -23,12 +26,16 @@ public class ApiReqTest extends SpringTestCase {
@Autowired
@Autowired
private
HttpsUtils
httpsUtils
;
private
HttpsUtils
httpsUtils
;
/**
* 生成数据查询json请求签名
*/
@Test
@Test
public
void
TestGatewayReq
()
{
public
void
TestGateway
Json
Req
()
{
JSONObject
jsonObject
=
new
JSONObject
();
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"apiKey"
,
"Y18q7v2Rq332H3ox"
);
jsonObject
.
put
(
"apiKey"
,
"Y18q7v2Rq332H3ox"
);
jsonObject
.
put
(
"method"
,
"
query
"
);
jsonObject
.
put
(
"method"
,
"
request
"
);
jsonObject
.
put
(
"signType"
,
"MD5"
);
jsonObject
.
put
(
"signType"
,
"MD5"
);
long
time
=
System
.
currentTimeMillis
();
long
time
=
System
.
currentTimeMillis
();
String
date
=
String
.
valueOf
(
time
);
String
date
=
String
.
valueOf
(
time
);
...
@@ -36,21 +43,73 @@ public class ApiReqTest extends SpringTestCase {
...
@@ -36,21 +43,73 @@ public class ApiReqTest extends SpringTestCase {
JSONObject
params
=
new
JSONObject
();
JSONObject
params
=
new
JSONObject
();
params
.
put
(
"authCode"
,
"20210000021281934021n275Hn63qaxP"
);
params
.
put
(
"authCode"
,
"20210000021281934021n275Hn63qaxP"
);
params
.
put
(
"reqParams"
,
new
JSONObject
());
params
.
put
(
"isTest"
,
false
);
JSONObject
reqParams
=
new
JSONObject
();
reqParams
.
put
(
"datasourceId"
,
"1"
);
reqParams
.
put
(
"query_database"
,
"product"
);
reqParams
.
put
(
"query_table"
,
"table1"
);
JSONObject
fields
=
new
JSONObject
();
fields
.
put
(
"flelds1"
,
"xxxxxx"
);
fields
.
put
(
"flelds2"
,
"xxxxxx"
);
reqParams
.
put
(
"request_fileds"
,
fields
);
reqParams
.
put
(
"response_fields"
,
"field1,field2,field3,field4"
);
reqParams
.
put
(
"page_num"
,
1
);
reqParams
.
put
(
"page_size"
,
100
);
params
.
put
(
"reqParams"
,
reqParams
);
jsonObject
.
put
(
"params"
,
params
);
jsonObject
.
put
(
"params"
,
params
);
try
{
try
{
String
apiKey
=
jsonObject
.
getString
(
"apiKey"
);
Map
<
String
,
String
>
paramsMap
=
new
HashMap
<>();
String
method
=
jsonObject
.
getString
(
"method"
);
paramsMap
.
put
(
"apiKey"
,
jsonObject
.
getString
(
"apiKey"
));
String
signType
=
jsonObject
.
getString
(
"signType"
);
paramsMap
.
put
(
"method"
,
jsonObject
.
getString
(
"method"
));
paramsMap
.
put
(
"signType"
,
jsonObject
.
getString
(
"signType"
));
String
signature
=
MapUtil
.
getSignValue
(
apiKey
,
method
,
signType
);
paramsMap
.
put
(
"params"
,
params
.
toString
());
String
sign
=
Md5
.
encrypt
(
signature
,
"RPf8HL36"
);
String
signParams
=
MapUtil
.
stringNormalSort
(
paramsMap
);
jsonObject
.
put
(
"sign"
,
sign
);
String
sign
=
Md5
.
encrypt
(
signParams
,
"RPf8HL36"
);
/* String response = httpsUtils.submitPost(url, jsonObject.toString());*/
System
.
out
.
println
(
"时间戳为:"
+
date
+
"--签名为:=="
+
sign
);
System
.
out
.
println
(
"时间戳为:"
+
date
+
"--签名为:=="
+
sign
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
/**
* 生成数据查询flow请求签名
*/
@Test
public
void
TestGatewayFlowReq
()
{
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"apiKey"
,
"fM59D6210E3K436m"
);
jsonObject
.
put
(
"method"
,
"request"
);
jsonObject
.
put
(
"signType"
,
"MD5"
);
long
time
=
System
.
currentTimeMillis
();
String
date
=
String
.
valueOf
(
time
);
jsonObject
.
put
(
"timestamp"
,
date
);
JSONObject
params
=
new
JSONObject
();
params
.
put
(
"authCode"
,
"20210000000111181048520T38nzc5x7"
);
params
.
put
(
"isTest"
,
false
);
JSONObject
reqParams
=
new
JSONObject
();
reqParams
.
put
(
"datasourceId"
,
"1"
);
reqParams
.
put
(
"query_database"
,
"product"
);
reqParams
.
put
(
"query_table"
,
"table1"
);
JSONObject
fields
=
new
JSONObject
();
fields
.
put
(
"flelds1"
,
"xxxxxx"
);
fields
.
put
(
"flelds2"
,
"xxxxxx"
);
reqParams
.
put
(
"request_fileds"
,
fields
);
reqParams
.
put
(
"response_fields"
,
"field1,field2,field3,field4"
);
reqParams
.
put
(
"data_size"
,
500
);
params
.
put
(
"reqParams"
,
reqParams
);
jsonObject
.
put
(
"params"
,
params
);
try
{
Map
<
String
,
String
>
paramsMap
=
new
HashMap
<>();
paramsMap
.
put
(
"apiKey"
,
jsonObject
.
getString
(
"apiKey"
));
paramsMap
.
put
(
"method"
,
jsonObject
.
getString
(
"method"
));
paramsMap
.
put
(
"signType"
,
jsonObject
.
getString
(
"signType"
));
paramsMap
.
put
(
"params"
,
params
.
toString
());
String
signParams
=
MapUtil
.
stringNormalSort
(
paramsMap
);
String
sign
=
Md5
.
encrypt
(
signParams
,
"IE36FItU"
);
System
.
out
.
println
(
"时间戳为:"
+
date
+
"--签名为:=="
+
sign
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
jz-dm-apigateway/src/test/java/com/jz/dm/gateway/orther/TestHttpReq.java
View file @
d6402b13
...
@@ -38,7 +38,7 @@ public class TestHttpReq extends SpringTestCase {
...
@@ -38,7 +38,7 @@ public class TestHttpReq extends SpringTestCase {
headers
.
put
(
"connection"
,
"keep-alive"
);
headers
.
put
(
"connection"
,
"keep-alive"
);
headers
.
put
(
"Charsert"
,
"UTF-8"
);
headers
.
put
(
"Charsert"
,
"UTF-8"
);
headers
.
put
(
"Accept"
,
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
);
headers
.
put
(
"Accept"
,
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
);
String
response
=
httpsUtils
.
submitPost
(
baseUrlPost
,
jsonObject
,
headers
);
String
response
=
httpsUtils
.
submitPost
(
baseUrlPost
,
jsonObject
.
toString
()
,
headers
);
System
.
out
.
println
(
response
);
System
.
out
.
println
(
response
);
}
}
...
...
jz-dm-apigateway/src/test/java/com/jz/dm/gateway/orther/TestMakeApi.java
View file @
d6402b13
...
@@ -38,7 +38,7 @@ public class TestMakeApi extends SpringTestCase {
...
@@ -38,7 +38,7 @@ public class TestMakeApi extends SpringTestCase {
params
.
put
(
"apiProtocl"
,
"HTTPS"
);
params
.
put
(
"apiProtocl"
,
"HTTPS"
);
params
.
put
(
"type"
,
"1"
);
params
.
put
(
"type"
,
"1"
);
try
{
try
{
String
result
=
httpsUtils
.
submitPost
(
url
,
params
,
headers
);
String
result
=
httpsUtils
.
submitPost
(
url
,
params
.
toString
()
,
headers
);
System
.
out
.
println
(
"接受到的结果为:"
+
result
);
System
.
out
.
println
(
"接受到的结果为:"
+
result
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
jz-dm-common/src/main/java/com/jz/common/constant/ResultCode.java
View file @
d6402b13
...
@@ -17,14 +17,14 @@ public enum ResultCode {
...
@@ -17,14 +17,14 @@ public enum ResultCode {
/** 执行成功 */
/** 执行成功 */
SUCCESS
(
200
,
ResultMsg
.
SUCCESS
),
SUCCESS
(
200
,
ResultMsg
.
SUCCESS
),
/** 执行失败 */
/** 执行失败 */
FAILURE
(
300
,
ResultMsg
.
DATA_NOT_FOUND
)
,
FAILURE
(
300
,
ResultMsg
.
DATA_NOT_FOUND
)
;
/*
/** 未授权的访问 */
*//** 未授权的访问 *//*
UNAUTHORIZED(401, ResultMsg.UNAUTHORIZED),
UNAUTHORIZED(401, ResultMsg.UNAUTHORIZED),
/** 授权过期 */
*//** 授权过期 *//*
INVALID_TOKEN(402, ResultMsg.INVALID_TOKEN),
INVALID_TOKEN(402, ResultMsg.INVALID_TOKEN),
/** 禁止访问 */
*//** 禁止访问 *//*
FORBIDOM
(
403
,
ResultMsg
.
FORBIDOM
);
FORBIDOM(403, ResultMsg.FORBIDOM);
*/
private
int
code
;
private
int
code
;
...
...
jz-dm-common/src/main/java/com/jz/common/constant/ResultMsg.java
View file @
d6402b13
This diff is collapsed.
Click to expand it.
jz-dm-common/src/main/java/com/jz/common/utils/HttpsUtils.java
View file @
d6402b13
package
com
.
jz
.
common
.
utils
;
package
com
.
jz
.
common
.
utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.toolkit.CollectionUtils
;
import
com.baomidou.mybatisplus.core.toolkit.CollectionUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
...
@@ -45,20 +44,20 @@ public class HttpsUtils {
...
@@ -45,20 +44,20 @@ public class HttpsUtils {
* @param headers 请求头
* @param headers 请求头
* @return
* @return
*/
*/
public
String
submitPost
(
String
url
,
JSONObject
params
,
Map
<
String
,
String
>
headers
)
{
public
String
submitPost
(
String
url
,
String
params
,
Map
<
String
,
String
>
headers
)
{
CloseableHttpClient
httpClient
=
getHttpClient
();
CloseableHttpClient
httpClient
=
getHttpClient
();
String
body
=
null
;
String
body
=
null
;
CloseableHttpResponse
response
=
null
;
CloseableHttpResponse
response
=
null
;
try
{
try
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
HttpPost
httpPost
=
new
HttpPost
(
url
);
httpPost
.
setHeader
(
"Content-type"
,
JSON_CONTENT_FORM
);
/* httpPost.setHeader("Content-type",JSON_CONTENT_FORM );*/
if
(
null
!=
headers
&&
headers
.
size
()
>
0
)
{
if
(
null
!=
headers
&&
headers
.
size
()
>
0
)
{
for
(
Map
.
Entry
<
String
,
String
>
e
:
headers
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
String
>
e
:
headers
.
entrySet
())
{
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
}
}
}
}
if
(
StringUtils
.
isNotBlank
(
params
.
toString
()
))
{
if
(
StringUtils
.
isNotBlank
(
params
))
{
httpPost
.
setEntity
(
new
StringEntity
(
params
.
toString
()
,
Consts
.
UTF_8
));
httpPost
.
setEntity
(
new
StringEntity
(
params
,
Consts
.
UTF_8
));
}
}
response
=
httpClient
.
execute
(
httpPost
);
response
=
httpClient
.
execute
(
httpPost
);
body
=
getBody
(
response
.
getEntity
());
body
=
getBody
(
response
.
getEntity
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment