提供基础层次上的的协议类型转换、XML<->Java转换、编码、压缩、加密转换。业务级转换由用户实现。
几个可能用到的转换器
自定义转换器
下面是发布一个接受string参数的ws,将参数转换成对象后调用并传递参数另一个ws的例子。
mule配置文件
<!-- 声明转换器 --> <custom-transformer name="stringToUser" class="com.oecp.mule.umo.helloworld.StringToUserObject" /> <custom-transformer name="objectToString" class="com.oecp.mule.umo.helloworld.ObjectToString"></custom-transformer> <flow name="helloworld"> <!--绑定入站地址--> <inbound-endpoint address="http://localhost:7007/services/Echo" exchange-pattern="request-response"/> <cxf:jaxws-service serviceClass="com.oecp.mule.umo.helloworld.HelloWorld"/> <!--指定处理单元--> <component class="com.oecp.mule.umo.helloworld.HelloWorldImpl" /> <!--参数转换 --> <transformer ref="stringToUser"/> <!--出站--> <outbound-endpoint address="http://localhost:65082/services/EchoUMO" responseTransformer-refs="objectToString"> <!--ws客户端--> <cxf:jaxws-client clientClass="com.oecp.wsclient.echo.EchoService" operation="getPower" port="EchoPort" wsdlLocation="classpath:EchoUMO.wsdl"> </cxf:jaxws-client> </outbound-endpoint> </flow>
转换器 ToUserObject.java
package org.mule.example.transformer; import org.mule.api.transformer.TransformerException; import org.mule.example.echo.User; import org.mule.transformer.AbstractTransformer; import org.mule.transformer.types.DataTypeFactory; public class ToUserObject extends AbstractTransformer { public ToUserObject(){ this.registerSourceType(DataTypeFactory.STRING); this.registerSourceType(DataTypeFactory.create(User.class)); } @Override protected Object doTransform(Object src, String encoding) throws TransformerException { String s[] = ((String)src).split(","); User u = new User(); u.setUid(s[0]); return u; } }
这种转换,必须要在服务器端有对应类型才能注册并转换,调用ws还要通过wsdl2java反向生成java文件部署后才能使用,比较繁琐。平台应用的话易用性比差一些。
动态脚本转换器
<!--代理模式测试。添加入站出战转换器--> <ws:proxy name="weather-forecast-ws-proxy-transformers" inboundAddress="http://localhost:8090/weather-forecast/4" transformer-refs="object-to-string-transformer zip-code-changer-1" responseTransformer-refs="object-to-string-transformer zip-code-changer-2" outboundEndpoint-ref="target-ws-endpoint" /> <!-- 转换器--> <object-to-string-transformer name="object-to-string-transformer" /> <script:transformer name="zip-code-changer-1"> <script:script engine="groovy"> println("============================zip-code-changer-1=================================") result = org.apache.commons.lang.StringEscapeUtils.unescapeHtml("$payload") </script:script> </script:transformer> <script:transformer name="zip-code-changer-2"> <script:script engine="groovy"> println("============================zip-code-changer-2=================================") result = org.apache.commons.lang.StringEscapeUtils.unescapeHtml("$payload") </script:script> </script:transformer> <!--简单服务--> <simple-service name="weather-forecaster-ws" address="http://localhost:6090/weather-forecast" component-class="org.mule.test.integration.tck.WeatherForecaster" type="jax-ws" />
动态脚本内可以连接数据库读取配置信息,调用java类解析wsdl文件,拼装成对应的soap消息信封。
xslt转换器
通过配置XSLT模板对数据进行数据格式和数据的转换
<xml-entity-decoder-transformer name="XmlDecoder" /> <xm:xslt-transformer name="XsltRest" xsl-file="xsl/rest-stock.xsl" /> <xm:xml-to-object-transformer name="XmlToObject" /> <flow name="HTTP input"> <http:inbound-endpoint host="localhost" port="48309" path="stockquote"> <http:body-to-parameter-map-transformer /> <response> <object-to-string-transformer /> </response> </http:inbound-endpoint> <flow-ref name="REST"/> <transformer ref="XmlToObject" /> </flow> <flow name="REST"> <transformer ref="ExtractSymbol" /> <http:rest-service-component serviceUrl="http://www.webservicex.net/stockquote.asmx/GetQuote" httpMethod="POST"> <http:payloadParameterName value="symbol" /> </http:rest-service-component> <transformer ref="XmlDecoder" /> <transformer ref="XsltRest" /> </flow>
xslt模板
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="2.0" xmlns:sq="http://www.webserviceX.NET/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/sq:string/sq:StockQuotes/sq:Stock"> <org.mule.example.stockquote.StockQuote> <name><xsl:value-of select="sq:Name"/></name> <symbol><xsl:value-of select="sq:Symbol"/></symbol> <date><xsl:value-of select="sq:Date"/><xsl:text> </xsl:text><xsl:value-of select="sq:Time"/></date> <change><xsl:value-of select="sq:Change"/></change> <last><xsl:value-of select="sq:Last"/></last> <open><xsl:value-of select="sq:Open"/></open> <high><xsl:value-of select="sq:High"/></high> <low><xsl:value-of select="sq:Low"/></low> <volume><xsl:value-of select="sq:Volume"/></volume> <previousClose><xsl:value-of select="sq:PreviousClose"/></previousClose> </org.mule.example.stockquote.StockQuote> </xsl:template> </xsl:stylesheet> 摘自http://prj.oecp.cn/projects/oecp-platform/wiki/%E8%BD%AC%E6%8D%A2%E5%99%A8