作者:超图-于丁1
iDesktopX配置文件快速扩展HTTP请求工具GPA算子
一、背景:
在综合系统业务中处理自动化GPA模型算子有着众多便利性,今天就为大家介绍一种通过简单文件配置,就可以将http请求配置封装为一个GPA算子。该http请求可以是iServer的rest api接口、可以是自己业务系统后端开发的请求接口、也可以是三方请求接口,只要符合http接口。
通过配置文件的方法封装为gpa算子,可以不用通过代码手写请求,引入专门的请求三方库,极大程度便利了算子的继承与制作。
二、以配置文件形式注册:
2.1 配置文件
文件路径:{gpa 包路径}/conf/http/list.json,其中 gpa 包 为 sps-core.jar 或 geoprocessing-server.jar。
注:配置文件如果不存在,会在第一次加载工具列表时创建。
如:{iServer_home}/support/geoprocessing/conf/http/list.json、{iDesktopX_home}/lib/conf/http/list.json
2.2 参数说明
配置文件内容为 HttpProcessInfo 的 json 数组。
其中,单个 HttpProcessInfo JSON 结构体如下:
HttpProcessInfo JSON结构体示例
{
"authenticationType":"TOKEN",
"title": "通过任务ID获取日志",
"inputs":[
{
"collection":false,
"description":"",
"name":"logID",
"required":true,
"title":"任务ID",
"paramType": "PATH",
"type":"java.lang.String"
},
{
"collection":false,
"description":"",
"name":"levels",
"required":false,
"title":"日志等级",
"type":"java.lang.String"
},
{
"collection":false,
"description":"",
"name":"newStructure",
"required":false,
"title":"isNewStructure",
"type":"java.lang.Boolean"
}
],
"method":"GET",
"name":"getLogByID",
"url":"http://172.16.120.41:8090/iserver/services/geoprocessing/restjsr/gpmodeler/logs/{logID}"
}
2.2.1 JSON 字段描述
name | String | 工具名称,必填 |
title | String | 工具标题,必填 |
| description | String | 工具描述 |
| url | String | http 接口,必填 |
| method | String | 请求方法,可选值为:GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH,默认为 GET |
authenticationType | String | 验证方式:必填
|
| requestBodyType | String | 请求体参数的类型。对于 ParamType 为 BODY 的 input(见下表 input 参数说明) 有效。默认为 JSON。 1、JSON:请求体为json格式,并将请求体拆分为多个 ParamType 为 BODY 的 input 。 2、TEMPLATE:提供一个 requestBodyTemplate 模板,模板中占位符为 {paramName},使用 input 的值填充。 |
| requestBodyTemplate | String | 请求体模板,requestBodyType 为 TEMPLATE 时,将参数填充到请求体模板中。 |
| inputs | JSONArray | 输入配置。可选,除了配置的输入外,默认存在一个请求头参数。 |
| outputs | JSONArray | 输出配置。为空时,直接将响应结果输出。 |
| asyncMonitor | Object | 异步监控器;如果当前 url 是一个异步提交任务的请求,通过指定 asyncMonitor 监控并获取结果。 |
2.2.2 Input 字段描述
name | String | 输入参数名称,必填 |
title | String | 输入参数标题,必填 |
| description | String | 输入参数描述 |
| type | String | 输入参数类型,默认为 java.lang.String |
| defaultValue | String | 参数默认值,配置后,通过GPA内置的转换器转换为指定类型。 |
| collection | bool | 是否为集合输入 |
required | bool | 是否为必填输入 |
| paramType | String | 指定参数的类型,默认为 QUERY。可选值如下: 1、QUERY:将参数以 url?key1=value1&key2=value2 的形式传递。 2、BODY:将参数构造为请求体,与上表中的 requestBodyType 参数值有关:
3、PATH:路径参数,将参数填充到 url 中,替换原 url 中的 {inputName} 。例如:url 为 http://localhost:6066/group/{groupid} 。路径参数 groupid 值为 test,所得到的 url 就为 http://localhost:6066/group/test。 |
| paramName | String | http 参数名称,默认为 name(gpa参数名称);允许 http 参数名称与 gpa 工具名称不一致。 |
| children | JSONArray | 类型为 input。配置多层级JSON,参数面板只显示最深层的参数,当第一层 input.paramType=BODY 时有效。非最深层的input 配置的 title,description,type 等参数无效。 |
2.2.3 output 字段描述
name | String | 输出名称,必填 |
title | String | 输出标题,必填 |
| description | String | 输出描述 |
| type | String | 输出类型,默认为 java.lang.String |
| collection | bool | 是否为集合输出 |
| nullable | bool | 输出是否允许为空 |
| dataPath | JSONArray | 类型为 String,从json字符串中读取输出的路径:json.path[1].path[2]...,默认为空,读取整个响应结果。 例如: json 为 {"state": {"runState": "FINISHED"}}; dataPath 配置为 ["state", "runState"] 则解析出的结果就为 FINISHED |
2.2.4 Interval 字段描述
| handler | String | 判断结束条件的处理器类型,可扩展,必填; 内置处理器:
|
| handlerParams | Object | 处理器参数,与配置的处理器相关。 |
| intervalTime | int | 间隔时间,默认为 3. |
| timeUnit | String | 时间单位,默认为 SECONDS(秒),可选值为: NANOSECONDS(纳秒)、MICROSECONDS(微秒)、MILLISECONDS(毫秒)、SECONDS(秒)、MINUTES(分)、HOURS(小时)、DAYS(天) |
内置处理器说明:
1)com.supermap.sps.processes.http.interval.StringMatchIntervalHandler(默认):http 响应体匹配上 finishPattern 时结束轮询
- responseType:响应体格式,可选值为 JSON、TEXT。默认为 JSON。
- dataPath:从响应体中读取任务状态信息的路径。responseType 为 JSON 时有效。
- finishPattern:匹配表达式,支持 正则表达式与通配符。
一些正则表达式的常见用法:
- 值为 FINISHED 或 FAILED:((FINISHED)|(FAILED))
- 值不包含 RUNNING:(?!.*RUNNING).*
处理器扩展:
1)实现 com.supermap.sps.processes.http.interval.IIntervalHandler 接口,实现类需要无参构造。
IIntervalHandler 接口
public interface IIntervalHandler {
/**
* 根据响应体判断请求是否结束
*
* @param responseBody 响应体
* @return 是否结束
*/
boolean isFinished(String responseBody);
}
2)实现 boolean isFinished(String responseBody) 方法,并为所需参数添加 setter、getter 方法,以便于JSON序列化。
如 StringMatchIntervalHandler 实现
public class StringMatchIntervalHandler implements IIntervalHandler {
private String finishPattern;
private StringMatchMode matchMode;
// region setter && getter
public String getFinishPattern() {
return finishPattern;
}
public void setFinishPattern(String finishPattern) {
this.finishPattern = finishPattern;
}
public StringMatchMode getMatchMode() {
return matchMode;
}
public void setMatchMode(StringMatchMode matchMode) {
this.matchMode = matchMode;
}
// endregion
@Override
public boolean isFinished(String responseBody) {
if (finishPattern == null) {
return false;
}
if (matchMode == StringMatchMode.WILDCARD) {
return FileNameUtils.wildcardMatch(responseBody, finishPattern);
} else if (matchMode == StringMatchMode.REGULAR_EXPRESSION) {
return responseBody != null && responseBody.matches(finishPattern);
} else {
return StringUtils.stringEquals(finishPattern, responseBody);
}
}
}
2.2.5 asyncMonitor 字段描述
| url | String | http 接口,必填 |
| method | String | 请求方法,可选值为:GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH,默认为 GET |
| requestBodyType | String | 见 JSON字段描述中的 requestBodyType。 |
| requestBodyTemplate | String | 见 JSON字段描述中的 requestBodyTemplate 。 |
| monitorParams | JSONArray | 监控参数配置。具体见下表。一般用作从提交异步任务的http响应体中解析出任务id,并传给 asyncMonitor 中监控任务的请求参数,以监控指定任务。 |
| interval | Object | 配置后,将定时轮询http请求,到满足配置的结束条件为止。 |
monitorParams 字段描述
name | String | 输入参数名称,必填 |
| type | String | 输入参数类型,默认为 java.lang.String |
| defaultValue | String | 参数默认值,配置后,通过GPA内置的转换器转换为指定类型。 |
| collection | bool | 是否为集合参数 |
| paramType | String | 见 Input 参数说明中的 paramType |
| paramName | String | 见 Input 参数说明中的 paramName |
| dataPath | JSONArray | 从提交任务的http响应体中获取值。 例如: 提交任务的响应体为:{"jobID": "gp-20230728-164457-E9849"} dataPath 值配置为:["jobID"] 则该参数的值为:gp-20230728-164457-E9849 |
2.3 示例
1)配置多层级 JSON
a.通过 children 配置多层级 JSON
以异步执行GPA模型的接口为例。
模型执行的参数为:
{
"parameter": {
"connectionInfo": ""
"targetFilePath": ""
}
}
将 parameter 参数分解为 connectionInfo 和 targetFilePath 两个参数。
配置如下:
{
"authenticationType":"TOKEN",
"title": "执行模型",
"method":"POST",
"name":"executeModel",
"url":"http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/sps.WorkflowProcessFactory.models.test:%E5%AF%BC%E5%87%BAtemp/jobs",
"inputs": [
{
"collection": false,
"name": "parameter",
"paramType": "BODY",
"children": [
{
"collection": false,
"name": "connectionInfo",
"required": true,
"title": "连接信息",
"type": "java.lang.String"
},
{
"collection": false,
"name": "targetFilePath",
"required": true,
"title": "文件路径",
"type": "java.lang.String"
}
]
}
]
}
效果:

