Search code examples
xmlmuleesb

Transforming http response to xml


These are my first steps with Mule, so I may not understand some basic concepts.

There is a store running Prestashop, that exposes REST service.

All I want to do now, is to send request to store, get response, replace one value, and save it to a file.

The response looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<stock_available>
    <id><![CDATA[1]]></id>
    <id_product xlink:href="http://xxxxxxxxxxxxx/api/products/1"><![CDATA[1]]></id_product>
    <id_product_attribute><![CDATA[0]]></id_product_attribute>
    <id_shop xlink:href="http://xxxxxxxxxxxxx/api/shops/1"><![CDATA[1]]></id_shop>
    <id_shop_group><![CDATA[0]]></id_shop_group>
    <quantity><![CDATA[0]]></quantity>
    <depends_on_stock><![CDATA[0]]></depends_on_stock>
    <out_of_stock><![CDATA[2]]></out_of_stock>
</stock_available>
</prestashop>

I want to replace the value 'quantity'. For now - with a constant value. My current flow is:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.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/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="xxxxxxxxxxxxxxxx" port="80" basePath="/api/" doc:name="HTTP Request Configuration">
        <http:basic-authentication username="xxxxxxxxxxxxxxxxxxxxxxx" password="x"/>
        <http:raml-api-configuration location="api.raml"/>
    </http:request-config>
    <flow name="1Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/stock_availables/{id}" method="GET" doc:name="HTTP">
            <http:request-builder>
                <http:uri-param paramName="id" value="#[message.inboundProperties.'http.query.params'.id]"/>
            </http:request-builder>
        </http:request>
        <byte-array-to-object-transformer doc:name="Byte Array to Object" mimeType="text/xml"/>
        <mulexml:object-to-xml-transformer doc:name="Object to XML"/>
        <file:outbound-endpoint path="C:\test" outputPattern="test.txt" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

This already connecting to a service, gets response and saves file. BUT I don't know, how to modify it's content.

One of problem, that I cannot understand is: When I look into debugger, after "Byte Array To Object", the payload has type java.lang.string. After "Object to XML" the payload has still type java.lang.string, and the payload's value is :

<string>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;prestashop xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;stock_available&gt;..............blahblahblah.........</string>

When I try to use xslt transform it results with error.

How to modify that value?


Solution

  • You have a couple options here. First off your http transport will return you a byte array which you can then transform to a String using the byte array to string transformer. After that you can either call a custom processor and transform the string payload in the java code or try some combination of the regex function inside MEL (#[regex()]) and the string appender transformer to transform the payload completely within the mule xml code.