英文:
Resolve environment variables embedded in a xml file saved in the registry in WSO2 MI
问题
I am using the Micro Integrator 4.1.0 of WSO2.
我正在使用WSO2的Micro Integrator 4.1.0版本。
I have added environment variables in my server (a pod in Kubernetes and I add the variables in a file that the pod read when it is deploying) and it's working fine with sequences (using $SYSTEM:variable_name) and with endpoints ($SYSTEM too).
我已经在我的服务器中添加了环境变量(一个在Kubernetes中的Pod,我将这些变量添加到一个文件中,该文件在部署时由Pod读取),并且它在序列(使用$SYSTEM:variable_name)和终端点($SYSTEM也适用)中正常工作。
My problem is that I have tried to add a environment variable in a simple XML file saved in a Registry project but it doesn't work.
我的问题是,我尝试在一个保存在Registry项目中的简单XML文件中添加环境变量,但它不起作用。
I have tried the next 2 options:
我尝试了以下两个选项:
<root>
<Backend>
<usuario>$SYSTEM:USER</usuario>
<password>$SYSTEM:PASS</password>
</Backend>
</root>
<root>
<Backend>
<usuario>${USER}</usuario>
<password>${PASS}</password>
</Backend>
</root>
When I read the file in a sequence the value that I am getting is the hardcoded value (for example: ${USER})
当我在一个序列中读取文件时,我得到的值是硬编码的值(例如:${USER})。
Is that possible or exists any trick?
这是否可能或存在任何诀窍?
英文:
I am using the Micro Integrator 4.1.0 of WSO2.
I have added environment variables in my server (a pod in Kubernetes and I add the variables in a file that the pod read when it is deploying) and it's working fine with sequences (using $SYSTEM:variable_name) and with endpoints ($SYSTEM too).
My problem is that I have tried to add a environment variable in a simple XML file saved in a Registry project but it doesn't work.
I have tried the next 2 options:
<root>
<Backend>
<usuario>$SYSTEM:USER</usuario>
<password>$SYSTEM:PASS</password>
</Backend>
</root>
-
<root>
<Backend>
<usuario>${USER}</usuario>
<password>${PASS}</password>
</Backend>
</root>
When I read the file in a sequence the value that I am getting is the hardcoded value (for example: ${USER})
Is that possible or exists any trick?
答案1
得分: 2
这是不可能的。由于这只是一个注册表文件,MI在部署过程中不会解析变量。相反,您需要按顺序读取系统变量并进行调解。按顺序读取文件的要求是什么?如果计划将上述文件中的XML内容发送到端点,您可以使用载荷工厂动态生成所需的请求正文。
英文:
This is not possible. Since this is just a registry file, MI won't resolve the variables during the deployment. Instead, you will need to read the system variables in the sequence and carry out the mediation. What is the requirement to read the file in the sequence? If are planning to send the XML content in the above file to an endpoint, you can use the payload factory to generate the required request body dynamically.
答案2
得分: 2
正如Sanoj提到的,你可以通过使用PL Factory中介解决这个问题。但既然你要求一个技巧,这里有一个巧妙的方法
你可以使用脚本中介来实现类似下面的操作。
有效载荷
<root>
<Backend>
<usuario>{USER}</usuario>
<password>{PASS}</password>
</Backend>
</root>
脚本
<script language="js"><![CDATA[
var pl = mc.getPayloadXML().toString();
var props = pl.match(new RegExp(/\{(.*)\}/g));
for(var i = 0; i < props.length; i++) {
var proName = props[i].replace('{', '').replace('}', '').trim();
pl = pl.replace(props[i], java.lang.System.getenv(proName));
}
mc.setPayloadXML(new XML(pl));
]]>
</script>
我懒得遍历有效载荷作为XML树,选择了基于正则表达式的解决方案。我相信你明白这里的思路,所以你可以在这基础上进行扩展。你也可以考虑编写一个类中介。
英文:
As Sanoj mentioned you can solve this with a PL Factory mediator. But since you asked for a trick, here is a hacky way to do it
You can do something like the below with Script Mediator.
The Payload
<root>
<Backend>
<usuario>{USER}</usuario>
<password>{PASS}</password>
</Backend>
</root>
Script
<script language="js"><![CDATA[
var pl = mc.getPayloadXML().toString();
var props = pl.match(new RegExp(/\{(.*)\}/g));
for(var i = 0; i < props.length; i++) {
var proName = props[i].replace('{', '').replace('}', '').trim();
pl = pl.replace(props[i], java.lang.System.getenv(proName));
}
mc.setPayloadXML(new XML(pl));
]]>
</script>
I was too lazy to traverse the Payload as a XML tree and went for a regex based solution. I believe you get the idea here, so you can build on top of this. You can consider writing a Class Mediator as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论