b. json 类型配置
使用 "type": "com.alibaba.fastjson.JSON" 配置复杂或不定的json字段。将 json 字符串转为 json 对象。
配置如下:
{
"parameter": {
"connectionInfo": ""
"targetFilePath": ""
},
"environments": {}
}
新增 environments 参数,并配置 "type": "com.alibaba.fastjson.JSON":
{
"authenticationType":"TOKEN",
"title": "执行模型",
"method":"POST",
"name":"executeModel",
"url":"http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/sps.WorkflowProcessFactory.models.test:%E5%AF%BC%E5%87%BAtemp/jobs",
"inputs": [
{
"collection": false,
"name": "parameter",
"paramType": "BODY",
"children": [
{
"collection": false,
"name": "connectionInfo",
"required": true,
"title": "连接信息",
"type": "java.lang.String"
},
{
"collection": false,
"name": "targetFilePath",
"required": true,
"title": "文件路径",
"type": "java.lang.String"
}
]
}, {
"collection": false,
"name": "environments",
"required": true,
"title": "环境参数",
"paramType": "BODY",
"type": "com.alibaba.fastjson.JSON"
}
]
}
2)使用定时轮询获取异步任务结果
a. 提交任务并监控
{
"asyncMonitor": {
"interval": {
"handler": "com.supermap.sps.processes.http.interval.JSONIIntervalHandler",
"handlerParams": {
"finishPattern": "FINISH*",
"matchMode": "WILDCARD",
"dataPath": [
"state",
"runState"
]
},
"intervalTime": 3,
"timeUnit": "SECONDS"
},
"method": "GET",
"monitorParams": [
{
"collection": false,
"dataPath": [
"jobID"
],
"name": "jobID",
"paramName": "jobID",
"paramType": "PATH",
"required": false,
"type": "java.lang.String"
}
],
"requestBodyType": "JSON",
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/jobs/{jobID}.rjson"
},
"authenticationType": "TOKEN",
"inputs": [
{
"collection": false,
"description": "",
"name": "id",
"paramName": "id",
"paramType": "PATH",
"required": true,
"title": "",
"type": "java.lang.String"
}
],
"method": "POST",
"name": "asyncSubmitGPATaskAndMonitor",
"title": "异步提交任务并监控",
"outputs": [
{
"collection": false,
"dataPath": [
"jobID"
],
"description": "",
"name": "jobID",
"nullable": false,
"title": "任务id",
"type": "java.lang.String"
},
{
"collection": false,
"dataPath": [
"state",
"runState"
],
"description": "",
"name": "runState",
"nullable": false,
"title": "结果状态",
"type": "java.lang.String"
},
{
"collection": false,
"dataPath": [
"messages",
"result"
],
"description": "",
"name": "result",
"nullable": true,
"title": "结果",
"type": "java.lang.String"
}
],
"requestBodyType": "JSON",
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/{id}/jobs"
}
b. 提交任务与监控分开处理
异步提交GPA任务的 http 工具配置
{
"authenticationType": "TOKEN",
"inputs": [
{
"collection": false,
"description": "",
"name": "id",
"paramName": "id",
"paramType": "PATH",
"required": true,
"title": "模型ID",
"type": "java.lang.String"
}
],
"method": "POST",
"name": "asyncSubmitGPATask",
"title": "异步提交任务",
"outputs": [
{
"collection": false,
"dataPath": [
"jobID"
],
"description": "",
"name": "jobID",
"nullable": false,
"title": "任务id",
"type": "java.lang.String"
}
],
"requestBodyType": "JSON",
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/{id}/jobs"
}
定时轮询获取异步任务执行状态 http 工具配置
{
"asyncMonitor": {
"interval": {
"handler": "com.supermap.sps.processes.http.interval.JSONIIntervalHandler",
"handlerParams": {
"finishPattern": "FINISH*",
"matchMode": "WILDCARD",
"dataPath": [
"state",
"runState"
]
},
"intervalTime": 3,
"timeUnit": "SECONDS"
}
},
"authenticationType": "TOKEN",
"inputs": [
{
"collection": false,
"description": "",
"name": "jobID",
"paramName": "jobID",
"paramType": "PATH",
"required": true,
"title": "任务ID",
"type": "java.lang.String"
}
],
"method": "GET",
"name": "asyncMonitor",
"title": "异步监控",
"outputs": [
{
"collection": false,
"dataPath": [
"jobID"
],
"description": "",
"name": "jobID",
"nullable": false,
"title": "任务id",
"type": "java.lang.String"
},
{
"collection": false,
"dataPath": [
"state",
"runState"
],
"description": "",
"name": "runState",
"nullable": false,
"title": "结果状态",
"type": "java.lang.String"
},
{
"collection": false,
"dataPath": [
"messages",
"result"
],
"description": "",
"name": "result",
"nullable": true,
"title": "结果",
"type": "java.lang.String"
}
],
"requestBodyType": "JSON",
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/jobs/{jobID}.rjson"
}
获取异步任务执行结果,需要定时轮询发起 http 请求,以上配置的 interval 代表每 3 秒发起一次请求,直到获取到请求结果 responseJson,并且 responseJson.state.runState 匹配上通配符 FINISH*。
异步提交任务+定时轮询获取结果模型搭建:

