Skip to main content

#[0 diskutieren mit

Input json:

 

[{ email: "abcb@hcl.com", status : "NO Id Avalible" }, { email: "kharathi@hcl.com", status : "ID Avalible" }]

 

I need to generate html table response like below in dataweave 1. Can anyone help on this.

 

Expected Response:

<html><body><table border=1 cellpadding=1 cellspacing=1 style=width:500px> <tbody> <tr> <td style=text-align: center;>&nbsp;Email Id</td> <td style=text-align: center;>&nbsp;Update Status</td> </tr> <tr> <td>EMAIL</td> <td>STATUS</td> </tr></body></html> 

5 Antworten
  1. 17. Jan. 2020, 04:39

    Thanks @Rohit Reddy for your response.​ .

     

    But i am facing issue while implementing solution this into Dataweave.

     

    Message        : Exception while executing: 

              td @(style:"text-align: center;"): $.status,

                                ^

    Type mismatch for 'Value Selector' operator

       found :string, :name

     required :datetime, :name or

     required :localdatetime, :name or

     required :object, :name or

     required :time, :name or

     required :array, :name or

     required :date, :name or

     required :localtime, :name or

     required :period, :name.

    Payload        : { "email": "abcb@hcl.com", "status" : "NO Id Avalible" }

    Payload Type     : java.lang.String

     

    Flow XML is: [Attached XML file]

     

    <flow name="testerappFlow">

        <poll doc:name="Poll">

          <fixed-frequency-scheduler frequency="1" timeUnit="MINUTES"/>

          <set-payload value="{ "email": "abcb@hcl.com", "status" : "NO Id Avalible" }" doc:name="Set Payload"/>

        </poll>

        <set-variable variableName="payloadVar" value="#[]" doc:name="Variable"/>

        <expression-component doc:name="Expression"><![CDATA[flowVars.payloadVar.add(payload);]]></expression-component>

        <logger message="#flowVars ---- payload: #payload" level="INFO" doc:name="Logger"/>

        <dw:transform-message doc:name="Transform Message" metadata:id="8aad28c5-b6fd-456f-b409-870e8f92b1e5">

          <dw:input-variable doc:sample="sample_data\string_1.dwl" variableName="payloadVar"/>

          <dw:set-payload><![CDATA[%dw 1.0

    %output application/xml

    ---

    html :

      body :

        table @(border:1,cellpadding:1,

        cellspacing:1,style:"width:500px"):

          tbody : 

            tr : {

              td @(style:"text-align: center;"): "Email Id",

              td @(style:"text-align: center;"): "Update Status"

            } ++

            {(flowVars.payloadVar map {

            

              td @(style:"text-align: center;"): $.status,

              td @(style:"text-align: center;"): $.email

        

         }

            )}

            ]]></dw:set-payload>

        </dw:transform-message>

        <logger message="payload at End: #payload" level="INFO" doc:name="Logger"/>

      </flow>

0/9000

How to declare a variable and instantiate Arraylist & HashMap object to it in dataweave 2 (i tried using java:invoke its working but is there a way to declare in dataweave ?). i should use this var to accumulate objects in a for loop and use it later .

3 Antworten
  1. 4. Aug. 2021, 08:49

    @Ganesh Palani​ 

     

    What is the need of creating a Java object reference directly?

     

    If you just wants to accumulate the payloads from for loop, you can create an object a variable outside the for scope and append the values in for scope and use it later on.

     

    @Ganesh Palani​ What is the need of creating a Java object reference directly?Attaching example flow:

    <flow name="testFlow1" doc:id="73a70eb2-9d8e-46b1-be71-694620e8f115" >

    <set-variable value="#[[]]" doc:name="myVar" doc:id="91444a89-bb1e-4287-b628-59e740a8b98c" variableName="myVar"/>

    <foreach doc:name="For Each (1 to 10)" doc:id="1b420d4f-0bf3-4c15-937a-b852016e0f33" collection="#[1 to 10]">

    <set-variable value="#[vars.myVar + payload]" doc:name="myVar" doc:id="7f24b408-afc8-4b44-95c7-e38ff2a438ae" variableName="myVar" />

    </foreach>

    <logger level="INFO" doc:name="Log myVar" doc:id="c762d992-6f79-4391-a410-354419bd11a1" message="#[output application/json --- vars.myVars]"/>

    </flow>

    Regards,

    Chetan

0/9000

The case:

I have an array of hotel IDs (hotelList), for each hotel I need to make an API call so I have a parallel for each where I handle each hotels call. If one of the calls fail I want to save/collect that hotel ID in a list (errorList) so that I can tell the source system which hotels failed. However I can't seem to populate the variable errorList when a call fails, instead it seems the temporary payload inside the for each carries the value but its then lost once another hotel is processed.

 

