I have a requirement where in I need to time various method calls into a time series db.
For the same, I have created 2 annotations one for the method call:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface Auditable {
String event();
String entity();
}
and another one for a field
#Target(ElementType.FIELD)
#Retention(RetentionPolicy.RUNTIME)
public #interface Id {
String id();
}
The reason I need the #ID annotation is that, the id field to be pushed to influx db would be known only at run time.
So, in my method, something like this would happen:
#Id
String key;
#Auditable(event="xxx",entity="yyy")
public void methodToBeIntercepted(){
String key = <logic to generate key>;
}
The idea that I wanted to use was add an annotation advice along with a field set advice.
#After("#annotation(auditable) && (set(#<package>.ID java.lang.String sample..*.*) && args(id))")
public void pointcutMethod(Auditable auditable,String id){
}
But the flow is never entering into the pointCutMEthod. If I change the condition to || above, then it enters but it clearly suggests that only 1 condition would be true at any given point of time.
What is it that I am doing wrongly here?
Your analysis is correct: The advice will never trigger. It just cannot because the two pointcuts you combine are mutually exclusive: Where #Auditable is (method call or execution) is a different joinpoint from set(). What you intend to express is the following: "Intercept member variable assignment within the control flow of a method execution." I.e. you need cflow(#annotation(auditable)).
Annotations and driver application:
package de.scrum_master.app;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target(ElementType.FIELD)
#Retention(RetentionPolicy.RUNTIME)
public #interface Id {
String id();
}
package de.scrum_master.app;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface Auditable {
String event();
String entity();
}
package de.scrum_master.app;
public class Application {
#Id(id = "my ID")
String key;
public static void main(String[] args) {
Application application = new Application();
application.methodToBeIntercepted();
}
#Auditable(event = "xxx", entity = "yyy")
public void methodToBeIntercepted() {
key = "I am the key";
}
}
Aspect:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import de.scrum_master.app.Auditable;
#Aspect
public class MyAspect {
#After("cflow(#annotation(auditable)) && set(#de.scrum_master.app.Id String de.scrum_master..*.*) && args(id)")
public void pointcutMethod(JoinPoint thisJoinPoint, Auditable auditable, String id) {
System.out.println(thisJoinPoint);
System.out.println(" " + auditable);
System.out.println(" " + id);
}
}
Console log:
set(String de.scrum_master.app.Application.key)
#de.scrum_master.app.Auditable(event=xxx, entity=yyy)
I am the key
Related
I made a registration form and I want to validate all filed of the form I validated expect one field to match the fields of PASSWORD matching so make custom validtion but is not working i attaced code in
#Entity
public class Userlist {
......
#Size(min = 8, message = "Please enter atleast 8 digit password")
private String userpassword;
#PasswordMatch(message="Your Password is not match with created password")
private String confirmpassword;
}
package com.picture.picturesalbum.anotation;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import java.lang.annotation.*;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
#Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Constraint(validatedBy = PasswordMatchValidator.class)
public #interface PasswordMatch {
public String message() default "Your Password is not match with created password ";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}
package com.picture.picturesalbum.anotation;
import com.picture.picturesalbum.model.Userlist;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class PasswordMatchValidator implements ConstraintValidator<PasswordMatch, String> {
Userlist userlist = new Userlist();
public boolean isValid(String value, ConstraintValidatorContext context) {
// Userlist userlist = new Userlist();
if (value.contentEquals(userlist.getUserpassword())) {
return true;
} else {
return false;
}
}
}
Error is
at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.length()" because "cs" is null
If you want to validate two fields of a class, you have to use a custom validator with a class level constraint and then compare both values in the validator class.
Check this answer for more information.
Another solution is to define a method that must validate to true and put the #AssertTrue annotation on the top of it:
#AssertTrue
private boolean isEqual() {
return userpassword.equals(confirmPassword);
}
I would like to create a custom annotation in my Spring Boot application which always adds a prefix to my class level RequestMapping path.
My Controller:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
#MyApi("/users")
public class UserController {
#GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}
My custom annotation
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#RestController
#RequestMapping(path = "/api")
public #interface MyApi {
#AliasFor(annotation = RequestMapping.class)
String value();
}
GOAL: a mapping like this in the end: /api/users/stackoverflow
Notes:
server.servlet.context-path is not an option because I want to create
several of these
I'm using Spring Boot version 2.0.4
I was not able to find an elegant solution for the issue. However, this worked:
Slightly modified annotation, because altering behavior of value turned out to be more difficult.
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#RestController
#RequestMapping
public #interface MyApi {
#AliasFor(annotation = RequestMapping.class, attribute = "path")
String apiPath();
}
Bean Annotation Processor
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
#Component
public class MyApiProcessor implements BeanPostProcessor {
private static final String ANNOTATIONS = "annotations";
private static final String ANNOTATION_DATA = "annotationData";
public Object postProcessBeforeInitialization(#NonNull final Object bean, String beanName) throws BeansException {
MyApi myApi = bean.getClass().getAnnotation(MyApi.class);
if (myApi != null) {
MyApi alteredMyApi = new MyApi() {
#Override
public Class<? extends Annotation> annotationType() {
return MyApi.class;
}
#Override
public String apiPath() {
return "/api" + myApi.apiPath();
}
};
alterAnnotationOn(bean.getClass(), MyApi.class, alteredMyApi);
}
return bean;
}
#Override
public Object postProcessAfterInitialization(#NonNull Object bean, String beanName) throws BeansException {
return bean;
}
#SuppressWarnings("unchecked")
private static void alterAnnotationOn(Class clazzToLookFor, Class<? extends Annotation> annotationToAlter, Annotation annotationValue) {
try {
// In JDK8 Class has a private method called annotationData().
// We first need to invoke it to obtain a reference to AnnotationData class which is a private class
Method method = Class.class.getDeclaredMethod(ANNOTATION_DATA, null);
method.setAccessible(true);
// Since AnnotationData is a private class we cannot create a direct reference to it. We will have to manage with just Object
Object annotationData = method.invoke(clazzToLookFor);
// We now look for the map called "annotations" within AnnotationData object.
Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS);
annotations.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(annotationData);
map.put(annotationToAlter, annotationValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Controller:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
#MyApi(apiPath = "/users")
public class UserController {
#GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}
Using Spring Boot, I've created an example application.
package hello;
import org.springframework.web.bind.annotation.RestController;
import constraint.CheckHelloId;
import dto.HelloDto;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#RestController
public class HelloController {
#RequestMapping(value = "/", method = RequestMethod.POST)
public String index(#RequestBody #Valid final HelloDto hello) {
hello.setId(null);
validateFromMethodHeader(hello);
return "Greetings from Spring Boot!";
}
private void validateFromMethodHeader(#CheckHelloId final HelloDto helloDto) {
System.out.println("Validating DTO...");
}
}
I'm trying to add a custom constraint to the DTO HelloDto in the private method that checks if the id field is null or not.
The interface:
package constraint;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
#Target({ FIELD, ANNOTATION_TYPE, TYPE, METHOD, PARAMETER })
#Retention(RUNTIME)
#Constraint(validatedBy = CheckHelloIdValidator.class)
#Documented
public #interface CheckHelloId {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The validator:
package constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraintvalidation.SupportedValidationTarget;
import javax.validation.constraintvalidation.ValidationTarget;
import org.apache.commons.lang3.StringUtils;
import dto.HelloDto;
#SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class CheckHelloIdValidator implements ConstraintValidator<CheckHelloId, HelloDto> {
private static final String ID_VALIDATION_ERROR = "Null or blank ID.";
#Override
public void initialize(CheckHelloId arg0) {
}
#Override
public boolean isValid(HelloDto helloDto, ConstraintValidatorContext context) {
if (StringUtils.isBlank(helloDto.getId())){
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(ID_VALIDATION_ERROR).addConstraintViolation();
return false;
}
return true;
}
}
The DTO:
package dto;
import java.io.Serializable;
public class HelloDto implements Serializable {
private static final long serialVersionUID = 8792903048191496378L;
private String id;
private String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
As seen in the private method at the controller, I'm trying to apply the validation of the DTO at a parameter level (I know I can just do it at the index via entity validation, but I want to test this concrete case, that's why I set the id field as null at the controller).
When I run the application and perform the call, the constraint does not apply in the private method, even when the id field is null. Could anybody shed some light on this? Thank you in advance.
To validate consuming json on rest controller beside annotated method parameters #RequestBody #Valid final HelloDto hello you have to annotate filed of your json data class with special constraints. For you case it enough to use hibernate validation constraints #NotNull or #NotEmpty:
package dto;
import java.io.Serializable;
public class HelloDto implements Serializable {
private static final long serialVersionUID = 8792903048191496378L;
#NotEmpty
private String id;
I have made custom annotation for validation of my bean field. I use #Age(value = 10) annotation for validation age. I have write code as below.
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
#Documented
#Constraint(validatedBy = AgeConstraintValidator.class)
#Target( { ElementType.METHOD, ElementType.FIELD })
#Retention(RetentionPolicy.RUNTIME)
public #interface Age {
String message() default "{Age is not valid }";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int value();
}
This is code for age constrain validator
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class AgeConstraintValidator implements ConstraintValidator< Age, Long> {
private Age age;
#Override
public void initialize(Age age) {
this.age = age;
}
#Override
public boolean isValid(Long dob, ConstraintValidatorContext context) {
System.out.print(" age in annotion");
if(dob != age.value()){
return true;
}
return false;
}
}
Now when i use #Age( value = 10) in my bean so it is not called Age annotation. Can anyone tell me any fault in my code. When i create my bean object and assign age differ for test but i can not get any validation on bean 's field .
Spring will not take this custom annotation automatically. You have to let Spring know about by defining a BeanPostProcessor. create a class which implements it
For e-g
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
and mention about this bean in your spring config xml as below
<bean class="<your pack>.InitHelloWorld" />
I want to validate two fields of a Request Class in manner that Either one field is valid OR another field is valid.
Eg:
Request Bean
public class CarRequest {
#NotEmpty
private String customerName;
#NotEmpty
private String customerId;
Controller Method
public #ResponseBody CarResponse addCar(
#ModelAttribute #Valid CarRequest request, BindingResult results)
throws RuntimeException, ValidationException {
if (results.hasErrors()) {
LOG.error("error occured while adding the car");
throw new ValidationException(
"Error Occoured while validiating car request");
}
}
Here I want to check that either customerName should be NotEmpty OR customerId should be NotEmpty. then my validation should pass. How can I implement it . Please suggest!!
You need to create custom validator to validate multiple fields.
create a custom annotation:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
#Documented
#Constraint(validatedBy = CarRequestValidator.class)
#Target({ ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface RequestAnnotation {
String message() default "{RequestAnnotation}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
create a custom validator:
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CarRequestValidator implements
ConstraintValidator<RequestAnnotation, CarRequest> {
#Override
public void initialize(RequestAnnotation constraintAnnotation) {
}
#Override
public boolean isValid(CarRequest value, ConstraintValidatorContext context) {
// validation logic goes here
return false;
}
}
Now, annotate your model with custom annotation:
#RequestAnnotation
public class CarRequest {
private String customerName;
private String customerId;
}