带有响应处理的WSO2ESB(ApacheSynapse)代理

WSO2 ESB (Apache Synapse) Proxy with response processing

本文关键字:ApacheSynapse 代理 WSO2ESB 响应 处理      更新时间:2023-09-26

我又来了。作为一项技术练习,我尝试使用WSO2ESB来代理一些web流量。具体来说,我正在尝试代理网络流量,并在飞行中更改返回的响应,如下所示:

  • 让ESB接收HTTP请求
  • 将请求代理到特定服务器
  • 接收响应
  • 查找任何出现的单词"sad",并将其替换为"happy"(不区分大小写的regex)
  • 将更改后的响应传回浏览器

有人会认为这是一个简单的regex或XSLT操作,但事实证明这比我想象的要困难得多。目前,这是我正在使用的代理脚本。。。

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="PassProxy"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description>Route content from a web server through the ESB service and alter it</description>
   <target>
      <endpoint>
         <address uri="http://server.yoyodyne.com/"/>
      </endpoint>
      <inSequence/>
      <outSequence>
         <property name="TheContentIncludingTheSoapEnvelope" expression="."/>
         <property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns="http://org.apache.synapse/xsd"
                   name="TheContentFromSoapBodyButNotReally"
                   expression="//soapenv:Envelope/soapenv:Body/*"/>
         <property name="TheContent"
                   value="An initial value that should be replaced"
                   scope="default"
                   type="STRING"/>
         <enrich>
            <source type="body" clone="true"/>
            <target type="property" property="TheContent"/>
         </enrich>
         <property name="ContentType" expression="$trp:Content-Type"/>
         <property name="ContentLength" expression="$trp:Content-Length"/>
         <log level="custom">
            <property name="ContentType" expression="$trp:Content-Type"/>
            <property name="ContentLength" expression="$trp:Content-Length"/>
            <property name="MessageVar" value="TheContent"/>
            <property name="TargetMessage" expression="get-property('TheContent')"/>
         </log>
         <script language="js">
            //hack because xpath fn:replace does not work in property tags and fn:translate works on chars not whole strings
            var contentType=mc.getProperty('ContentType');
            var contentObject=mc.getProperty('TheContent'); //how to get the text of this? And do it in un-escaped format???
            if(/text/i.test(contentType)) {
                if(!contentObject) {
                    mc.setProperty('TheAlteredContent','Well that didn''t work as expected');
                } else {
                    if(typeof contentObject == 'object' || typeof contentObject == 'undefined') {
                        var returnMessage='';
                        for (var key in contentObject) {
                            returnMessage=returnMessage+'found key "'+key+'"'n';
                        } //end object key for
                        returnMessage='Can''t work with this type of input - '+typeof contentObject+'n'Available keys to try:'n'+returnMessage;
                        contentObject=returnMessage;
                    } else {
                        contentObject=contentObject.replaceAll('sad', 'happy');
                        //more regex statements to go here
                    } //end typeof if
                } //end property check if
            } else {
                //not text - do nothing
                contentObject='binary content (I think). Found content type of "'+contentType+'"';
            } //end content type check if
            //send the content back
            mc.setProperty('TheAlteredContent',contentObject);
         </script>
         <enrich>
            <!-- need to figure out how to replace the content and not append to the end of it. Replace tag on target keeps getting removed for some reason -->
            <source type="property" property="TheAlteredContent" clone="true"/>
            <target type="body"/>
         </enrich>
         <!-- doctype headers in the HTML cause logging to fail, so no debugging for you -->
         <!--<log level="full"/>-->
         <send/>
      </outSequence>
   </target>
</proxy>

诚然,使用丰富操作可能不是处理这一问题的最佳方式,但在当时这似乎是个好主意。最终发生的情况是,响应的HTML部分作为带有转义内容的对象被传递到JS代码中(或者被传递出去??)。因为"contentObject"var是一个对象,所以regex会失败。使用toString()将"contentObject"强制为字符串也不起作用。即使它确实起作用,HTML内容仍然是转义形式,并且考虑到HTML代码中可能存在需要保持HTML格式的转义条目,因此转换回可能会有问题。这里的最后一个问题是,"TheAlteredContent"属性的内容被附加到内容中,而不是替换它,即使在最后的丰富操作中添加了属性"action=replace"(ESB实际上删除了它)。

有人知道如何更好地做到这一点,或者也许是让上述代码发挥作用的方法吗?

我相信,您正在从后端接收HTML响应。只需在outpath中使用自定义类中介器并更改内容。我的意思是,在mediate()代码中执行String.replace。这比使用javascript在代理配置中制作整个复杂的编码逻辑要简单得多。。

这能解决你的问题吗?