How to convert Object to String in mule esb? - mule

I am working with Mule ESB and our goal is to use dropbox as a end point connector means to store data to dropbox(from CSV/doc or any other data source). We are able to store data to drop box but data stored in the form of object and that is not in readable format so how to convert object to string so that it can be in readable format ?

Mule already has Object to String transformer to transform String from Object :- https://developer.mulesoft.com/docs/display/current/Transformers

What I think you're after is called object serialization, and you have many choices about how to do it in a way that people can read it.
Perhaps the oldest way in java (which is the language Mule is based on) is to implement a toString() method and format your object's data as you like. Of course, this means your message payload must be an instance of a class you can make changes to. If you choose this method, you can simply add an <object-to-string-transformer /> as Anirban suggested.
Some common ways people do this, especially with Mule ESB, is to use XML or JSON to represent the information in the object. Mule includes strong support of XML and JSON. Many times, you can simply add a <json:object-to-json-transformer /> to your flow, and the payload will be converted automatically. For this to work, your payload needs to be simple types such as Maps and Lists, or an instance of a class with JAXB mappings.

Your question is a bit confusing .
if you are asking about the transformation in mule add this in your XML file <object-to-string-transformer doc:name="Object to String"/> after the dropbox component.

Related

Best way to load properties file during runtime in mule

I would like to know what would be the best way to load the properties file in mule for the following scenario.
I have a properties file where in I set all the email related fields set in it like templates, to and from etc.
All these need to be set to a specific object along with other changes to that object so I'm planning to use a Java transformer and now I need to load all those values from that properties file and send to transformer. So what would be a best approach to work in above scenario.
Load properties in Java transformer using core java load properties
Load properties using spring context and send it to transformer and access using inbound properties
Kindly let me know if there is any other better approach other than above
First, you should be able to load the property file using spring context as shown below:
<context:property-placeholder location="somename.properties" />
Then, you can set specific property value as flow variable as shown below:
<set-variable variableName="fromAddress" value="${xyz.fromAddress}" />
Finally, you can access this flow variable in your processor class as shown below:
String fromAddress = muleEvent.getFlowVariable("fromAddress");
I will suggest to use Java load properties if these properties are only used in one Transformer. There is one more benefit out of Java load properties is that after modifying your properties file you do not need to restart the application.

Mule: Extracting connector-ref to properties file

I have a line that reads like this in my flow:
<jms:inbound-endpoint queue="${queue.name}" connector-ref="${inbound.connector}" doc:name="Inbound Endpoint">
Where the "${inbound.connector}" property refers to a string in my properties file:
inbound.connector=Active_MQ
The reason I am doing this is because the connector-ref will vary depending on the environment. Sometimes it will be an Active_MQ connector, sometimes it will be a JMS Connector.
The properties file is under src/main/resources, as it should be. I have other properties in the flow that are read from the properties file just fine, such as the "queue.name" property. However, for some reason when I try to start Mule it is returning an error saying:
NoSuchBeanDefinitionException: No bean named '${inbound.connector}' is defined.
As far as I know the connector-ref value is just a string, so this should work in theory. I don't understand this error. Is the connector-ref in fact not a string, and this approach is illegal?
Internally mule runtime creates/initializes all the connector objects(spring bean) like HTTP Connectors, DATASOURCE connectors and various other connector's when the application comes up. All these connectors are nothing but a spring beans as Mule is written on top of Springframework. So it is not possible to make it dynamic by passing in a string value where it expects a object.

Difference between invocation variable set using Message Properties transformer and set-variable transformer?

Context for the question is that: When I use set-variable, then the datamapper recognizes and shows the variable in drop-down but it never shows up when invocation variable is set using Message Properties transformer.
Is this normal?
No it is not normal.
Make sure to use the invocation scope on the message properties transformer.
Message Properties Transformer is the old way of adding variables/properties of different scope to message.
Mule refined the message-properties-transformer and split that into <set-variable>, <set-property> and <set-session-variable> transformers.
Apart from the clear seperation of functionality, this makes the flow more readable and understandable when looking at a flow diagram.
Point to remember is, Message Properties by default adds the property to outbound scope. If you want to add it to invocation scope that have to be mentioned in the scope attribute.
<message-properties-transformer scope="invocation">
<add-message-property key="someKey" value="someValue"/>
</message-properties-transformer>
Hope this helps.

