How to find current flow name in mule? - mule

I need to capture current flow name into a variable.
I tried with #[flow.name] but no luck in mule 3.8.0
can anybody please assist me?

Based on the answer in this post: How to get caller flow name in private flow in Mule
There is a simplest way to get the flow name and put it into a variable:
<expression-component doc:name="Expression"><![CDATA[flowVars.flowName = flow.name;]]></expression-component>

Alternately, you can directly use expression #[mule:context.serviceName] in a variable :-
<set-variable variableName="myFlowName" value="#[mule:context.serviceName]" doc:name="Variable"/>
<!-- Print the value of variale in logger -->
<logger message="#[flowVars.myFlowName]" level="INFO" doc:name="Logger"/>
This will set your current flow name directly in variable

In mule 3.8.5 using Groovy script component,
flowVars.currentFlowName = eventContext.getFlowConstruct().getName();

I have been using #[flow.name] in 3.7.3 and just tried in 3.8.0 to make sure it had not been removed and it worked fine for me in logger and setting a flowVars value. I suggest posting up at least a snippet of your flow and maybe we can spot the issue you are having.
PS, not sure why flow.name is not in standard forms or really documented by Mule, and as it is not there continues to be some worries they will remove it. I have seen it stated more than just here that it is not accessible in MEL, but #[flow.name] is a MEL expression and does work. To use if for something like I Parse Template in exception strategies, I use sulthony's form, set a flowVars value in an expression and refer to that flowVars in my template.

You can access flow name in logger by using #[flow.name] but its not accessible in MEL. Use flowconstruct for getting flow name. Refer this answer
Hope this helps.

Related

Invalid content error on compression:extract

I am attempting to use the zip-extract method in a foreach loop to unzip several files. When I attempt to deploy the project the following error is returned:
Invalid content was found starting with element 'compression:extract'. One of >'{"http://www.mulesoft.org/schema/mule/core":annotations, >"http://www.mulesoft.org/schema/mule/core":abstract-message-processor, >"http://www.mulesoft.org/schema/mule/core":abstract-mixed-content-message-processor}' is expected.
The actual code is:
<compression:extract doc:name="Extract" doc:id="9119d722-95eb-4aee-a734-50e2a2825449" >
<set-payload value="#[payload]" />
<compression:extractor >
<compression:zip-extractor />
</compression:extractor>
</compression:extract>
I have not been able to find anything online that would point towards a solution.
It look like accidentally a <set-payload> was put inside the <compression:extract> element. Also it doesn't make sense at all because it puts the payload as the payload, which it is already is. Just remove that line.
Maybe the intention was to use <compression-compressed>?
Example:
<compression:extract>
<compression:compressed>#[payload]</compression:compressed>
<compression:extractor>
<compression:zip-extractor/>
</compression:extractor>
</compression:extract>
payload is already the default, so it is not needed.

How to read properties from .properties file in Mule

I'm trying to use Mule Credentials Vault security feature.
I've created .properties file, Security Property Placeholder and defined the key and encryption algorithm.
Now I want to use some of the properties from the file when I return HTTP response.
I have the file src/main/resources/data.properties that contains for example:
In my canvas, under Configuration XML I added:
<secure-property-placeholder:config name="Secure_Property_Placeholder" key="24681357" location="data.properties" doc:name="Secure Property Placeholder" encryptionAlgorithm="DES"/>
<set-variable variableName="card.number" value="${number}" />
In my canvas I have message flow that builds xml 'Create XML response based on User'. The value in settings is:
This doesn't work. The error I get is:
-> org.mule.module.launcher.DeploymentInitException: IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
-> Caused by: org.mule.api.lifecycle.InitialisationException: Invalid bean definition with name 'org.mule.autogen.bean.13' defined in null: Could not resolve placeholder 'key' in string value "${key}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
-> Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'key' in string value "${key}"
Does anyone know how can I read the properties from .properties file (credentials vault)? And then use it in my flow?
Thanks,
Keren
If you simply want to get the value for the property number and add it into the XML you can use ${number} from .properties. No need to define any other variables in Configuration XML.
<set-payload value="<user><name>Royal Bank of Canada</name><id>Royal_Bank_Of_Canada</id><cc><company>>Visa</company><number>${number}</number><secret>123</secret></cc></user>" doc:name="Set Payload"/>
However note that the property placeholder is resolved at startup so you will not be able to dynamically retrieve a property based on some user input. For this you will have to do some Java coding. This SO post gives you some hints on how this can be achieved. Based on those answers I have created a simple example on how this can be done with a very simple helper bean.
I'm afraid you just can't. The Mule Credentials Vault is an enterprise feature and therefore tipically you won't have access to the source code unless you are a MuleSoft customer.
Even if you were a customer, the api you'd use would be sort of unsupported. I suggest to manually create a custom java component levearing your code and Jasypt (not as a property placeholder but as a library).
The other option, if you are a customer (I guess you are given you are using the credentials vault) is to contact the official support so they take care of it for you.
The property placeholder is used resolve at startup so you will not be able to dynamically retrieve a property based on some user input.
Use ${propertyName} from .properties in MEL to access particular property
From Dataweave you can read it as given below
p('variablename')
where variablename is defined in property files ex: variablename = 15

