com.alogic.remote.xscript.CreateRequest Maven / Gradle / Ivy
package com.alogic.remote.xscript;
import com.alogic.remote.Client;
import com.alogic.remote.Request;
import com.alogic.remote.naming.ClientFactory;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.BaseException;
import com.anysoft.util.IOTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
/**
* 构造并定义一个Client请求
*
* @author yyduan
* @since 1.6.10.3
*
* @version 1.6.13.3 [20200422 duanyy]
* - method和clientId参数可动态获取;
*
* @version 1.6.13.6 [20200629 duanyy]
* - 增加rpc的debug信息的输出;
*/
public class CreateRequest extends NS {
/**
* 缺省的method
*/
public static final String DFT_METHOD = "POST";
/**
* 缺省的clientId
*/
public static final String DFT_CLIENT_ID = "default";
/**
* 输出对象的上下文id
*/
protected String cid = "remote-req";
/**
* 客户端id,从Context中获取
*/
protected String $clientId = "default";
/**
* method
*/
protected String $method = DFT_METHOD;
protected String $debug = "false";
public CreateRequest(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p){
super.configure(p);
cid = PropertiesConstants.getString(p, "cid", cid,true);
$clientId = PropertiesConstants.getString(p, "clientId", $clientId);
$method = PropertiesConstants.getRaw(p, "method", $method);
$debug = PropertiesConstants.getRaw(p,"debug",$debug);
}
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
String clientId = PropertiesConstants.transform(ctx,$clientId,DFT_CLIENT_ID);
Client client = ClientFactory.getCall(clientId);
if (client == null){
throw new BaseException("core.e1003","Can not find client :" + clientId);
}
Request req = null;
try {
req = client.build(
PropertiesConstants.transform(ctx,$method,DFT_METHOD),
PropertiesConstants.transform(ctx,$debug,false)
);
ctx.setObject(cid, req);
super.onExecute(root, current, ctx, watcher);
}finally{
ctx.removeObject(cid);
IOTools.close(req);
}
}
}