How to get parameter from property to script mediator in Integration Studio? - wso2

I want to use the query parameter from the url in the script mediator in Integration Studio. But I can read this parameter as Log. But when I try with the following method in the script mediator, the parameter does not come. Can you help with this
<property description="Get EnergyType Of Url" expression="$url:energyType" name="energyType" scope="default" type="STRING"/>
<log description="Logging EnergyType Of Url">
<property expression="$url:energyType" name="EnergyType"/>
</log>
<script language="js"><![CDATA[
var energyType = mc.getProperty('energyType')></script>

Try this:
<script language="js">
<![CDATA[
var energyType = mc.getProperty('query.param.energyType')
energyType =energyType.toString();
mc.setProperty('energyType',energyType);
]]>
</script>
<property "get-property('energyType')" name="energyType" scope="default" type="STRING"/>
<log level="custom">
<property expression="get-property('energyType')" name="energyType"/>
</log>

Related

WSO2 exclude property from log

I am new to wso2 and I want to log all the properties in the incoming request except one property
here's my code:
<log level="full"/>
<propertyGroup>
<property expression="json-eval($.username)" name="Username" scope="default" type="STRING"/>
<property expression="json-eval($.password)" name="Password" scope="default" type="STRING"/>
<property expression="json-eval($.objectStore)" name="ObjectStore" scope="default" type="STRING"/>
<property expression="json-eval($.documentClass)" name="DocumentClass" scope="default" type="STRING"/>
<property expression="json-eval($.fileName)" name="FileName" scope="default" type="STRING"/>
<property expression="json-eval($.fileData)" name="FileData" scope="default" type="STRING"/>
<property expression="json-eval($.contentType)" name="ContentType" scope="default" type="STRING"/>
</propertyGroup>
I want to exclude the fileData property from log because it's base64
is there's any solution other than a custom log that has only the properties that I want to log ?
Its not posible by default i think. You can write your custom mediator for logging necessary attributes and properties
By default, the <log level="full"/> won't log your property - it is logging standard headers (i.e. To, From, WSAction, SOAPAction, ReplyTo, and MessageID) and the full payload of the message. And as i see in your example - json-eval($.fileData) is part of the JSON payload.
So, there is no other option than using a custom logger.

Dynamic REGEX in filter mediator of WSO2 EI

I have made Filter mediator to check email subject has specific keyword or not using REGEX.
<property value="Test SR AWS onboarding of AWS server" name="emailSubject" scope="default" type="STRING" />
<filter regex=".*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR SEC.*|.*INAWS.*|.*INSAP.*|.*INFW.*|.*INSEC.*"
source="get-property('emailSubject')">
<then>
<log level="custom">
<property name="==Test Case ===" value="pass" />
</log>
</then>
<else>
<log level="custom">
<property name="==Test Case ===" value="Fail" />
</log>
</else>
</filter>
Plenty of keywords(more than 60) are required in my case. I have hard coded keyword in Code, Instead of this, i am trying to store these keyword in somewhere(eg. localentry) and try to match subject with this to make code as generic.
Localentry:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="EmailTicketing_Keyword" xmlns="http://ws.apache.org/ns/synapse">
<SR>.*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR SEC.*</SR>
</localEntry>
Reading from Localentry:
<property expression="get-property('EmailTicketing_Keyword')" name="tokenconfig" scope="default" type="OM"/>
<property expression="$ctx:tokenconfig//*[local-name()='SR']" name="SR" scope="default" type="STRING"/>
I am unable to use above property( SR) to match with subject in Filter Mediator.
Is there any way to achieve my use case?
PS: new Keyword may be added in future, to avoid code level changes whenever key word change required i just add new keyword in localentry instead of code which will work fine since keyword change is generic,That's why i am trying this.
Your localEntry as XML wont work, because it starts as . (dot) and wstx parser will throw error. Use instead LocalEntry as Text:
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="RegTicketing">
.*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR
SEC.*
</localEntry>
For using that as regexp You need use ScriptMediator as below:
<property name="tokenconfig" expression="get-property('RegTicketing')" scope="default" type="STRING"/>
<script language="js">
var regStr = mc.getProperty('tokenconfig').toString();
var testStr = mc.getProperty('emailSubject').toString();
var regExp = new RegExp(regStr);
mc.setProperty('testResult',regExp.test(testStr).toString());
</script>
And you can use that testResult in FilterMediator:
<filter xpath="$ctx:testResult='true'">
As per the documentation [1] of the filter mediator, the regex only accepts a string. Therefore according to this you will not be able to dynamically set the value of the regex expression in the filter mediator.
As an alternative approach you can use a class mediator for this. You can feed the content of the local entry and the email subject, then evaluate the regex expression within the class mediator.
[1]-https://docs.wso2.com/display/EI660/Filter+Mediator+
Solution:
<script language="js"><![CDATA[var log = mc.getServiceLog();
log.info("===Inside keyword Match Script Mediator===" );
var emailSubject = mc.getProperty('transemailSubject').toString();
log.info("EmailSub::" + emailSubject);
var keywordSR = mc.getProperty('SR').toString();
//log.info("SR Keywords::" + keywordSR);
var regExpSR = new RegExp(keywordSR);
mc.setProperty('SR_Match',regExpSR.test(emailSubject).toString());
log.info("SR_Match::" + mc.getProperty('SR_Match'));]]></script>
<filter xpath="$ctx:SR_Match='true'">
<then>
<log level="custom">
<property name="==Test Case ===" value="pass" />
</log>
</then>
<else>
<log level="custom">
<property name="==Test Case ===" value="Fail" />
</log>
</else>
</filter>

Update Registry file content in WSO2 EI 6.1.1 dynamically

I am storing config file in Config registry like below.
My use case here is, need to update accessToken tag content from wso2 ei service,because it expires within an hour. how can i pass regenerated accesstoken to this file?
<!-- Reading config registry file content -->
<property name="ZohoAppConfig" expression="get-property('registry','conf:/ZohoConfig/ZohoAppConfigFile.xml')" scope="default" type="OM" />
<property description="accessToken" expression="$ctx:ZohoAppConfig//*[local-name()='accessToken']" name="accessToken" scope="default" type="STRING"/>
<property description="refreshToken" expression="$ctx:ZohoAppConfig//*[local-name()='refreshToken']" name="refreshToken" scope="default" type="STRING"/>
<property description="AllPortalsEP" expression="$ctx:ZohoAppConfig//*[local-name()='AllPortals']" name="uri.var.AllPortalsEP" scope="default" type="STRING"/>
<log level="custom">
<property name="===ZohoAppConfig===" expression="get-property('ZohoAppConfig')"/>
<property expression="get-property('accessToken')" name="===accessToken===="/>
<property expression="get-property('uri.var.AllPortalsEP')" name="===uri.var.AllPortalsEP===="/>
</log>
<!-- Need to update registry content here -->
Awaiting for your response.
You can modify your ZohoAppConfig, for example using enrich mediator:
<enrich>
<source clone="true" property="someNewAccesToken" type="property"/>
<target action="replace" xpath="$ctx:ZohoAppConfig//accessToken" type="custom" xmlns:ns="http://org.apache.synapse/xsd"/>
</enrich>
This will update in your property ZohoAppConfig the token. Next to store back to registry you need use property:
<property expression="$ctx:ZohoAppConfig" name="conf:/ZohoConfig/ZohoAppConfigFile.xml"
scope="registry" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
That should works in 6.1.1, I tested it on 6.3.0. but that way is caching the read in memory for 15sec, what could be in some cases problematic.
Another way is to store using script mediator, and set the cacheDuration to 0:
<script language="js"><![CDATA[
mc.getConfiguration().getRegistry().updateResource('conf:/ZohoConfig/ZohoAppConfigFile.xml',mc.getProperty('sample'));
var regEntry = mc.getConfiguration().getRegistry().getRegistryEntry('conf:/ZohoConfig/ZohoAppConfigFile.xml');
regEntry.setCachableDuration(0);
mc.getConfiguration().getRegistry().updateRegistryEntry(regEntry);
]]></script>
I have described it much more with an example, along with some encountered problem here

WSO2 Integrator: REST API GET Request missing response body

I am using WSO2 Integrator 6.6.0 to make a blocking HTTP GET to a REST API that returns a JSON response (HTTP 200 OK).
But I never see the response body inside my sequences. I am not sure what I am doing wrong, having exhausted all available documentation and other threads.
Sequence doing the call (simplified to anonymize), which logs the response afterwards:
<!-- Remove XML body as not needed for GET request -->
<payloadFactory media-type="json">
<format></format>
<args></args>
</payloadFactory>
<header name="Accept" value="application/json" scope="transport"/>
<property name="NO_ENTITY_BODY" value="true" scope="axis2" type="BOOLEAN" />
<call blocking="true">
<endpoint>
<http method="GET" uri-template="http://my-api/order-status">
</endpoint>
</call>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<log level="full">
<property name="response-log" value="Received response"/>
<property expression="$body" name="response-body"/>
<property expression="json-eval($)" name="json-eval-body"/>
</log>
This results simply into a log line - with no response body at all!
INFO {org.apache.synapse.mediators.builtin.LogMediator} - To:
http://my-api/order-status, WSAction: urn:mediate, SOAPAction:
urn:mediate, MessageID:
ID:414d51204343494153303131202020209ca4175f28ac422e, Direction:
response, response-log = Received response, response-body =
<soapenv:Body
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"></soapenv:Body>,
json-eval-body = {}, Payload: {}
I can see the request in my API application and I can also see what it correctly returns a response body in its logs. Also, using Postman to do the same request I receive the following JSON response with 200 OK and Content-Type appication/json:
{
"order_status": "NOT_FOUND"
}
Inside WSO2 I do not see anything! I want to be able to convert the response into XML format.
What could be wrong?
Before making a REST call, it is necessary to remove the headers from the main call.
Put this snippet before the call mediator:
<property name = "FORCE_HTTP_CONTENT_LENGTH" scope = "axis2" type = "STRING" value = "true" />
<property action = "remove" name = "REST_URL_POSTFIX" scope = "axis2" />
<property action = "remove" name = "TRANSPORT_HEADERS" scope = "axis2" />
As I understand it should solve.
Can you modify the NO_ENTITY_BODY property as follows and try this mediation again.
<property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
Having simplified this configuration for this question, I had missed that having the following property set in my proxy service:
<property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
Means that WSO2 is ignoring the response from the API. Removing this property resolved the issue for the following config:
<header name="Accept" value="application/json" scope="transport" />
<property action="remove" name="NO_ENTITY_BODY" scope="axis2"/>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<call blocking="true">
<endpoint key="conf:/endpoints/FX_TRADING_API_ORDERSTATUS.xml"/>
</call>

How to use property mediator pattern in wso2

When using property mediator pattern i am getting internal server error
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="regex">
<log level="custom">
<property name="log" value="******************************"/>
</log>
<property name="regex" expression="$url:regex" scope="default"
type="string" pattern="(.|\s)*\S(.|\s)*" group="2"/>
<property name="service_ep"
value="http://www.mocky.io/v2/5d0086223200007700f9d561" />
<header name="To" expression="get-property('service_ep')" />
<log level="full"/>
</sequence>
Response what i am getting :
<am:fault xmlns:am="http://wso2.org/apimanager">
<am:code>0</am:code>
<am:type>Status report</am:type>
<am:message>Runtime Error</am:message>
<am:description>Unknown type : string for the property mediator or the
property value cannot be converted into the specified type.
</am:description>
</am:fault>
The type should be in block letters.
<property name="regex" expression="$url:regex" scope="default"
type="STRING" pattern="(.|\s)*\S(.|\s)*" group="2"/>
Please refer to this documentation. https://docs.wso2.com/display/EI650/Property+Mediator#PropertyMediator-ConfigurationConfigs