For simplicity I have a test case where I simply try to populate the errorList when i find "5" in the hotel ID, the hotelList is a hardcoded variable here. This is the xml:

 

<flow name="testFlow" doc:id="7b226885-c819-46a2-ae53-a249481eb8cc" >

<scheduler doc:name="Scheduler" doc:id="c51bef69-c084-415d-a731-f4cb2fd3d8e4" >

<scheduling-strategy >

<fixed-frequency frequency="9000000" startDelay="5" timeUnit="MINUTES"/>

</scheduling-strategy>

</scheduler>

<set-variable value='#["123","456","789"]' doc:name='hotelList: ["123","456","789"]' doc:id="748d293f-7f97-4d0d-9d6e-c7b19af8d125" variableName="hotelList"/>

<set-variable value="#[]" doc:name="errorList: []" doc:id="2ae34c4a-d74f-4e13-b306-7b9706a5a4ff" variableName="errorList"/>

<parallel-foreach doc:name="Parallel For Each" doc:id="3c81e54d-8e33-420d-9683-9877ceb94919" collection="#vars.hotelList">

<choice doc:name="Choice" doc:id="ac78978a-7581-4f7e-81df-1c0df6067289">

<when expression='#(payload contains ("5"))'>

<ee:transform doc:name="vars.errorList << payload" doc:id="8d30fabf-2a70-43ba-abae-28351f6d68e9">

<ee:message>

<ee:set-payload><![CDATA[%dw 2.0

output application/java

---

vars.errorList << payload]]></ee:set-payload>

</ee:message>

<ee:variables>

</ee:variables>

</ee:transform>

</when>

<otherwise>

<logger level="INFO" doc:name="Logger" doc:id="7e8f344d-845a-4902-8220-532bc8e8a757" message="Hotel is normal" />

</otherwise>

</choice>

</parallel-foreach>

<logger level="INFO" doc:name="vars.errorList" doc:id="2e18d4e7-629b-4b7f-9316-e0161e9da441" message="#vars.errorList" />

</flow>

 

How can I populate an array (variable) in a Parallel for each?

9 Antworten
  1. 17. Aug. 2022, 14:54

    Yes, this happens because in the transform message you are setting the payload instead of the variable errorList. See the difference below. You will also notice i've added a default [] in the transformation. When you do it like this, there is no need to declare the set variable before the for each loop. (Since it will initialize itself as an empty array the first loop).

     

    Your transformation:

    Yes, this happens because in the transform message you are setting the payload instead of the variable errorList. See the difference below.Transformation as it should be:

    image

0/9000
5 Antworten
  1. 30. März 2022, 03:30

    Thank you Prathamesh, I resolved some syntax errors in your code <?xml version="1.0" encoding="UTF-8"?>

     

    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"

    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="

    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

    <flow name="uniquesqnnumberFlow" doc:id="34bf7919-6a30-40a3-be29-1eb76d4c3cb5" >

    <scheduler doc:name="Scheduler" doc:id="dd6cbd2e-0142-4177-a613-468266ab48c2" >

    <scheduling-strategy >

    <fixed-frequency frequency="2" timeUnit="MINUTES"/>

    </scheduling-strategy>

    </scheduler>

    <ee:transform doc:name="Transform Message" doc:id="c72cc7b8-a11f-4348-903f-d238b25ed59a" >

    <ee:message >

    <ee:set-payload ><![CDATA[%dw 2.0

    output application/json

    ---

    [

    {

    "name":"india"

    },

    {

    "name":"USA"

    }]

    ]]></ee:set-payload>

    </ee:message>

    </ee:transform>

    <set-variable value="#[]" doc:name="Set Variable" doc:id="44063b97-5699-473e-8178-15ecae31cbd8" variableName="FinalOutput"/>

    <foreach doc:name="For Each" doc:id="d201c3e7-d7b0-404f-b57f-ec803120a589" collection="#payload">

    <logger level="INFO" doc:name="Logger " doc:id="522ce86c-183d-45dd-95c5-0c1ace587713" message="#payload"/>

    <ee:transform doc:name="Transform Message" doc:id="e554634d-8eb8-4caf-b6a5-3b723dd1faf4" >

    <ee:message >

    </ee:message>

    <ee:variables >

    <ee:set-variable variableName="FinalOutput" ><![CDATA[%dw 2.0

    output application/json

    ---

    vars.FinalOutput + payload]]></ee:set-variable>

    </ee:variables>

    </ee:transform>

    </foreach>

    <logger level="INFO" doc:name="Logger" doc:id="45146aa4-6e7b-4fab-b789-d122d92a791e" message="#vars.FinalOutput"/>

    </flow>

    </mule>

