@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RUNTIME)
@Constraint(validatedBy = ValidLocaleValidator.class)
@Documented
public @interface ValidLocale {
String message() default "Locale";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class ValidLocaleValidator implements ConstraintValidator<ValidLocale, String> {
private Set<String> validLocales = new HashSet<String>();
@Override
public void initialize(ValidLocale constraintAnnotation) {
Locale[]locales = Locale.getAvailableLocales();
for (java.util.Locale l : locales) {
validLocales.add(l.toString());
}
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
System.out.println("is valid start");
if(!validLocales.contains(value)){
return false;
}
return true;
}
}
@GetMapping("/player-messages/messages/{messageId}/locale/{locale}")
@Timed
public ResponseEntity<List<PlayerMessageViewDTO>> getPlayerMessage(
@PathVariable Long messageId,
@PathVariable @ValidLocale final String locale) {
log.debug("Request to get MessageId:{}, Locale:{}", messageId, locale);
List<PlayerMessageViewDTO> viewDTO = playerMessageService.getPlayerMessagesByLocale(messageId, locale);
return new ResponseEntity<List<PlayerMessageViewDTO>>(viewDTO, HttpStatus.OK);
}
@GetMapping("/player-messages/messages/{messageId}/locale/{locale}")
@Timed
public ResponseEntity<List<PlayerMessageViewDTO>> getPlayerMessage(
@PathVariable Long messageId,
@PathVariable final String locale) {
log.debug("Request to get MessageId:{}, Locale:{}", messageId, locale);
//validate locale
if (!validateLocale(locale))
return ResponseEntity.badRequest().body(null);
List<PlayerMessageViewDTO> viewDTO = playerMessageService.getPlayerMessagesByLocale(messageId, locale);
return new ResponseEntity<List<PlayerMessageViewDTO>>(viewDTO, HttpStatus.OK);
}
private boolean validateLocale(@PathVariable String locale) {
//validate locale
try {
LocaleUtils.toLocale(locale);
} catch (Exception e) {
return false;
}
return true;
}