模型执行结果:


3)请求体模板
对应的 json 配置文件
{
"inputs": [
{
"collection": false,
"description": "",
"name": "key1",
"paramName": "inflow",
"paramType": "BODY",
"required": true,
"title": "inflow",
"type": "java.lang.String"
},
{
"collection": false,
"description": "",
"name": "key2",
"paramName": "infiltration",
"paramType": "BODY",
"required": true,
"title": "infiltration",
"type": "java.lang.String"
},
{
"collection": false,
"description": "",
"name": "key3",
"paramName": "schemeModelId",
"paramType": "BODY",
"required": true,
"title": "schemeModelId",
"type": "java.lang.String"
}
],
"method": "POST",
"name": "testRequestBodyTemplate",
"title": "请求体模板测试",
"outputs": [],
"requestBodyTemplate": "{\"argsArray\": [\"--inflow\", \"{inflow}\", \"--infiltration\", \"{infiltration}\"], \"schemeModelId\": \"{schemeModelId}\"}",
"requestBodyType": "TEMPLATE",
"url": "http://localhost:2834/testRequestBodyTemplate"
}
请求体模版参数填充示例:
备注:该http工具请求的响应体就是请求体。
请求体模板为:{"argsArray": ["--inflow", "{inflow}", "--infiltration", "{infiltration}"], "schemeModelId": "{schemeModelId}"}"
配置三个参数:inflow,infiltration,schemeModelId
参数值为:inflow=a,infiltration=b,schemeModelId=c。则将 {inflow}、{infiltration}、{schemeModelId} 分别替换为 a,b,c。

