【问题标题】:WSO2 ESB (Apache Synapse) Proxy with response processing具有响应处理的 WSO2 ESB (Apache Synapse) 代理
【发布时间】:2013-12-31 10:30:15
【问题描述】:

又是我。作为一项技术练习,我尝试使用 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 实际上删除它)。

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

【问题讨论】:

    标签: javascript proxy wso2 wso2esb synapse


    【解决方案1】:

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

    这会解决您的问题吗?

    【讨论】:

    • 可能,我得调查一下。确实,这不是一个问题,而是我分配给自己以更好地学习 ESB 的任务。
    猜你喜欢
    • 2016-04-15
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多