Mule ESB download file from URL string

大兔子大兔子 提交于 2019-12-08 01:24:34

问题


So, using Mule ESB, I'm searching Bing for certain PDF files. Then I'm parsing the JSON response to capture the URL of the file location. Now I need to retrieve the file and save locally. Below is what I have so far, but I have a feeling I'm going about this all wrong. How can I complete the use case?

I'm having two problems:

1) Can't figure out how to strip "http" from #[message.payload.Url] (since the HTTP Endpoint adds http to the url I'm passing in.

2) Can't figure out how to retrieve the file. I don't even know if HTTP Endpoint is the right option. HTTP? File?

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

回答1:


I couldn't test the flow because some credentials are needed but the following should help you:

  • Use an expression-transformer to strip the HTTP out,
  • Your approach with an http:outbound-endpoint followed by a file:outbound-endpoint will work fine,
  • Change the http:inbound-endpoint to one-way: there is no way to return anything sensible since the execution flow gets split.

For example, assuming message.payload.Url resolves to a java.lang.String, you can use:

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


来源:https://stackoverflow.com/questions/12945051/mule-esb-download-file-from-url-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!