4) list 文件示例:
[
{
"authenticationType": "TOKEN",
"title": "通过 tolen 获取工具列表",
"inputs": [],
"method": "GET",
"name": "GetListByToken",
"outputs": [],
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/list.json?format=false"
},
{
"authenticationType": "TOKEN",
"title": "执行模型",
"method": "POST",
"name": "executeModel",
"url": "http://localhost:8090/iserver/services/geoprocessing/restjsr/gp/v2/sps.WorkflowProcessFactory.models.test:%E5%AF%BC%E5%87%BAtemp/jobs",
"inputs": [
{
"collection": false,
"name": "parameter",
"paramType": "BODY",
"children": [
{
"collection": false,
"name": "connectionInfo",
"required": true,
"title": "连接信息",
"type": "java.lang.String"
},
{
"collection": false,
"name": "targetFilePath",
"required": true,
"title": "文件路径",
"type": "java.lang.String"
}
]
},
{
"collection": false,
"name": "environments",
"required": true,
"title": "环境参数",
"paramType": "BODY",
"type": "com.alibaba.fastjson.JSON"
}
]
},
{
"authenticationType": "TOKEN",
"title": "通过任务ID获取日志",
"inputs": [
{
"collection": false,
"description": "",
"name": "logID",
"required": true,
"title": "任务ID",
"paramType": "PATH",
"type": "java.lang.String"
},
{
"collection": false,
"description": "",
"name": "levels",
"required": false,
"title": "日志等级",
"type": "java.lang.String"
},
{
"collection": false,
"description": "",
"name": "newStructure",
"required": false,
"title": "isNewStructure",
"type": "java.lang.Boolean"
}
],
"method": "GET",
"name": "getLogByID",
"url": "http://172.16.120.41:8090/iserver/services/geoprocessing/restjsr/gpmodeler/logs/{logID}"
}
]


1007

被折叠的 条评论
为什么被折叠?



