read value from property file inside xpath - xpath

Below First part is my code, i have to read my substring logic from xpath.But I dont want the above code to be in my camel context insead i want to read from property file, i wish i could do something like below in second part where substring logic is a key in property file. I am loading my property file from BrigePropertyPlaceholder by Spring.
<xpath>starts-with(substring-after((/*/FullName/text()), 'Mr.'), "Xyz")
</xpath>
<camelContext> ....
<xpath>{properties:substringlogic}</xpath>
</camelContext>

Try with
<xpath>{{substringlogic}}</xpath>
See more details in the Camel docs: http://camel.apache.org/using-propertyplaceholder.html

Related

Issue on Camel route - parsing XML tags

I have a complex camel route, which starts with an initialization route, which tries to set the headers with the info from the XML used as input.
I wonder how the route is not being able to parse the XML content, using XPath.
Before calling the route, I print the xml information in my java JUNIT, and it prints correctly, with all xml tags.
So I know the information is being sent as I am expecting.
But that route, which should set the headers using XPath, returns empty to any expression I try to use! I even used a XPath tool to assist me (https://codebeautify.org/Xpath-Tester), to check if was some xpath coding mistake, but I get the results I want from there.
So, let's suppose, I have an XML as:
<bic:Test>
<bic:context>
<bic:memberCode>GOOGLE</bic:memberCode>
</bic:context>
</bic:Test>
So, with the line below:
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag>/text()</xpath>
</setHeader>
or
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag></xpath>
</setHeader>
I will see the header with empty content.
I tried so many different things, that finally I decided to print the all the content, using an XPath expression as /.
It will print only the content ("GOOGLE"), not the tags.
Could you please assist me?
Thank you in advance!
This is probably a namespace related issue.
You have to define the bic namespace in the camel context and then use it in the xpath expression.
Have a look at the documentation in https://github.com/apache/camel/blob/master/camel-core/src/main/docs/xpath-language.adoc and particularly in the example of "Using XML configuration"
Also look at "Namespace auditing to aid debugging" for further information about debugging namespace related issues in camel.

Mule ESB: How to use xpath expression on session variable?

I have a requirement to extract some information form session variables. The session variables Type is String and contains XML. i tried XPATH expression like -
xpath('//tns:CreateUpdateRQ/tns:RequestInfo/sns:requestSourceID',sessionVars.originalMessage)
however I get exception stating "Could not find transformer NULL Payload to Document". I tried MEL like - #[sessionVars.originalMessage.CreateUpdateRQ.RequestInfo.requestID]. But it's not working either.
How can I extract information from session variable of Type string holding XML.
P.S. I can set the payload to sesion variable & use xpath but it's not suitable for my case because I need to extract the information from session variable 7 pass it as argument to datamapper.
Appreciate you help on this.
To facilitate reading the xml file, I recommend transforming String (Object) to xml, for example:
<xml:object-to-xml-transformer />
then
<logger level="WARN" message="#[xpath://soapenv:Envelope/soapenv:Body//...]" />
In short, make sure the payload is XML type, you can by setting it
<set-property propertyName="Content-Type" value="text/xml" />

JSP Global Variable using Spring

I have a date format at I wish to set globally for my jsp fmt tag to use.
May I know what is right approach to perform this?
I will wish to configure this value in future
pattern = "dd-MM-yyyy kkm"
I have read the following:
How do I create a global JSP variable that I can access across multiple pages or inside frames/iframes?
I think using applicationContext is right approach. You can set your date format at your "welcomepage.jsp",
String pattern = "dd-MM-yyyy kkm";
application.setAttribute("ApplicationPattern",pattern);
get and use the same format wherever required.
String pattern2 =(String)getServletContext().getAttribute("ApplicationPattern");
If you want to set the default data-format pattern for entire app, you could use ServletContext.setAttribute(), but you need a place to do this work, set it in a jsp page or use a initialize filter for your app.
Or you create a Utlitiy class with a static method(field) definition to get the default pattern, then from your fmt
tag use a method call to reference the default pattern

In a blueprint.xml route, call a propertyplaceholder using an xpath returned value

I have a blueprint.xml in which I write some routes for an ESB.
I want to get values from an XML file passed into the route.
I want to then use these values to make up a dynamic property key name and call the properties file and get the matching property (all within the route). I want to avoid having to create a Java processor due to the overhead of instantiating this each time.
Essentially I want to do this:
<from uri="file:C:/myfilelocation?"/>
<to uri= {{<xpath>//company</xpath>+<xpath>//branch</xpath>}}/>
So in blueprint you call a property using {{}}
I am trying to place the xpath values as the property key inside of the property {{}} tags. In my properties file I have a mapping for each company/branch combination like so:
company1branch1=http://thiscompany.com
company2branch2=http://someothercompany.com
Any way to do this, e.g. some sort of escape characters?
The < to > is for static uris, if you want to use dynamic runtime computed uris, then you should use the recipient list EIP: http://camel.apache.org/recipient-list.html that allows to send a message to a recipient calculcated at runtime.
This is also describer in this FAQ: http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html
Though with the xpath, you would need to set them as headers first.
Something a like:
<setHeader headerName="company">
<xpath resultType="java.lang.String">/xxxx</xpath>
</setHeader>
...
<recipientList><simple>{{${header.company}${header.branch}}}</simple></recipientList>
Also the recipient list can send to 2+ destinations, the separator is by default comma. But you can configure that. See the links above.

How do I do substitutions for localised strings in freemarker

I'm using spring and freemarker and have the basics working.
I've got a properties file like
help.text=For further information please see the help page.
I'm currently outputting localised messages using
${rc.getMessage("help.text")}
However I'm having trouble figuring out how I can pass in my substitution variables. Can you help?
Cheers,
Peter
If I read the Spring API documentation about RequestContext (your rc?) correctly, then
${rc.getMessage("help.txt", ["yourHelpUrl"])}
might work, because getMessage can receive an additional List argument with message args, which you can supply via a FreeMarker sequence.
<#import "/spring.ftl" as spring/>
<#assign args = ["yourHelpUrl"]>
<#spring.messageArgs "help.txt" args/>
I always do the substitution variables in my Java code somewhere and then dump the fully localized text into a Map where it's accessed by Freemarker like this:
${localizedValues["help.txt"]}
If you are correctly configuring freemarker in the spring MVC context then the right way to do it is:
Import the spring macros in the template
Use the mensaje macro
<#import "/spring.ftl" as spring />
<#spring.messageText "code", "Default message"/>
See the documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-velocity

Resources