I have following custom ConstraintValidator:
public class FileNamePatternValidator implements ConstraintValidator<FilenamePattern, FileUpload> {
String regex;
@Override
public void initialize(FilenamePattern constraintAnnotation) {
regex = constraintAnnotation.regex();
}
@Override
public boolean isValid(FileUpload fileUpload, ConstraintValidatorContext context) {
if (fileUpload == null) {
return true;
}
// Check if filename of fileUpload is correct
return true;
}
}
Now I was wondering if I put this FilenamePattern on a parameter inside a method like this
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{xy}")
public Response saveXY(
@PathParam("xy")
String xy,
@RestForm("txtFile")
@FilenamePattern(regex = "^[a-zA-Z_\.]+\.txt$")
FileUpload txtFile
) {
// Do something with file
return Response.ok().build();
}
which can be called in parallel. Does this mean that I do not need to worry about thread safety of my ConstrainValidator. Because if jakarta ee handles ConstraintValidator inside container as singletons then field String regex;
is shared resource and I need to save regex value as ThreadLocal.