I'm trying to build an API using Switch Mediator to set up a property and reuse its value at a later stage in the sequence. Here is the code. Variable {environment} is initialized ad API call (e.g. http://localhost:8290/.../environmentvalue/test -> sets uri.var.environment = environmentvalue. I use it in the switch case mediator to set up a new property named "target".
Here is part of the code:
<resource methods="POST" uri-template="/{environment}/test">
<inSequence>
<log level="custom">
<property expression="get-property('uri.var.environment')" name="environment"/>
</log>
<switch source="get-property('uri.var.environment')">
<case regex="prod">
<log level="custom">
<property name="target" value="production"/>
</log>
</case>
<case regex="pre">
<log level="custom">
<property name="target" value="preproduction"/>
</log>
</case>
<case regex="stag">
<log level="custom">
<property name="target" value="staging"/>
</log>
</case>
<default>
<log level="custom">
<property name="target" value="invalid"/>
</log>
</default>
</switch>
<log level="custom">
<property name="target" expression="get-property('target')"/>
</log>
</inSequence>
</resource>
As I invoke the API from POSTMAN (POST http://localhost:8290/.../prod/test, IN WSO2 IS Consolle I get the following:
[2023-05-10 17:36:15,250] INFO {LogMediator} - {api:myapi} environment = prod
[2023-05-10 17:36:15,250] INFO {LogMediator} - {api:myapi} target = production
[2023-05-10 17:36:15,250] INFO {LogMediator} - {api:myapi} target = null
The switch mediator works fine. {environment} part of the URL is assigned to the environment property, then used as the source for switching cases. Match is found correctly and the target property in the switch mediator is set correctly to the corresponding case. But outside it is set to null!
I tried several combinations to retrieve the target property as it has been correctly initialized as for switch case, but I always get a 'null', as if the property scope is limited to the switch nest. I also tried to explicitly assign the scope of the target property as axis2 in the switch case instances, and then retrieve it by get-property('axis2', 'target')... but nothing changed. I always get a null as I try to access it outside. I don't understand what I'm doing wrong.
You have your property initialization and value assignment in a log mediator, hence it will only be logged. So add the property mediator outside of the log mediator, like below.
<resource methods="POST" uri-template="/{environment}/test">
<inSequence>
<log level="custom">
<property expression="get-property('uri.var.environment')" name="environment"/>
</log>
<switch source="get-property('uri.var.environment')">
<case regex="prod">
<property name="target" scope="default" type="STRING" value="production"/>
<log level="custom">
<property name="target" value="production"/>
</log>
</case>
<case regex="pre">
<log level="custom">
<property name="target" value="preproduction"/>
</log>
</case>
<case regex="stag">
<log level="custom">
<property name="target" value="staging"/>
</log>
</case>
<default>
<log level="custom">
<property name="target" value="invalid"/>
</log>
</default>
</switch>
<log level="custom">
<property expression="get-property('target')" name="target"/>
</log>
</inSequence>
</resource>