0/9000

As part of settings we kept Total ttl=120 and Token ttl timeout unit= Seconds.

 

But after 120 seconds when client tries to use the expired access token, he is still able to by-pass the validation, where are our expectation is that Oauth2.0 validation should fail with error type 'OAUTH2-PROVIDER:TOKEN_UNAUTHORIZED'.

 

Please find below details configuration xml for reference.

 

Note:

As of now we are going with a workaround using component Revoke token, which we are applying it after specified time in async scope which is doing the job. But as per our understanding this is not an ideal solution.

 

Reference XML:

 

<mule

 xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"

 xmlns:oauth="http://www.mulesoft.org/schema/mule/oauth"

 xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"

 xmlns:os="http://www.mulesoft.org/schema/mule/os"

 xmlns:oauth2-provider="http://www.mulesoft.org/schema/mule/oauth2-provider"

 xmlns:http="http://www.mulesoft.org/schema/mule/http"

 xmlns="http://www.mulesoft.org/schema/mule/core"

 xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="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/oauth2-provider http://www.mulesoft.org/schema/mule/oauth2-provider/current/mule-oauth2-provider.xsd

http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd

http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd

http://www.mulesoft.org/schema/mule/oauth http://www.mulesoft.org/schema/mule/oauth/current/mule-oauth.xsd

http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">

 <http:listener-config name="HTTP_Listener_config"

  doc:name="HTTP Listener config"

  doc:id="778f937f-a80a-4060-be6c-8f5212c8b73b">

  <http:listener-connection host="0.0.0.0"

   port="8080" />

 </http:listener-config>

 <os:object-store name="client-os"

  doc:name="Object store" doc:id="91296a69-57d5-4766-ac76-ec1ff662acc8" />

 <os:object-store name="token-os"

  doc:name="Object store" doc:id="82a51666-3c83-4b32-9c06-32e97abb6ab6" />

 <oauth2-provider:config

  name="OAuth2_Provider_Config" doc:name="OAuth2 Provider Config"

  doc:id="66ea9233-dfeb-44af-bf79-8a2e01b56cbb"

  listenerConfig="HTTP_Listener_config" clientStore="client-os"

  supportedGrantTypes="CLIENT_CREDENTIALS">

  <oauth2-provider:client-validation-rate-limiter >

   <oauth2-provider:period-rate-limiter duration="60" />

  </oauth2-provider:client-validation-rate-limiter>

  <oauth2-provider:token-config tokenStore="token-os" tokenTtl="120" >

  </oauth2-provider:token-config>

 </oauth2-provider:config>

 <flow name="create-client" doc:id="8373609a-4a30-4662-bf4e-93a316d1619f" >

  <http:listener doc:name="Listener" doc:id="3371c037-6f19-4f07-adb8-fb8435dfa0bb" config-ref="HTTP_Listener_config" path="/create"/>

  <oauth2-provider:create-client doc:name="Create client" doc:id="d1a9fdf4-fe08-4bb9-9e39-264c3489bd39" config-ref="OAuth2_Provider_Config" clientId="#attributes.headers.client_id" type="CONFIDENTIAL" secret="#attributes.headers.client_secret" description='#""' principal='#""' redirectUris='#["abc.com"]' authorizedGrantTypes='#["CLIENT_CREDENTIALS"]' failIfPresent="true"/>

  <ee:transform doc:name="Transform Message" doc:id="fd369b5a-ccf6-47e2-9a54-507c4aa1d4d2" >

   <ee:message >

    <ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

"Client Created"]]></ee:set-payload>

   </ee:message>

  </ee:transform>

 </flow>

 <flow name="validate-token" doc:id="23febab6-588e-46fe-9487-82711b26f291" >

  <http:listener doc:name="Listener" doc:id="89b2d88f-7532-46bc-b9db-576bc6c8dd1b" config-ref="HTTP_Listener_config" path="/validate"/>

  <oauth2-provider:validate-token doc:name="Validate token" doc:id="ef60d50a-c3a9-497f-8f3f-49c6b992e317" config-ref="OAuth2_Provider_Config"/>

  <ee:transform doc:name="Transform Message" doc:id="06f69772-b600-407e-aa97-c5a796356af1">

   <ee:message>

    <ee:set-payload><![CDATA[%dw 2.0

output application/json

---

payload]]></ee:set-payload>

   </ee:message>

  </ee:transform>

 </flow>

</mule>

13 Antworten
  1. 22. Juli 2020, 13:40

    Correct, you could give it one final try by using the in-memory object store (uncheck v2 os store from runtime and set the config as non-persistent), I think then it will work.

     

    Using the in-memory object-store is not ideal because you will lose horizontal scaling.

    In either case, be aware of the object store v2 - 30 day expiry of your values if you continue using it.

