具有响应处理的WSO2 ESB(Apache Synapse)代理

时间:2013-12-12 20:08:09

标签: javascript proxy wso2 wso2esb synapse

再次,我。作为一项技术练习,我试图使用WSO2 ESB来代理一些网络流量。具体来说,我正在尝试代理网络流量并动态更改返回的响应,如下所示:

  • 让ESB收到HTTP请求
  • 将请求代理到特定服务器
  • 收到回复
  • 找到“sad”这个词的任何出现,并将其替换为“happy”(不区分大小写的正则表达式)
  • 将更改的回复传递回浏览器

有人会认为这是一个简单的正则表达式或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是正则表达式失败的对象。使用toString()将“contentObject”强制转换为字符串也不起作用。即使它确实有效,HTML内容仍然是转义形式,并且转换回来可能会有问题,因为HTML代码中可能存在可能需要保留HTML格式的转义条目。这里的最后一个问题是“TheAlteredContent”属性的内容被附加到内容而不是替换它,即使在最终的富集操作中添加了属性“action = replace”(ESB实际上将其删除)。

任何人都知道如何更好地实现这一目标,或者也许是一种使上述代码有效的方法?

1 个答案:

答案 0 :(得分:0)

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

这会解决您的问题吗?