1

又是我。作为一项技术练习,我尝试使用 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”

任何人都知道如何更好地做到这一点,或者让上述代码工作的方法?

4

1 回答 1

0

I believe that, you are receiving HTML response from the backend. Simply use a custom class mediator in the outpath and change the content. I mean, do a String.replace in the mediate() code. It would be fairly simple than making whole complex coding logic in a proxy configuration using a javascript..

Will that solve your issue?

于 2013-12-13T16:48:32.843 回答