0/9000

In the Oauth2 provider config settings I kept Token ttl=120 and Token ttl timeout unit= Seconds. 

But after 120 seconds the token is not getting expired, its still able to by-pass the validation, but I expect Invalid/expired token error.

I also tried setting in the Token object store Entry ttl as 30 sec, but still it does not work..

 

Help please.. I am stuck...

6 Antworten
0/9000

I have following code snippet:

 

<set-variable value="#[]" doc:name="Add Response"

 

doc:id="05bb1a62-3826-41dc-b4bb-8ef27e5b99cb"

 

variableName="responseArray" />

 

<foreach doc:name="For Each"

 

doc:id="1bf6620c-9156-4580-81e4-53f80d66d1da"

 

collection="#collectionOfRequests">

 

<wsc:consume operation="operation"

 

doc:name="Call Service" doc:id="6d44833e-9317-4dba-8011-7844d0e207dd"

 

config-ref="Web_Service_Consumer_Config" />

 

<set-variable value="#vars.responseArray &lt;&lt; (write(payload, 'application/xml'))" doc:name="Add Response" doc:id="a6516eff-a3c9-436c-85f5-79ce8be44608" variableName="addResponse" />  

 

</foreach>

 

The issue is, the response xml is not added to responseArray. I tried using dataweave but got the same issue.  

 

What is the right way to add a xml or json payload to an array so i can do an aggregation mapping?

2 Antworten
  1. 14. Juni 2020, 18:04

    Hi @Xin Liu​ ,

     

    Everything is fine just change the expression as following for your set variable, which you have inside foreach scope:

    #[vars.responseArray + payload]

    I hope it works for you.

     

    Regards,

     

    Lalit Panwar

0/9000

Input json:

 

[{ email: "abcb@hcl.com", status : "NO Id Avalible" }, { email: "kharathi@hcl.com", status : "ID Avalible" }]

 

I need to generate html table response like below in dataweave 1. Can anyone help on this.

 

Expected Response:

<html><body><table border=1 cellpadding=1 cellspacing=1 style=width:500px> <tbody> <tr> <td style=text-align: center;>&nbsp;Email Id</td> <td style=text-align: center;>&nbsp;Update Status</td> </tr> <tr> <td>EMAIL</td> <td>STATUS</td> </tr></body></html> 

2 Antworten
  1. 17. Jan. 2020, 04:39

    Thanks @Rohit Reddy for your response.​ .

     

    But i am facing issue while implementing solution this into Dataweave.

     

    Message        : Exception while executing: 

              td @(style:"text-align: center;"): $.status,

                                ^

    Type mismatch for 'Value Selector' operator

       found :string, :name

     required :datetime, :name or

     required :localdatetime, :name or

     required :object, :name or

     required :time, :name or

     required :array, :name or

     required :date, :name or

     required :localtime, :name or

     required :period, :name.

    Payload        : { "email": "abcb@hcl.com", "status" : "NO Id Avalible" }

    Payload Type     : java.lang.String

     

    Flow XML is: [Attached XML file]

     

    <flow name="testerappFlow">

        <poll doc:name="Poll">

          <fixed-frequency-scheduler frequency="1" timeUnit="MINUTES"/>

          <set-payload value="{ "email": "abcb@hcl.com", "status" : "NO Id Avalible" }" doc:name="Set Payload"/>

        </poll>

        <set-variable variableName="payloadVar" value="#[]" doc:name="Variable"/>

        <expression-component doc:name="Expression"><![CDATA[flowVars.payloadVar.add(payload);]]></expression-component>

        <logger message="#flowVars ---- payload: #payload" level="INFO" doc:name="Logger"/>

        <dw:transform-message doc:name="Transform Message" metadata:id="8aad28c5-b6fd-456f-b409-870e8f92b1e5">

          <dw:input-variable doc:sample="sample_data\string_1.dwl" variableName="payloadVar"/>

          <dw:set-payload><![CDATA[%dw 1.0

    %output application/xml

    ---

    html :

      body :

        table @(border:1,cellpadding:1,

        cellspacing:1,style:"width:500px"):

          tbody : 

            tr : {

              td @(style:"text-align: center;"): "Email Id",

              td @(style:"text-align: center;"): "Update Status"

            } ++

            {(flowVars.payloadVar map {

            

              td @(style:"text-align: center;"): $.status,

              td @(style:"text-align: center;"): $.email

        

         }

            )}

            ]]></dw:set-payload>

        </dw:transform-message>

        <logger message="payload at End: #payload" level="INFO" doc:name="Logger"/>

      </flow>

0/9000