Mule parsing Incoming REST request

I have a REST client that is preparing payload in JSON format and invoking a REST service. My job is to create the REST service in Mule. I need some information on how I can map the incoming Payload to a java object so that I can invoke the REST service component class and get the values passed in the JSON object. Does the payload after HTTP inbound endpoint already contain the JSON values sent by the client? In which case a simple JSON to Object mapper would map it as Hashmap?
You will most likely need to create a custom transformer for this use case if you have a special use case.
See: http://www.mulesoft.org/documentation/display/current/Creating+Custom+Transformer+Class
If you get sent JSON you can convert it into a custom class like this:
<json:json-to-object-transformer name="jsonToFruitCollection" returnClass="
org.mule.module.json.transformers.FruitCollection"/>
Alternatively You can also use ObjectMapper and can probably use a bean to map your JSON directly to your Java object in your Java class.
You can also use <json:json-to-object-transformer/> directly after your Http inbound endpoint, parse and get each element value in your Mule flow and store in variables. Then these variables can be passed into your Java class where you can map these to your Java object easily.
Both the approach will work fine

Mule 3.4+: Best practice in setting the payload for static SOAP request

I'm pretty new to Mule so this may be a silly question. I'd like to call a remote axis2 SOAP service from Mule and for this I will use the SOAP component. What I am struggling with is the correct pattern for PAYLOAD population. Here is a very simple payload example
<oper:CreateTask xmlns:oper="http://api.abc.com/workflow/operationtypes">
<workType>
<Name>Reminder Task</Name>
</workType>
<activitySubject>
<GenericSubject>Richard Fanning</GenericSubject>
</activitySubject>
<description>This is a Mule generated Reminder Task</description>
</oper:CreateTask>
The payload is currently being populated via the set-payload transformer and the XML is embedded in the flow as seen below
<flow name="createWorkflowTask" doc:name="createWorkflowTask">
<set-payload value="<oper:CreateTask xmlns:oper="http://api.abc.com/workflow/operationtypes"><workType><Name>Reminder Task</Name></workType><activitySubject><GenericSubject>Richard Fanning</GenericSubject></activitySubject><description>This is a Mule generated Reminder Task</description></oper:CreateTask>" doc:name="Set Payload"/>
<cxf:proxy-client doc:name="SOAP" enableMuleSoapHeaders="true" payload="body"/>
<http:outbound-endpoint exchange-pattern="one-way" method="POST" address="http://localhost:6081/workflow/services/ActivityServices" doc:name="HTTP"/>
</flow>
My question is what the most appropriate way of setting this payload. My thoughts would be
if the PAYLOAD were larger would it be better to maintain this XML in a file in the Mule project and read it as outlined in this question
I'd prefer not to generate client stub classes for the Request but perhaps I should use CXF to define the service class. What advantages would this provide?
Are there other preferred methods of payload population. In my use case this (sub)flow would be called from a router so I'd not be passing any relevant information that would alter the message.
Aside: Perhaps for the worktype name "Reminder Task" I should extract to mule-app.properties and use XSLT to populate in final request?
Thanks
Rich
In order to set payload in a flow, you can use either of the following ways.
Write a component(Java bean) which has the XML request as String and then make that string as return from the component. This component should be the first message processor in your flow.
Write a component(Java bean) which read the XML request from a file into a String and then make that String as return from the component. This component should be the first message processor in your flow.
Use a Inbound-Endpoint ( file or JMS) as the entry point of your flow. These inbound can read from the path specified. This way your input can be dynamic. And you can execute the flow multiple times fo different requests without the need to start the Mule server everytime.
More on Mule File and JMS endpoints in the following links.
Mule JMS Transport Reference
Mule File Endpoint
Next for your XSLT population of the worktype name, Mule XSLT Transformer from the XML module can be used. More on this in the following link Mule XSLT Transformer
Hope this helps.