Mule ESB动态设置端点路径

时间:2012-10-25 21:29:40

标签: esb mule

我正在尝试构建一个Web服务,该服务接受许多用于构建Bing查询API路径的变量。出于某种原因,如果我构建URI,我的流程会识别新的URI被适当地分配给有效负载,但是当我将有效负载分配给出站的HTTP端点时,它会失败。

<flow name="BingQuery" doc:name="BingQuery">
        <http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8082" doc:name="HTTP" />
       <set-variable variableName="query" doc:name="Variable" value="#[header:INBOUND:query]"/>
       <set-variable variableName="numResults" doc:name="Variable" value="#[header:INBOUND:top]"/>
       <set-variable variableName="offsetResults" doc:name="Variable" value="#[header:INBOUND:offset]"/>
       <scripting:component doc:name="Groovy">
           <scripting:script engine="Groovy">
               <scripting:text><![CDATA[def query = message.getInvocationProperty("query")
                def numResults = message.getInvocationProperty("numResults")
                def offsetResults = message.getInvocationProperty("offsetResults")
                def path = "Data.ashx/Bing/Search/v1/Web?Query=" + "$query" + "&amp;WebFileType=%27PDF%27&amp;\$top=" + "$numResults" + "&amp;\$skip=" + "$offsetResults" + "&amp;\$format=Json"
                    println "$path"
                    message.setProperty("pathVar","$path")
                    return null;]]></scripting:text>
           </scripting:script>
       </scripting:component> 
       <logger level="INFO" doc:name="Logger" message="#[header:OUTBOUND:pathVar]"/> 
        <https:outbound-endpoint exchange-pattern="request-response"
            host="api.datamarket.azure.com" port="443" path="#[header:OUTBOUND:pathVar]"
            user="*****" password="******"
            doc:name="Bing" /> ...

我得到的例外是

ERROR 2012-10-25 14:14:26,250 [[poll_directory].BingQuery.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to transform from "json" to "java.util.Map"
Code                  : MULE_ERROR-65110
--------------------------------------------------------------------------------
Exception stack is:
1. Unexpected character ('T' (code 84)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@73b62d67; line: 1, column: 2] (org.codehaus.jackson.JsonParseException)
 org.codehaus.jackson.JsonParser:1291 (null)
2. Failed to transform from "json" to "java.util.Map" (org.mule.api.transformer.TransformerException)
 org.mule.module.json.transformers.JsonToObject:136 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.codehaus.jackson.JsonParseException: Unexpected character ('T' (code 84)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@73b62d67; line: 1, column: 2]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
   + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

必须有一个简单的原因它不起作用,我确定这只是因为我还不知道Mule中的正确语法。我也知道使用MEL和Groovy可能有更好的方法来做到这一点,但我还不熟悉MEL这样做。

在发送之前,对User属性和Password属性的某些字符进行了编码。将该密钥放入变量中,然后传递给User&amp;密码属性可以防止这种情况并导致成功。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core"
    version="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd 
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
    <configuration doc:name="Configuration">
        <expression-language>
            <import class="org.mule.util.StringUtils" />
        </expression-language>
    </configuration>
    <flow name="BingQuery" doc:name="BingQuery">
        <http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8082" doc:name="HTTP" />
       <set-variable variableName="query" doc:name="Variable" value="#[header:INBOUND:query]"/>
       <set-variable variableName="numResults" doc:name="Variable" value="#[header:INBOUND:top]"/>
       <set-variable variableName="offsetResults" doc:name="Variable" value="#[header:INBOUND:offset]"/>
        <scripting:component doc:name="Groovy">
          <scripting:script engine="Groovy">
              <scripting:text><![CDATA[def query = message.getInvocationProperty("query")
               def numResults = message.getInvocationProperty("numResults")
               def offsetResults = message.getInvocationProperty("offsetResults")
               def path = "Data.ashx/Bing/Search/v1/Web?Query=" + "$query" + "&WebFileType=%27PDF%27&\$top=" + "$numResults" + "&\$skip=" + "$offsetResults" + "&\$format=Json"
                   println "$path"
                   message.setProperty("pathVar","$path")
                   message.setProperty("APIIdentifier","*****")
                   return null;]]></scripting:text>
          </scripting:script>
      </scripting:component>
        <https:outbound-endpoint exchange-pattern="request-response"
            host="api.datamarket.azure.com" port="443" path="#[header:OUTBOUND:pathVar]"
            user="#[header:OUTBOUND:APIIdentifier]" password="#[header:OUTBOUND:APIIdentifier]"
            doc:name="Bing" />
        <json:json-to-object-transformer
            returnClass="java.util.Map" doc:name="JSON to Object" />
        <expression-transformer expression="#[message.payload.d.results]"
            doc:name="Expression" />
        <collection-splitter doc:name="Collection Splitter" />
        <vm:outbound-endpoint exchange-pattern="one-way"
            path="fileWriter" doc:name="VM" />
    </flow>
    <flow name="RestProcessor" doc:name="RestProcessor">
        <vm:inbound-endpoint exchange-pattern="one-way"
            path="fileWriter" doc:name="VM" />
        <set-variable variableName="fileName" value="#[message.payload.ID].pdf"
            doc:name="Variable" />
        <http:rest-service-component
            serviceUrl="#[joinChar=message.payload.Url.contains('?')?'&amp;':'?' ; StringUtils.join(new String[]{message.payload.Url,(String)joinChar,'followRedirects=true'})]"
            httpMethod="GET">
           <http:error-filter> 
               <expression-filter expression="#[Integer.valueOf(message.inboundProperties['http.status']) &gt;= 400]"></expression-filter> 
           </http:error-filter>
        </http:rest-service-component>
        <file:outbound-endpoint path="/home/administrator/Documents"
            outputPattern="#[flowVars.fileName]" responseTimeout="10000"
            mimeType="application/pdf" doc:name="File" />
    </flow>
</mule>

1 个答案:

答案 0 :(得分:1)

这是用MEL重写的流程的开始(虽然我测试了测试端点而不是Bing,但它对我有用。)

请注意,我还在出站HTTPS端点上将方法设置为GET:

<flow name="BingQuery">
    <http:inbound-endpoint exchange-pattern="one-way"
        host="localhost" port="8082" />
    <set-variable variableName="query"
        value="#[message.inboundProperties.query]" />
    <set-variable variableName="numResults"
        value="#[message.inboundProperties.top]" />
    <set-variable variableName="offsetResults"
        value="#[message.inboundProperties.offset]" />
    <expression-component>
        flowVars['bingQuery'] =
            'Data.ashx/Bing/Search/v1/Web?Query=' + query
          + '&amp;WebFileType=%27PDF%27&amp;$top=' + numResults
          + '&amp;$skip=' + offsetResults
          + '&amp;$format=Json';
        payload = null;
    </expression-component>
    <logger level="ERROR" message="#[bingQuery]" />
    <http:outbound-endpoint exchange-pattern="request-response"
        method="GET"
        host="api.datamarket.azure.com" port="443" path="#[bingQuery]"
        user="*****" password="******" />

如果流程中的其他位置未使用querynumResultsoffsetResults变量,我建议在expression-component中直接使用用于创建它们的表达式。< / p>