【问题标题】:Mule file download with HTTP Endpoint使用 HTTP 端点下载 Mule 文件
【发布时间】:2012-10-10 13:41:24
【问题描述】:

我负责在流程结束时下载文件的 HTTP 端点出错了。它一直在尝试与http://:80/ 进行通信,而不是传入的 URL。我在这里做错了什么?

<?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>

【问题讨论】:

  • 之后记录的内容:“有效负载为:”?

标签: mule


【解决方案1】:

我无法发现错误:您的流程看起来不错(除了我将在下面详述的问题),因此,除了使用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>

【讨论】:

  • 很棒的解决方案,但是 rest-service-component 不做 followRedirects?我将尝试过滤掉任何不以 .pdf 结尾的内容,看看是否可行。它在第三个结果中死亡,因为它是对 pdf 的重定向。
  • 哦,没问题:rest-service-component 默认配置为拒绝任何不是 20 倍的东西,让我看看我是否能找到配置来跟踪重定向。
  • OK 查看了我的回答以支持重定向。请注意,我还在 MEL 中全局导入了 org.mule.util.StringUtils,以稍微简化语法。
  • 我编辑了我的问题以显示我遇到的异常。这次我得到了6个文件。比以前更多。
  • 这很奇怪但并非不可能:尝试在分流器之后将流切割成两个流。将它们与单向 VM 端点连接。这应该可以隔离故障。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2012-04-19
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
相关资源
最近更新 更多