带有响应处理的WSO2ESB(ApacheSynapse)代理
WSO2 ESB (Apache Synapse) Proxy with response processing
我又来了。作为一项技术练习,我尝试使用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在代理配置中制作整个复杂的编码逻辑要简单得多。。
这能解决你的问题吗?
相关文章:
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- servlet和代理servlet之间的区别
- 向localhost发出带有grunt的代理CORS请求
- PHP代理jQuery部分为javascript
- 代理一个网站能够在iframe中进行所见即所得编辑-它是如何工作的
- 在Chrome for iOS中,附加到用户代理字符串的唯一选项卡ID
- jQuery的等价物's$(this)在Ractive.js事件代理中
- WebRTC-是否可以将浏览器代理视频从源发送到接收器
- 使用nock模拟mocha中具有特定请求头的超级代理请求
- Apache2反向代理重定向
- 使用Node.js代理POST并修改头
- iframe的假用户代理
- 由于本地存储代理,Extjs网格面板最多显示25行.如何更改此限制值
- 将 ExtJS 代理 AJAX 响应映射到模型
- 使用不同参数的超级代理缓存缓存 HTTP 响应
- HTML - 如何在不同的用户代理中显示不同的图像
- 使用 HTTP 代理进行负载平衡
- NodeJs 代理连接
- 使用phantomjs在本地文件上使用extjs代理进行ajax调用
- 带有响应处理的WSO2ESB(ApacheSynapse)代理