com.alogic.xscript.plugins.Check Maven / Gradle / Ivy
package com.alogic.xscript.plugins;
import org.apache.commons.lang3.StringUtils;
import com.alogic.validator.Validator;
import com.alogic.validator.ValidatorFactory;
import com.alogic.xscript.AbstractLogiclet;
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.Properties;
import com.anysoft.util.PropertiesConstants;
/**
* 检查某个参数是否存在
*
* @author duanyy
* @version 1.6.8.14 [20170509 duanyy]
* - 增加xscript的中间文档模型,以便支持多种报文协议
*
* @version 1.6.12.11 [20181206 duanyy]
* - 增加validator功能
*
* @version 1.6.12.13 [20181213 duanyy]
* - 修正错误代码
*/
public class Check extends AbstractLogiclet{
protected String arguId;
protected String $code = "clnt.e2000";
protected String $reason = "";
protected String validatorId = null;
public Check(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p) {
super.configure(p);
arguId = PropertiesConstants.getString(p,"id", arguId,true);
$code = PropertiesConstants.getRaw(p,"code", $code);
$reason = PropertiesConstants.getRaw(p,"reason", $reason);
validatorId = PropertiesConstants.getString(p,"validator",validatorId,true);
}
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx,
ExecuteWatcher watcher) {
if (StringUtils.isNotEmpty(arguId)){
String value = ctx.GetValue(arguId, "");
if (StringUtils.isEmpty(value)){
throw new BaseException(
PropertiesConstants.transform(ctx, $code, "clnt.e2000"),
PropertiesConstants.transform(ctx, $reason, "Can not find parameter:" + arguId)
);
}
if (StringUtils.isNotEmpty(validatorId)){
Validator validator = ValidatorFactory.getDefault().load(validatorId, true);
if (validator != null){
if (!validator.check(value, false)){
throw new BaseException(validator.getCode(),
String.format("Failed to validate parameter [%s=%s],Reason:%s",
arguId,value,validator.getMessage()));
}
}
}
}
}
}