How to extract xpath list in camel - java

My requirement is as below:
I have a properties file which has values as:
/message/header/messagetype ='DATA'
So the XPaths are the keys and the values are the dates I want to check in Camel at runtime
I read an ActiveMQ queue which gets me an XML message. Now at runtime I need to check the respective XPaths from the properties file and check there respective values in the XML message I get from the queue. There could be any number of XPath checks defined in the properties file. This is defined by the business needs and the code should take care of it.

There is no standard way to read a properties file and "transform" it to XPath checks. However, you can always use a plain Java Bean and call it from your Camel route. For example like this:
from()
...
.bean(YourBean.class)
...
With this you can use Java code as a "component" in your Camel route.
For more details see the Camel docs about Bean binding and the Bean component.

Thank you for the help. I figured out how to do this by using Predicate functionality in Camel.So what I am doing now is, creating multiple Predicate objects based on my property file entries and adding them to 'PredicateBuilder' as below ::
PredicateBuilder.and(p1,p2....pn);
This returns me an object of Predicate which I can use in when() of Camel.
for eg. choice().
when(predicateBuilder()).

Related

How to read a spring property value straight into an xml file

I have an xml file that contains values that define a java object, the values are processed via a java method to handle some specific tasks, the xml file has the below architecture :
<javaObject>
<attr1>value1</attr1>
<attr2>${property.name}</attr2>
<attr3>value3</attr3>
</javaObject>
My goal is to get the attr2 from the property file, I've tried ${property.name} but it's not working, I've also tried
<property name="property.name" value="${property.name} />
At runtime, I get a NULL when I call the value of attr2
What is the best way to implement that ?
If I understand your question correctly, you unmarshal the object and want to work with the value of proeprty.name?
You can inject the value of property.name in your class via
#Value("${property.name}")
String propertyName

Dynamic java business rule validation

I am struggling to fit below use case.
Requiremnet:Dyamic comparison tool.Input to method is a json and then map it java class and then run some validation and persist it to database along with the result of the validation.
This looks simple when your json is predefined.You can create a java class and write code to do the required validation.
My use case is to be able to handle any kind of json and then create a beam dynamically and run some rule on it on the go
Now for example
Json 1:Student info json which has information about name,class,marks.
Validation:Persist data to DB only if marks>50
Json 2:Order info json which has information about order id,price,order type.
Validation:Persist data to DB only if order type= shoe.
Things i need to do:
Step 1: create a test file that has info regarding data type of validation object and the validation condition.
(Only way i think i can pass validation rule dynamically.Like how Apache does it on log stash.
Eg: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html)
Step 2:Pass this test file while compiling your spring boot project.
Sample text file format
input{
name:String
class:String
grade:String
}
ValidationRule{
marks>50
}
output{
//if in case you don't want to persist all the data to db.You can mention which
field to use.
}
Now with the help of this text file i am assuming that java can create a bean.Then apply business rule to it.

How to use jolokia to retrieve mbean for a class

I have a mbean for a class say foo.bar.Log4j
and I want to use jolokia to list all loggers?
I have tried reading on https://jolokia.org/reference/pdf/jolokia-reference.pdf but that tells me how to get values of predefined java.memory etc
Please suggest on how to get jolokia to retrieve loggers for a user-defined class
You have to keep in mind that even if your mbean is a singleton in your servlet, your servlet could be running on multiple endpoints - that's why the namespace alone is not sufficient to identify your mbean instance.
If you want to get all instances of foo.bar.Log4j, you can use the read endpoint like this:
http://yourserver/jolokia/read/foo.bar.Log4j:*
In general, you can get a list of all your available mbeans like this:
http://yourserver/jolokia/list
You should end up with a large json document that contains everything you might want to fetch. You will see things like
"foo.bar.Log4j": {
"name=foo,type=MyLogger": {
"desc": ...
"attr": {
...
}}}
You can now get the attributes using something like this:
http://yourserver/jolokia/read/foo.bar.Log4j:type=name=foo,type=MyLogger
In addition to type and name, you may see other fields as well, for example context or id. This a:b key is the Java ObjectName for your mbean.

Dynamic key in bean:message in Struts 1.3

I am working on a project which made use of an old (but nice) framework Struts 1.3, and I am trying to accomplish a simple task.
I have a bean containing a property, which is a key corresponding to a property file. I would like to use it for recall the appropriate translation.
Variable in property file
props.myprop.sample=This is my sample property
The property is in a bean passed to the jsp called for convenience AllProps which has a getter for the property, and this should be a pseudo code:
<bean:define id="sample" name="AllProps" property="sample" type="java.lang.String"/> // should result in sample = props.myprop.sample
<div><bean:message key="sample"/></div>
Which should output:
<div>This is my sample property</div>
But obviously result in a property not found, can you give me help on how to deal with this ?
I would like to stick as much as possible to Struts tag, then Jsp tag, and scriptlet as last resource.
Thanks
Straight from the documentation:
<bean:message>
Render an internationalized message string to the response.
Retrieves an internationalized message for the specified locale, using
the specified message key, and write it to the output stream. Up to
five parametric replacements (such as "{0}") may be specified.
The message key may be specified directly, using the key attribute, or
indirectly, using the name and property attributes to obtain it from a
bean.
(emphasis mine)
So, all you need is
<bean:message name="AllProps" property="sample"/>

Spring "typemismatch" and required fields

In the context of Spring Webflow 2.0.x......
I handle form binding "typemismatches", i.e. as a result of trying to map a String onto a Integer field, by using the following in my messages.properties
typeMismatch={0} contains invalid data.
This works fine.
The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)
So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....
Like Yves mentioned, among the three approaches, i have used a custom validator method and its very easy. You can use a custom validator which checks if the form field already has a xml error message of required. If the field does not have an error, then you can check for your string validation. That way it will display only one.
The other method that you could use is try a multiple xml validation, one being required and the other one being a mask which checks for a particular regular expression. In your case if your field is an integer field, then you can go and perform a mask with regex checking for only numbers. The order of mask, required or required, mask in the xml decides which message gets a higher preference.
For example:
<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
<var-name>mask</var-name>
<var-value>${somepattern}</var-value>
</var>
</field>
You have many options, in order of preference:
Set selectively the message typeMismatch.target.yourFieldName or typeMismatch.int in resources files
Implement your own Validator so that you can send a dedicated message when Integer parsing will fail before the binding step
Create a BindingErrorProcessor to handle different kind of parsing issues

Categories

Resources