Why do we have to specify "methodParams" in struts validation plugin? - java

I have checked the validator source code and the examples of user-defined rule in the book "Jakarta Struts Live." When people define validator rules in validator-rules.xml, the following declaration for "methodParams" seems to be the only choice:
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
Okay, my question is that if this is the only possibility, why do we bother to specify it? Or please provide me with an example if this declaration is not the only choice.
Thanks.

Please take a look at this link:
http://struts.apache.org/1.1/userGuide/dev_validator.html
It says:
The methodParams attribute takes a
comma separated list of class names.
The method attribute needs to have a
signature complying with the above
list. The list can be comprised of any
combination of the following:
java.lang.Object - Bean validation is
being performed on.
org.apache.commons.validator.ValidatorAction
- The current ValidatorAction being performed.
org.apache.commons.validator.Field -
Field object being validated.
org.apache.struts.action.ActionErrors
- The errors objects to add an ActionError to if the validation
fails.
javax.servlet.http.HttpServletRequest
- Current request object. javax.servlet.ServletContext - The
application's ServletContext.
org.apache.commons.validator.Validator
- The current org.apache.commons.validator.Validator
instance. java.util.Locale - The
Locale of the current user.
It seems that "methodParams" can be any combination of the listed types, and Strurs Validator Plugin will inject the instances into the validation function call accordingly.
"methodParams" can be changed.

Related

How do I get an error message from hibernate validator in a specific language on demand?

When the hibernate validator library is used for bean validation it loads its translations from a resource bundle contained within the library:
ValidationMessages_fr.properties
ValidationMessages_en.properties
ValidationMessages_es.properties
...
I have a case now where I would like to use one of these translated messages somewhere else.
This is easy enough and works:
this.hibernateBundle = ResourceBundle.getBundle("org.hibernate.validator.ValidationMessages", EN);
return hibernateBundle.getString(key);
But there is a catch. The translations can have variable parts and seem to have some kind of templating language in the translations.
Examples:
javax.validation.constraints.DecimalMax.message = must be less than ${inclusive == true ? 'or equal to ' : ''}{value}
javax.validation.constraints.Pattern.message = must match "{regexp}"
javax.validation.constraints.Size.message = size must be between {min} and {max}
We could do some basic parsing and substitute the variables but I would rather use the same methods hibernate validator already uses in its own implementation.
Does anyone have an idea what the template language is or how I can retrieve the translation and substitute the variables?
This looks like what I need but I have no idea how to call it in a "non-constraint validation context": https://www.baeldung.com/hibernate-parametermessageinterpolator

Logback add custom field/fields in all the logs

For a Spring application I want to add a custom field to the log.
Currently I use the default format but I want to add a custom field (category field) which should be present in all the logs:
W:Action D:2022-01-10 23:21:03.285 L:INFO C:c.h.l.LoggingDemoApplication F:StartupInfoLogger.java(61) Fn:logStarted T:main R: - Hello World
What are the best solution to add a custom field to the logback log?
What I studied until now are the following possible solutions:
Use marker. The disadvantage with this is that it's not scalable: if in future you need another custom field can't add another marker. Further based on some other posts the marker is best suited to mark special logs that need to be handle differently.
Use MDC.
Also using this it seems not the best solution because:
It keeps the context so if there are multiple log statements in the same function, before each logger.info() there should be MDC.put("category", "action")
The code becomes to verbose.
Create a custom convertor (link). Get the arguments from the ILoggingEvent, get argument of 0. If this is the same type as category enum, then use it. The call for this is like logger.info("Message here: {} {} {}", CatEnum.Action.getValue(), msg1, msg2, msg3).
Create some static method in which the final format is generated.
Pattern is similar to: <pattern>%m%n</pattern>
To log, something like this should be used: logger.info(customFormatter.fmtLog(CatEnum.Action.getValue(), msg)). The returned value of fmtLog should be all the info from default logging + the category field.
Are there any built in solutions to add a custom field?

