How to pass parameters to XSLT from JBoss Actions Pipeline - xslt

Say I have a given action:
<service category="MyService" name="MyFirstService">
<actions mep="RequestResponse">
<action class="actions.CXFListenerAction" name="CXFServiceListener"/>
<action class="org.jboss.soa.esb.actions.transformation.xslt.XsltAction" name="Transform XML">
<property name="templateFile" value="/stylesheets/transform_response.xslt"/>
<property name="failOnWarning" value="true"/>
</action>
</actions>
I am trying to figure out how to add a property name or parameter that I could then access from within the XSLT. I've tried add additional property names,
<property name="param1" value="Hey!"/>
but I'm not 100% sure if this is correct for adding parameters accessible by the XSLT.
Thanks.

The properties defined for the XsltAction class are properties specific to that action class and are not related to parameters in the template file.
So in short, it's not possible to pass parameters to the xslt from the JBoss ESB action pipeline. However, it would be possible to create a custom action that decorates your ESB message with data you define as a property in your jboss-esb.xml file and insert that before your XsltAction. That may be what you're looking for.

Related

WSO2 Call Template Mediator how to get a variable

I'm trying to use Call Template Mediator in WSO2 and I'm trying to get a dynamic value inside function, but I don't find the way to get it.
For example:
<property expression="$ctx:variable" name="test" type="STRING"/>
<call-template target="HelloWorld_Logger">
<with-param name="message" value="VARIABLE: " expression="$ctx:test" />
</call-template>
I'm not able to get the property "test", if i remove value field the IDE reports an error.
There is some way to get a property inside <call-template> function?
The Call Template Mediator doesn't have a parameter called expression to pass dynamic values. You must pass the XPath expression within {} to the value parameter itself. In your case, the call-template will be as follows,
<call-template target="HelloWorld_Logger">
<with-param name="message" value="{$ctx:test}"/>
</call-template>
Please refer to the Call Template Mediator for more information.

WSO2 ESB. Accessing Secure Vault programmaticaly

I am implementing handler for REST API in Java (org.apache.synapse.rest.Handler interface). And there is a case, when I need to access Secure Vault and get a value.
I know that you are able to achieve this by expression="wso2:vault-lookup('YOUR.KEY.HERE')" in sequence, but can't find api to do this in handler. I believe that org.apache.synapse.MessageContext can help, but not sure how.
You can use below code segment in the custom handler.
public String getSecretPassword(String alias, MessageContext messageContext){
RegistrySecretRepository regRepo = new RegistrySecretRepository();
regRepo.setSynCtx(messageContext);
return regRepo.getSecret(alias);
}
Dependency for pom.xml, the version needs to be changed according to your product version.
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.mediation.security</artifactId>
<version>4.2.0</version>
</dependency
Please refer - http://malantech.blogspot.com/2016/10/basic-authentication-handler-with.html
Thanks
I believe you will not be able to get the value of the security vault directly from your handler so I advise you to recover the password and put it in a property and inside your handler to retrieve the property.
<property name="passwordvault"
expression="wso2:vault-lookup('YOUR.KEY.HERE')"
scope="default"/>
And use the MessageContext to get the propertie like this:
context.getProperty("passwordvault");
That's just a workaround which is not advisable , i believe you can try below code as i have used similar earlier as well and it worked
<property expression="wso2:vault-lookup('ei.training.userid')" name="UserID" scope="default" type="STRING"/>
<log>
<property expression="wso2:vault-lookup('ei.training.userid')" name="UID"/>
</log>
And I will answer my own question.
I've created a dummy sequence and placed it into Registry
<sequence name="SecureVaultSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="wso2:vault-lookup('MY.PASS')" name="NAME"
scope="default" type="STRING"
xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
</sequence>
Then in my handler i retrieved it like this:
messageContext.getConfiguration().getSequence("conf:Resources/sequences/SecureVaultSeq.xml").mediate(messageContext);
key = (String) messageContext.getProperty("NAME");
Hope this will help someone.

replace function with get-property in wso2esb

I am trying the below option and i m getting error, can anyone suggest me the solution. I have added synapse.xpath.dom.failover.enabled=true
Code:trying to replace ~TOKEN~ with the property value
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
Error:
TID: [-1234] [] [2017-02-15 00:14:19,318] ERROR {org.apache.synapse.util.xpath.SynapseXPath} - Evaluation of the XPath expression fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code')) resulted in an error {org.apache.synapse.util.xpath.SynapseXPath} net.sf.saxon.trans.XPathException: Unknown system function get-property()
get-property is not a standard xpath function and you must say to the xpath engine that this function comes from synapse :
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', syn:get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
You do not need to add the definition of 'syn' namespace, it is well known in your mediation (xmlns:syn="http://ws.apache.org/ns/synapse")
Two things need to be done:
A. Review this and ensure your Synapse config is set correctly. Is there any replace function in wso2esb?
B. Then you need to add prefix fn & syn if you need to use methods like get-property() with XPath 2.0. Also note, you must use (include) the following namespaces values for ‘syn’ and ‘fn’.
xmlns:syn=”http://ws.apache.org/commons/ns/payload”
xmlns:fn=”http://www.w3.org/2005/xpath-functions”
Here is a sample property mediator using replace.
<property xmlns:syn="http://ws.apache.org/commons/ns/payload" xmlns:fn="http://www.w3.org/2005/xpath-functions" name="xmlValue" expression="fn:replace(syn:get-property('textValue'), 'xmlData=', '')></property>;

How to limit length of properties in logs in WSO2 ESB?

I need to cut body content in logs in WSO2 ESB. For this purpose, I have two parameters in registry: enabled (true or false) and limit (integer, for example: 1024).
For that, I have next expression in a property:
<property name="BODY_TO_LOG" expression="fn:substring(get-property('default','BODY'),0,getproperty('default','LIMIT'))" type="STRING" scope="default"/>
Note that BODY and LIMIT properties have been set previously.
My problem is it does not limit in logs, although the value of these properties are right.
Is it correct that form of limit the content of property?
Your expression should work. I have tested it and came to the nonsensical conclusion that when you declare your LIMIT property with type INTEGER, it doesn't log anything. When you declare your LIMIT property without a type - it works. If you declare your LIMIT property with a type STRING it also works.
So, basically your declaration of the LIMIT property should resemble the following:
<property name="LIMIT" type="STRING" value="1024" scope="default"/>
or
<property name="LIMIT" value="1024" scope="default"/>

WSO2 ESB Collout address from Property or XPath-expression

I have the config with development and production sections. This sections contain the URLs of backends. In my inSequence i need it to Callout to these backends several times per request.
<config>
<env>prod</env>
<backend env="prod">http://localhost:1234/</backend>
<backend env="dev">http://localhost:2345/</backend>
</config>
I read this config from Local Entry (as XML) and want to set Callout's URL as an Property.
I don't want to hardcode these backends inside my code with "Switch" statement, because it's possible to use more than two environments.
Could you please show me an example?
Thank you.
You can read xml file in registry. Simply define property of OM type like this:
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM" />
Then you can see the value by logging like this:
<log level="custom"> <property name="test.b" expression="$ctx:test//b" /> </log>
And in the xml file that you have put in the root of registry, you would fill it like:
<a>Hello<b>WSO2</b></a>
I have learned it from this link.
I found the answer. According to source of Callout mediator:
CalloutMediator.java
It uses "To" header if URL is not specified.