Mule MEL, how to get a property value?

In the Mule MEL, how to get a property value ?
In the java code, I did this:
eventContext.getMessage().setInvocationProperty("amount", 100);
I have tried these options
#[message.invocationProperty.invocation]
#[message.invocationProperty('invocation')]
#[message.getInvocationProperty().get('invocation')]
I realize that message is an instance of org.mule.el.context.MessageContext, then what is the correct syntax ?
Try #[message.inboundProperties['propertyName']] or #[message.invocationProperties['propertyName']]
If you set a variable with scope INVOCATION (with Message Enricher or Variable), you can get the variable with the syntax below:
#flowVars['your_Variable_Name']
Although the sintaxis is almost the same, it depends on the scope of the property variable, but the most usual way is:
#[flowVars['flow_var_name']]
In my personal opinion I don't recommend to use:
#[flowVars.variable]
Because in some complex environment with many messageContext switches the variable could get lost. I recommend to take a look on the next post from Mulesoft oficial blog that shows how to handle Properties and variables.
This answer for your comment
<set-variable variableName="amount" value="message.invocationProperties['amount']" />
Solution is
<set-variable variableName="amount" value="#[message.invocationProperties['amount']]" />
To get the Invocation properties of a message follow this syntax:
#[flowVars.parameter] or #[flowVars['paramater']]

Creating my own connector - Access Flow Variables within a MessageDispatcher

I want my connector to be robust and accept MEL expressions. Right now though I'm having a problem where I'm doing the MEL Evaluator, but what's in my event is not evaluated. I have <set-variable variableName="key" value="#[payload]" /> and then I do this: <my-connector:outbound location="#[key] />.
When I debug this while stepping through my connector, I look into the MuleEvent and I see under the flowVariables field that key=#[key] instead of key=MyProperty.
I looked at LoggerMessageProcessor to ensure that I'm doing this correctly and my code for evaluation looks the same, but when I debug into the logger, I see that MuleEvent has the expression parsed in the flowVariables, instead of the notation I showed above.
Is there a trick to having the MuleEvent have the expressions of flowVariables already evaluated?

How to refer session variables in Groovy script in mule studio

I just started working with Mule.
Flow description:
I have an HTTP inbound endpoint receive XML message, and I Hvae to update the database (derby) using the XML payload.
Ex: I will be receiving Emp Id and Emp name, Exp in the request. I have to update the table with these values.
My Implementation:
After receiving XML input I am using the message Property transformer to save the values in Session scope.
<message-properties-transformer scope="session" doc:name="Message Properties">
<add-message-property key="EmpNum"
value="#[xpath:/CreateEmployee/EmpNum]" />
</message-properties-transformer>
like above. And then I have Groovy Script component to update the table.
My Query is:
r.update(conn, "INSERT INTO Employee values(#[header:session:EmpNum],#[header:session:EmpName],#[header:session:Experience],#[header:session:Role])");
But it is throwing error:
Lexical error at line 1, column 29. Encountered: "#" (35), after : "". (org.apache.derby.iapi.error.StandardException)
org.apache.derby.iapi.error.StandardException:-1 (null)
Lexical error at line 1, column 29. Encountered: "#" (35), after : "". Query: INSERT INTO Employee values(#[header:session:EmpNum],#[header:session:EmpName],#[header:session:Experience],#[header:session:Role]) Parameters: [](SQL Code: 30000, SQL State: + 42X02) (java.sql.SQLException)
org.apache.commons.dbutils.QueryRunner:540 (null)
I have used a logger component to display the values.
#[header:session:EmpNum]
is displaying the proper value.
Please help me how to refer this session values in Groovy script?
The following works for me when using Groovy script in Mule to read flow variables or session variables respectively.
For reading flow variables I'm using
message.getInvocationProperty('yourVarsName').toString()
For reading session variables I'm using
sessionVars['yoursVarsName'] or flowVars['yoursVarsName']
They work very well for me in the Groovy script in Mule 3.5.
You cannot use Mule Expression Language (MEL) directly in a Groovy script.
If you're using Mule 3.3, replace #[header:session:EmpName] with sessionVars('EmpName') and similar with the other variables.
For previous versions, replace #[header:session:EmpName] with message.getProperty('EmpName',PropertyScope.SESSION)
You have to allow the Groovy script to set the value before sending it as a SQL command. You are sending the literal "message.getProperty('Experience',PropertyScope.SESSION)" straight into the SQL command.
qr.update(conn, "INSERT INTO Employee values("+message.getProperty('EmpNum',PropertyScope.SESSION)+","+message.getProperty('EmpName',PropertyScope.SESSION)+","+message.getProperty('Experience',PropertyScope.SESSION)+","+message.getProperty('Role',PropertyScope.SESSION)+")")
Also don't forget to import the PropertyScope class in the script:
import org.mule.api.transport.PropertyScope
#[groovy:message.getSessionProperty('sesVarValue')
Not sure if any one if anyone needs the answer but for Mule 3.6+ I was able to access Sessions or flow variable by simply doing sessionVars['sessVarName'] /flowVars['flowVarName']
**DO NOT Forget to use "+" to concatenate Strings if values used as strings.