Need to understand broadleaf commerce custom annotation implementation

I have been trying go through code of Broadleaf Commerce. There were multiple Custom Annotations used however I was not able to locate there Processor. Can anyone help me here. To take example #AdminPresentation it a custom annotation in package org.broadleafcommerce.common.presentation;
However how this is processed throughout the app, I was not able to locate. What I have understood till now we can use Reflection or AOP for its processing. But There was nothing for this.
Please help.
Source code - https://github.com/BroadleafCommerce/BroadleafCommerce
For a short answer, org.broadleafcommerce.openadmin.server.dao.provider.metadata.BasicFieldMetadataProvider#addMetadata is one place that processes those annotations.
On a broader level, the controllers in the openadmin will use the AdminEntityService to get ClassMetaData (all data about how a class should be displayed to an admin user). The #AdminPresentation annotation is one source of this data. The method AdminEntityServiceImpl#getClassMetadata is the main gateway for getting the ClassMetaData.
#getClassMetadata calls #inspect and eventually gets to PersistenceManager#inspect. This method uses the DynamicEntityDao to eventually get to Metadata#getFieldMetadataForTargetClass. That method gets each field of a class via reflection, and then each of those Fields is processed through the available FieldMetadataProviders. The FieldMetadataProviders turn a java.lang.reflect.Field into org.broadleafcommerce.openadmin.dto.FieldMetadata.
Any provided FieldMetadataProvider can contribute field metadata. This metadata is used in the FormBuilderService to construct the admin page.
Class References:
AdminEntityService - org.broadleafcommerce.openadmin.server.service.AdminEntityServiceImpl
PersistenceManager - org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerImpl#inspect
DynamicEntityDao - org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#getPropertiesForEntityClass
Metadata - org.broadleafcommerce.openadmin.server.dao.Metadata#getFieldMetadataForTargetClass
FieldMetadataProvider - org.broadleafcommerce.openadmin.server.dao.provider.metadata.FieldMetadataProvider, org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#fieldMetadataProviders
FormBuilderService - org.broadleafcommerce.openadmin.web.service.FormBuilderServiceImpl
Have a look to :
https://www.baeldung.com/java-custom-annotation
You will get explanations about : "default" in the Custom Annotations.
Florent COUDERT.

How do you disable trusted.packages check for Spring-Kafka JsonDeserializer?

I have legacy Spring-Scala project. I have added new event type to one of the topics and now consumers throw exceptions
Caused by: java.lang.IllegalArgumentException: The class '...' is not in the trusted packages: [java.util, java.lang, ...].
It seems that a few classes are always added by Spring. I want to disable this feature but it seems that list of classes must be empty for the code to ignore it. But I don't see how that list would ever be empty if some java packages are added by Spring itself. Any suggestions?
I got this. There is a method called .addTrustedPackages and it handles "*" argument as one that clears the internal trustedPackages list. Neat design... >.<
So the following code works
val deserializer = new JsonDeserializer[...]()
deserializer.addTrustedPackages("*")

How do I make session/cache variables available to my main.html template?

I'm using Play Framework and setting a cache value as such:
String sessionId = Scope.Session.current().getId();
Cache.set(sessionId + "_user", "Doser");
and I want to ouput the value in my main.html without adding the value to every single controller in my application.
How do I achieve this in Play?
The other option you have for this, is to create an action in your controller that uses the #Before annotation, and then add the value using renderArgs().
I answered a previous question which I think is very similar to your requirements.
Does Play Framework support "snippets"?
You should also be aware that all session variables are available within your template, by default. You can see all the implicit objects that are available in the template in the Template Documentation here -- http://www.playframework.org/documentation/1.2.2/templates#implicits.
I need to stop answering my own questions.
I've created a tag as described in the link below, and it works perfectly:
http://www.playframework.org/documentation/1.2.2/templates#tags

Categories

Resources