Setting Message Payload to Session Variable - mule

In my Mule flow, I have a message that is constantly being edited to form a record of all processing at the end of the flow. At one point, I make an HTTP call to a webservice to get some details for the message.
My problem is the following:
The HTTP response is saved as the message payload which requires me to save my master record to a session variable in order to save all previous processing. However, when I set the session variable, the value of the session variable is java.class#d6883 (java.class = the class of the object. Had to edit to comply with my code of conduct). How do I set the value of the message payload into a session variable rather than the memory location of the message payload?

Assuming the object you put in session is Serializable, you can achieve this using MEL:
<set-session-variable variableName="tempCanonical" value="#[message.payload]" />

Related

attributes.headers getting lost after a http Request call in Mulesoft?

I am getting some attributes in an API but all getting lost after an HTTP request connector in mule4.
why is it happening?
Look in the connector's configuration properties -> advanced tab for the connector configuration (in this case the HTTP connector's "request" operation) and you'll find a target variable and target value. If you fill in the target with a name - this does an enrichment to avoid overwriting the Mule message. If you leave it blank (the default) it will save the message (attributes, payload) over the top of the existing one - which is what you're seeing now. This mirrors the old mule 3 functionality, but sometimes you want it to leave what you have there alone.
So for the target value you get to pick exactly what gets saved.. If you want just payload: put that in. If you want both payload and attributes - I'd use "message" as that will mean you get both payload and attributes saved in the variable. Of course you may not want as much saved, so feel free to put in whatever dataweave expression you like - so you could even create something with bits from anywhere like:
{
statusCode: attributes.statusCode,
headers: attributes.headers,
payload: payload
}
A connector operation may replace the attributes with those of the operation. If you need to preserve the previous attributes you need to save them to a variable.
This is a default behaviour of MuleSoft. Whenever request crosses to transport barrier it losses existing attributes. You need to preserve attribute before HTTP Request.

mule invoke java component as array parameter

properties file:
#torun='GSD11','GSD12'
torun='GSD11'
<flow name="deleteInvoiceFlow" doc:name="deleteInvoiceFlow">
<http:inbound-endpoint exchange-pattern="request-response" host="${hostname}" port="${port}" path="deleteInvoice" doc:name="HTTP"/>
<invoke object-ref="client" method="deleteInvoice" methodArguments="${torun}" methodArgumentTypes="java.lang.String" />
</flow>
<spring:bean id="client" name="client" class="com.util.DeleteTable"/>
Java: DeleteTable:
public String deleteInvoice(#Payload String deleteCompany) throws SQLException{
It works well for single parameter from the properties as shown above in properties file. But If i run application with below companies in properties
`torun='GSD11','GSD12'
it gives error message as
1 (java.lang.ArrayIndexOutOfBoundsException). Message payload is of type: String
How do I enable to receive parameteras array?
the Mule message's Payload is a Object. Thus allowing it to handle any kind of object.
If you check the MuleMessage interface you'll see it.
In your code above your sending whatever comes from your inbound endpoint (http) to your spring bean, and you are assuming it's going to be a string.
Now the payload can certainly change in an http inbound endpoint depending on what type of request you receive (get/post/put/etc) so be careful with that.
Going back to your question, if you're positive the payload is going to be an array you can just change the firm of your method to that. If not I'll advise you change it to object and validate what's coming and cast accordingly.
HTH
Mule docs says:
http://www.mulesoft.org/documentation/display/current/Invoke+Component+Reference
methodArguments="#[1], #[2]"
methodArgumentTypes="java.lang.Float, java.lang.Float"
but my list is random and it grows to 100s to 1000s, I don't want to put 1000s of argument types.
As a workaround solution I am loading the mule-app.propertes in java component and reading the property content.
public String deleteInvoice(){
Properties prop = new Properties();
InputStream input = DeleteTable.class.getClassLoader().getResourceAsStream("mule-app.properties");
prop.load(input);
return prop.getProperty("torun");
}

Set variable session copyonwritearraylist

Im new on Mule ESB and i have the next problem. I use connector "Collection Splitter" to separate a list of orders (books) to other things. When i do the checks i go back to join the books on the order with connector "Collection Agreggator". What i want is save the information of the payload in that moment in a session variable. The system dont do that. I think is posible because the type saves on the payload is "CopyOnWriteArrayList" type and i dont now if is posible save this type of list in a session variable.
Someone can be help me please?
Thanks!
You can also use a set-session-variable transformer. Set the name is something of your choosing and set the value to be #[payload]. That you will maintain both your payload and have a new session variable. However be very careful when using session variables, since these are serialized when sending a Mule message over a transport. If possible, try to use flow variables.
use the message enricher scope to achieve the same.
define the required component inside the message enricher scope.
in the enricher scope specify source as payload and the target as session variable.
try and let me know the status.

how to set a property globally in wso2 ESB

I am trying to figure out how to implement session management in wso2 esb.So i have written a class mediator which generates session_ID that i want to store.For storing the session id I am using following code as:
org.apache.axis2.context.ServiceContext serviceContext = org.apache.axis2.context.MessageContext
.getCurrentMessageContext().getServiceContext();
serviceContext.setProperty("SessionIDGlobal", uuid);
But while running it in my esb's proxy it throws null pointer exception at getCurrentMessageContext part.I have followed another approach where-in i am storing the sessionID in property mediator and tried to get its value but when i click postRequest operation after generateSessionID operation from try-it. all the property gets reset and my sessionID property gives me null value. What should i do to rectify this problem? Is there any alternate way?
You have to create servicecontext like this;
ConfigurationContext cfgCtx =(((Axis2MessageContext) synCtx).getAxis2MessageContext(). getConfigurationContext();
cfgCtx.getOperationContext().getServiceContext();
You should store in the Message context but you have stored in the service Context. please refer this to understand how you can set the properties at different scopes. Synapase (default), Axis2, Transport etc.
Please refer this blog post for complete details.
http://blog.facilelogin.com/2011/02/wso2-esb-property-mediator-different.html

get attachment from System.ServiceModel.Channels.Message

Does any one know how to get attachment from System.ServiceModel.Channels.Message?
When client requests from the server, and server is returning a xop element with attachment in the response. But I don't know how to get the attachment from the Message obj returned from the service call.
I don't fully understand all the circumstances, but there are several options the first is to use appropriate message encoder in binding, for example CustomBinding with MtomMessageEncodingBindingElement element. In that case, XmlDictionaryReader returned by Message.GetReaderAtBodyContents() will automatically replace xop:Include elements by corresponding attachment. In case, if there are needs to manipulate attachments at lower level, or in case if non-MTOM encoding is used, you will need to write appropriate message encoder that will meets you requirements.