Is there a way to limit what session variables get exported as HTTP headers in a mule flow that involves a mid-flow HTTP transport? - mule

I'm storing some data as session variables in a mule flow that involves a mid-flow http call to an external service. I need this data for a subsequent branch of the flow logic, hence the session variable. However, I do not want this data to go out with the http call as http headers. Is there a way to limit which session variables mule includes as http headers in a given call?

If you want to decide what session property to propagate you can write a custom session handler and inject it into your http connector something like this:
<http:connector name="httpConnector">
<service-overrides sessionHandler="your_session_handler" />
</http:connector>
An example of a custom session handler can be found here
That said I'd like to echo David Dossot's answer: session variables should seldom be used, especially knowing how Mule leaks them by default. If you want to propagate something, you should set it explicitly as an outbound property and mule will propagate it.

Use flow variables: session variables should seldom be used, especially knowing how Mule leaks them by default.
Flow variables (aka invocation properties) should be unaffected by endpoint interactions, this by design. Any other behaviour would be a bug. If your variables do not need to extend further than a flow, use flow variables, not session variables.

session variables are not sent across http calls, only session ID is sent via cookie (usually)
if you need to pass some data via http call to external domain - include them in GET, for example, as cookie are not shared

Related

How can i Use a token generated in the next thread in Jmeter

I have extracted the authorization token after running my login API successfully. And it can be used within the same thread while I'm configuring different HTTP requests.
But I want to create a separate thread for rest of my APIs and use the authorization token which I extracted already. How can I do it in Jmeter?
According to JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So you can pass it in 2 ways:
Using __setProperty() function in 1st Thread Group (or thread) to store the value into a JMeter Property and then use __P() function to read it back where required.
Using Inter-Thread Communication Plugin if your logic is more complex

Calling a HTTP endpoint before calling APIKit router

I am calling a HTTP endpoint before my message reaches the apikit:router.
By the time the message reaches the apikit:router, it has lost all the original HTTP headers. Because of that the router is not able to figure out the intended method.
Actually I am able to copy the inbound properties in a session variable, after the HTTP call I am able to revert them as outbound property. Then I have my api router in another flow, so the outbound property gets coverted to inbound property again. This solution works fine, but the flow looks pretty ugly. (as I had to do this copy for about 10 headers)
Is there any better workaround ? Also I am trying to avoid manually dealing with these headers.
Wrap the HTTP call within the enricher, it will preserve the http inbound properties and the payload of the original call. Like this,
<enricher target="#[flowVars.resultOfInnerHTTP] source="#[payload]">
<http:request...../>
</enricher>
<apikit:router.../>

Neo4jClient: add arbitrary HTTP headers to Cypher requests?

I'm trying to add a custom HTTP header to Neo4jClient's outgoing Cypher requests, on a per-request basis. What I mean by per-request basis is that the contents of the HTTP header depend on the (user in the) current session.
The idea is that this header will be interpreted by a load balancer so that it always redirects the request to that slave in the Neo4j cluster where the data of the user in the current session is already mapped to memory, leading to performance gains.
For example, I might keep the address of a particular slave in the user's session and add the HTTP header Neo4j-slave: <address> to outgoing requests towards the load balancer. It will then redirect this request to the right slave.
I'm not sure if Neo4jClient is built with this kind of extensibility in mind; from the looks of it, I'm going to have to duplicate a lot of code in non-virtual methods if I don't want to alter existing code.
I've been looking at implementing IHttpClient as an entry point into the GraphClient. After all, I can pass my implementation to GraphClient's constructor and it receives the outgoing HttpRequestMessage so I can modify it along the way) but I think that only works for modifications that only depend on the HttpRequestMessage itself (or on some state somewhere but I want to avoid that).
I've also been looking into ThreadLocals as a means to pass additional arguments to HttpClient#SendAsync but I'm not sure if those even work if asynchronous methods are involved.
Is there a more or less trivial way to hook into Neo4jClient and add this header?
Thanks!
I can't think of a good way to do this currently, however if you add the required extensibility to IGraphClient in a clean way, I'd accept a pull request for this and we can include it in the published library.

Using Mule variables or properties accross transport barriers

Let say I get a inbound variable from an http connector when I use this URL
http://x.x.x.x:8080/post?post-message=Hallo wold.
How can I use the value of the #[header:INBOUND:post-message] accross the complete flow from after the HTTP connector all the way up tp the end. Should I use the Mule Object store to write it to ram?
This post shows the scope of variables but it seem there is not one thay can flow from start to en like a session bean
https://m-square.com.au/mule-school-the-mulemessage-property-scopes-and-variables/
Kind Regards.
If you need the variable available throughout the whole flow (and
other flows reached through a flow-ref) use the invocation scope
(set-variable to set, flowVars[] to read it)
If you need it to reach other flows through a transport (e.g. VM) put
it in the outbound or session scope.
If you need it to live as long as the app is running, through
different calls, use the Mule registry (volatile, only available as
long as the app is up) or the object store (which can be configured
as persistent, to hold state even if the app goes down).
Mule Session variables are good enough to get the value throughout application.
But if you need to use the value outside your application, then you can set mule outbound properties.
There are three type of scope level variables supported by mule .
You can use flow variable if you want the variable to be accessible throughout the whole flow.
You can use session variable if you want it to be accessible form other flows through a transport barrier.
Refer this blog for getting better understanding of how different types of variables are propagated between different mule flows.
http://blogs.mulesoft.com/dev/anypoint-platform-dev/mule-school-the-mulemessage-property-scopes-and-variables/

Session variables skipping multiple requests

I understand that session variables in Mule ESB can be shared between different flows of the same request, but... How can I make a variable visible to multiple requests?
It is a variable, so it would change depending on the data the flow receive.
Thanks in advance!
you can use the new mule 3.4 cache scope to store/cache objects, alternatively you can use the Mule Objectstore to save the session value as key and object. This
can be in-memory or persistent
<objectstore:retrieve key="mykey" defaultValue-ref="myvalue"/>