2012-10-19 49 views
1

負責在流程結束時下載文件的HTTP端點正在出錯。它不斷嘗試與http://:80/進行通信,而不是傳入的URL。我在這裏做錯了什麼?使用HTTP端點下載Mule文件

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

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" 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/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.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 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd "> 
    <flow name="BingFlow1" doc:name="BingFlow1"> 
     <http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/> 
     <https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&amp;WebFileType=%27PDF%27&amp;$top=50&amp;$format=Json" user="*****" password="*****" 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"/> 
     <expression-transformer expression="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://')]" doc:name="Expression"/> 
     <logger message="Payload is: #[message.payload]" level="INFO" doc:name="Logger"/> 
     <http:outbound-endpoint exchange-pattern="request-response" host="#[org.mule.util.StringUtils.substringBefore(message.payload, '/')]" port="80" method="GET" doc:name="HTTP" contentType="application/pdf" mimeType="application/pdf" path="#[org.mule.util.StringUtils.substringAfter(message.payload, '/')]"/> 
     <file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[org.mule.util.StringUtils.replace(message.payload, '/','.')]" path="/home/user/Documents/output" mimeType="application/pdf"/> 
    </flow> 
</mule> 

當改爲第一個答案匹配,我得到這個異常:

******************************************************************************** 
Message    : Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHas 
hMap 
Code     : MULE_ERROR--2 
-------------------------------------------------------------------------------- 
Exception stack is: 
1. Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHashMap (org.mule.transp 
ort.http.components.RestServiceException) 
    org.mule.transport.http.components.RestServiceWrapper:219 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/http/components/RestServiceException.html) 
-------------------------------------------------------------------------------- 
Root Exception stack trace: 
org.mule.transport.http.components.RestServiceException: Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Mess 
age payload is of type: LinkedHashMap 
    at org.mule.transport.http.components.RestServiceWrapper.doInvoke(RestServiceWrapper.java:219) 
    at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:126) 
    at org.mule.component.AbstractComponent.access$000(AbstractComponent.java:61) 
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything) 
******************************************************************************** 

---解決---

非常感謝大衛。這是我的最終解決方案:

<?xml version="1.0" encoding="UTF-8"?> 
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:file="http://www.mulesoft.org/schema/mule/file" 
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" 
    xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns="http://www.mulesoft.org/schema/mule/core" 
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
    xmlns:spring="http://www.springframework.org/schema/beans" 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.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" /> 
     <https:outbound-endpoint exchange-pattern="request-response" 
      host="api.datamarket.azure.com" port="443" 
      path="Data.ashx/Bing/Search/v1/Web?Query=%27california%20school%20district%20contract%27&amp;WebFileType=%27PDF%27&amp;$top=10&amp;$format=Json" 
      user="*****" password="*****" 
      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" 
      doc:name="VM" path="fileWriter" /> 
    </flow> 
    <flow name="RestProcessor" doc:name="RestProcessor"> 
     <vm:inbound-endpoint exchange-pattern="one-way" 
      doc:name="VM" path="fileWriter" /> 
     <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 responseTimeout="10000" 
      doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/ken/Documents/output" 
      mimeType="application/pdf" /> 
    </flow> 
</mule> 
+0

後記錄的內容: 「有效載荷是:」? –

+0

URL減去'http://' – Thaneofife

回答

4

我不能當場錯誤:您的流量看起來不錯(除了一個問題,我將在下面詳細),因此,可選擇使用的http:outbound-endpoint,它有時可以是挑剔的,你可以使用http:rest-service-component

您的下一個問題將出現在文件端點的outputPattern中:請記住,此時message.payload將是來自HTTP端點而不是(部分)URL的響應。您需要預先計算文件名並將其存儲在流變量中,然後使用它。

這將使:

<configuration> 
    <expression-language> 
    <import class="org.mule.util.StringUtils" /> 
    </expression-language> 
</configuration> 

<flow name="BingFlow1" doc:name="BingFlow1"> 
    <http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/> 
    <https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&amp;WebFileType=%27PDF%27&amp;$top=50&amp;$format=Json" user="*****" password="*****" 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"/> 
    <set-variable variableName="fileName" value="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://').replace('/','.')]" /> 
    <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]" /> 
     </http:error-filter> 
    </http:rest-service-component> 
    <file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/user/Documents/output" mimeType="application/pdf"/> 
</flow> 
+0

真棒解決方案,但其餘的服務組件不會做followRedirects?我會盡力濾除任何不以.pdf結尾的內容,並查看是否有效。它在第三個結果上死去,因爲它是重定向到pdf。 – Thaneofife

+0

哦,沒問題:'rest-service-component'默認配置爲拒絕任何不是20x的東西,讓我看看我能否找到配置來遵循重定向。 –

+1

確定檢查了我的答案以支持重定向。注意我還在MEL中全局導入了'org.mule.util.StringUtils'來稍微減輕語法。 –