com.alogic.remote.xscript.ExecuteRequest Maven / Gradle / Ivy
package com.alogic.remote.xscript;
import com.anysoft.util.*;
import org.apache.commons.lang3.StringUtils;
import com.alogic.remote.Request;
import com.alogic.remote.Response;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
/**
* 执行Request请求
* @author yyduan
* @since 1.6.10.3
*
* @since 1.6.17.11 [20230614 duanyy]
* - remote相关插件强行消费http response内容,避免连接不能重用
*
* @version 1.6.17.13 [20230725 duanyy]
* - 通过reuse参数定制连接是否被重用
*/
public class ExecuteRequest extends Segment {
/**
* Response对象的上下文id
*/
protected String cid = "remote-res";
/**
* Request对象的上下文id
*/
protected String pid = "remote-req";
/**
* 服务路径
*/
protected String path = "";
/**
* 调用key
*/
protected String key = "";
protected boolean reuse = false;
public ExecuteRequest(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p){
super.configure(p);
cid = PropertiesConstants.getString(p, "cid", cid,true);
pid = PropertiesConstants.getString(p, "pid", pid,true);
path = PropertiesConstants.getRaw(p,"path",path);
key = PropertiesConstants.getRaw(p,"key",key);
reuse = PropertiesConstants.getBoolean(p,"reuse",reuse,true);
}
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
Request req = ctx.getObject(pid);
if (req == null){
throw new BaseException("core.e1001","It must be in a remote-request context,check your script.");
}
Response response = null;
try {
String p = ctx.transform(path);
if (StringUtils.isEmpty(p)){
throw new BaseException("core.e1003","The url path to call is null.");
}
String k = ctx.transform(key);
k = StringUtils.isEmpty(k)?KeyGen.uuid(10, 0, 9) : k;
response = req.execute(p, k, ctx);
ctx.setObject(cid, response);
super.onExecute(root, current, ctx, watcher);
}finally{
ctx.removeObject(cid);
if (response != null){
response.discard();
if (!reuse){
IOTools.close(response);
}
}
}
}
}