【问题标题】:Mule ESB download file from URL stringMule ESB 从 URL 字符串下载文件
【发布时间】:2012-10-08 08:16:08
【问题描述】:

所以,使用 Mule ESB,我正在 Bing 中搜索某些 PDF 文件。然后我解析 JSON 响应以捕获文件位置的 URL。现在我需要检索文件并在本地保存。以下是我到目前为止所拥有的,但我有一种感觉,这一切都错了。如何完成用例?

我有两个问题:

1) 无法弄清楚如何从 #[message.payload.Url] 中去除“http”(因为 HTTP 端点将 http 添加到我传入的 url。

2) 不知道如何检索文件。我什至不知道 HTTP Endpoint 是否是正确的选择。 HTTP?文件?

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

<mule 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 ">
    <flow name="BingFlow1" doc:name="BingFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" 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"/>
        <http:outbound-endpoint exchange-pattern="request-response" host="#[message.payload.Url]" port="80" method="GET" doc:name="HTTP"/>
        <file:outbound-endpoint path="/home/user/Documents/output" outputPattern="#[message.payload.ID].pdf" responseTimeout="10000" doc:name="File"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>

【问题讨论】:

    标签: esb mule


    【解决方案1】:

    我无法测试流程,因为需要一些凭据,但以下内容应该对您有所帮助:

    • 使用 expression-transformer 去除 HTTP,
    • 您使用http:outbound-endpoint 后跟file:outbound-endpoint 的方法可以正常工作,
    • http:inbound-endpoint 更改为one-way:由于执行流被拆分,因此无法返回任何合理的值。

    例如,假设message.payload.Url 解析为java.lang.String,您可以使用:

    <expression-transformer expression="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://')]" doc:name="Expression"/>
    

    【讨论】:

    • 太棒了。我只是将入站端点更改为单向。我正在返回其余客户端,但你是对的......我已经将它回显到控制台,所以没有真正需要。
    • 您将如何编写表达式,我在集合拆分器之后添加了&lt;expression-transformer expression="#[message.payload.Url]" doc:name="Expression"/&gt;,但我不确定如何从 URL 中删除 http://。您能否提供一个示例甚至文档来说明如何做到这一点?我在 Mule 文档中没有看到它。
    • 没有特定的文档:这是 MEL(基于 mvel.codehaus.org),它允许您访问任何 Java 对象,包括在本例中用于处理字符串的 org.mule.util.StringUtils。我相应地审查了我的答案。
    • 使用脚本组件来做字符串操作怎么样?我只是不知道如何在不编写 java 类的情况下使用表达式转换器。
    • 完全不需要脚本组件来执行此操作。通过使用substringindexOfjava.lang.String 方法,您可以在不使用帮助类的情况下使用表达式转换器(但是为什么?)。
    猜你喜欢
    • 2011-03-15
    • 2013-12-06
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多