1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.util;
21
22 import java.time.Instant;
23 import java.time.format.DateTimeFormatter;
24
25 public final class DateUtil {
26
27 private static final org.slf4j.Logger LOG =
28 org.slf4j.LoggerFactory.getLogger(DateUtil.class);
29
30 private static final DateTimeFormatter MILLISECOND_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
31
32 private static final DateTimeFormatter SECOND_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
33
34 private DateUtil() {
35
36 }
37
38
39
40
41
42
43
44
45
46 public static boolean verifyCreated(
47 Instant created,
48 int timeToLive,
49 int futureTimeToLive
50 ) {
51 if (created == null) {
52 return true;
53 }
54
55 Instant validCreation = Instant.now();
56 if (futureTimeToLive > 0) {
57 validCreation = validCreation.plusSeconds(futureTimeToLive);
58 }
59
60 if (created.isAfter(validCreation)) {
61 LOG.warn("Validation of Created: The message was created in the future!");
62 return false;
63 }
64
65
66 validCreation = Instant.now().minusSeconds(timeToLive);
67
68
69 if (created.isBefore(validCreation)) {
70 LOG.warn("Validation of Created: The message was created too long ago");
71 return false;
72 }
73
74 LOG.debug("Validation of Created: Everything is ok");
75 return true;
76 }
77
78 public static DateTimeFormatter getDateTimeFormatter(boolean milliseconds) {
79 if (milliseconds) {
80 return MILLISECOND_FORMATTER;
81 }
82 return SECOND_FORMATTER;
83 }
84 }