解析嵌入在注册表中的 XML 文件中的环境变量,这是在 WSO2 MI 中的。

huangapple go评论58阅读模式
英文:

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:

&lt;root&gt;
    &lt;Backend&gt;
        &lt;usuario&gt;$SYSTEM:USER&lt;/usuario&gt;
        &lt;password&gt;$SYSTEM:PASS&lt;/password&gt;
    &lt;/Backend&gt;
&lt;/root&gt;

-

&lt;root&gt;
    &lt;Backend&gt;
        &lt;usuario&gt;${USER}&lt;/usuario&gt;
        &lt;password&gt;${PASS}&lt;/password&gt;
    &lt;/Backend&gt;
&lt;/root&gt;

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中介解决这个问题。但既然你要求一个技巧,这里有一个巧妙的方法 解析嵌入在注册表中的 XML 文件中的环境变量,这是在 WSO2 MI 中的。

你可以使用脚本中介来实现类似下面的操作。

有效载荷

&lt;root&gt;
    &lt;Backend&gt;
        &lt;usuario&gt;{USER}&lt;/usuario&gt;
        &lt;password&gt;{PASS}&lt;/password&gt;
    &lt;/Backend&gt;
&lt;/root&gt;

脚本

&lt;script language=&quot;js&quot;&gt;&lt;![CDATA[ 
	var pl = mc.getPayloadXML().toString();
	var props = pl.match(new RegExp(/\{(.*)\}/g));
	for(var i = 0; i &lt; props.length; i++) {
		var proName = props[i].replace(&#39;{&#39;, &#39;&#39;).replace(&#39;}&#39;, &#39;&#39;).trim();
		pl = pl.replace(props[i], java.lang.System.getenv(proName));       
	}
	mc.setPayloadXML(new XML(pl));
	]]&gt;
&lt;/script&gt;

我懒得遍历有效载荷作为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 解析嵌入在注册表中的 XML 文件中的环境变量,这是在 WSO2 MI 中的。

You can do something like the below with Script Mediator.

The Payload

&lt;root&gt;
    &lt;Backend&gt;
        &lt;usuario&gt;{USER}&lt;/usuario&gt;
        &lt;password&gt;{PASS}&lt;/password&gt;
    &lt;/Backend&gt;
&lt;/root&gt;

Script

&lt;script language=&quot;js&quot;&gt;&lt;![CDATA[ 
	var pl = mc.getPayloadXML().toString();
	var props = pl.match(new RegExp(/\{(.*)\}/g));
	for(var i = 0; i &lt; props.length; i++) {
		var proName = props[i].replace(&#39;{&#39;, &#39;&#39;).replace(&#39;}&#39;, &#39;&#39;).trim();
		pl = pl.replace(props[i], java.lang.System.getenv(proName));       
	}
	mc.setPayloadXML(new XML(pl));
	]]&gt;
&lt;/script&gt;

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.

huangapple
  • 本文由 发表于 2023年3月9日 22:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定