forked from knastnt/JavaRush-Training-Topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationUtil.java
More file actions
92 lines (77 loc) · 3.09 KB
/
ValidationUtil.java
File metadata and controls
92 lines (77 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ru.javawebinar.topjava.util;
import org.slf4j.Logger;
import ru.javawebinar.topjava.HasId;
import ru.javawebinar.topjava.util.exception.ErrorType;
import ru.javawebinar.topjava.util.exception.IllegalRequestDataException;
import ru.javawebinar.topjava.util.exception.NotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.*;
import java.util.Set;
public class ValidationUtil {
private static final Validator validator;
static {
// From Javadoc: implementations are thread-safe and instances are typically cached and reused.
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
// From Javadoc: implementations of this interface must be thread-safe
validator = factory.getValidator();
}
private ValidationUtil() {
}
public static <T> void validate(T bean) {
// https://alexkosarev.name/2018/07/30/bean-validation-api/
Set<ConstraintViolation<T>> violations = validator.validate(bean);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
public static <T> T checkNotFoundWithId(T object, int id) {
checkNotFoundWithId(object != null, id);
return object;
}
public static void checkNotFoundWithId(boolean found, int id) {
checkNotFound(found, "id=" + id);
}
public static <T> T checkNotFound(T object, String msg) {
checkNotFound(object != null, msg);
return object;
}
public static void checkNotFound(boolean found, String arg) {
if (!found) {
throw new NotFoundException(arg);
}
}
public static void checkNew(HasId bean) {
if (!bean.isNew()) {
throw new IllegalRequestDataException(bean + " must be new (id=null)");
}
}
public static void assureIdConsistent(HasId bean, int id) {
// conservative when you reply, but accept liberally (http://stackoverflow.com/a/32728226/548473)
if (bean.isNew()) {
bean.setId(id);
} else if (bean.id() != id) {
throw new IllegalRequestDataException(bean + " must be with id=" + id);
}
}
// http://stackoverflow.com/a/28565320/548473
public static Throwable getRootCause(Throwable t) {
Throwable result = t;
Throwable cause;
while (null != (cause = result.getCause()) && (result != cause)) {
result = cause;
}
return result;
}
public static String getMessage(Throwable e) {
return e.getLocalizedMessage() != null ? e.getLocalizedMessage() : e.getClass().getName();
}
public static Throwable logAndGetRootCause(Logger log, HttpServletRequest req, Exception e, boolean logException, ErrorType errorType) {
Throwable rootCause = ValidationUtil.getRootCause(e);
if (logException) {
log.error(errorType + " at request " + req.getRequestURL(), rootCause);
} else {
log.warn("{} at request {}: {}", errorType, req.getRequestURL(), rootCause.toString());
}
return rootCause;
}
}