1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.util.storage;
18
19 import java.io.Serializable;
20 import java.util.concurrent.locks.ReentrantLock;
21
22 import org.joda.time.DateTime;
23 import org.opensaml.xml.util.DatatypeHelper;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28
29
30
31
32
33
34 public class ReplayCache {
35
36
37 private final Logger log = LoggerFactory.getLogger(ReplayCache.class);
38
39
40 private StorageService<String, ReplayCacheEntry> storage;
41
42
43 private String partition;
44
45
46 private long entryDuration;
47
48
49 private ReentrantLock cacheLock;
50
51
52
53
54
55
56
57 public ReplayCache(StorageService<String, ReplayCacheEntry> storageService, long duration) {
58 storage = storageService;
59 entryDuration = duration;
60 partition = "replay";
61 cacheLock = new ReentrantLock(true);
62 }
63
64
65
66
67
68
69
70
71 public ReplayCache(StorageService<String, ReplayCacheEntry> storageService, String storageParition, long duration) {
72 storage = storageService;
73 entryDuration = duration;
74 if (!DatatypeHelper.isEmpty(storageParition)) {
75 partition = DatatypeHelper.safeTrim(storageParition);
76 } else {
77 partition = "replay";
78 }
79 cacheLock = new ReentrantLock(true);
80 }
81
82
83
84
85
86
87
88
89
90
91 public boolean isReplay(String issuerId, String messageId) {
92 log.debug("Attempting to acquire lock for replay cache check");
93 cacheLock.lock();
94 log.debug("Lock acquired");
95
96 try {
97 boolean replayed = true;
98 String entryHash = issuerId + messageId;
99
100 ReplayCacheEntry cacheEntry = storage.get(partition, entryHash);
101
102 if (cacheEntry == null || cacheEntry.isExpired()) {
103 if (log.isDebugEnabled()) {
104 if (cacheEntry == null) {
105 log.debug("Message ID {} was not a replay", messageId);
106 } else if (cacheEntry.isExpired()) {
107 log.debug("Message ID {} expired in replay cache at {}", messageId, cacheEntry
108 .getExpirationTime().toString());
109 storage.remove(partition, entryHash);
110 }
111 }
112 replayed = false;
113 addMessageID(entryHash, new DateTime().plus(entryDuration));
114 } else {
115 log.debug("Replay of message ID {} detected in replay cache, will expire at {}", messageId, cacheEntry
116 .getExpirationTime().toString());
117 }
118
119 return replayed;
120 } finally {
121 cacheLock.unlock();
122 }
123 }
124
125
126
127
128
129
130
131 protected void addMessageID(String messageId, DateTime expiration) {
132 log.debug("Writing message ID {} to replay cache with expiration time {}", messageId, expiration.toString());
133 storage.put(partition, messageId, new ReplayCacheEntry(expiration));
134 }
135
136
137 public class ReplayCacheEntry implements ExpiringObject, Serializable {
138
139
140 private static final long serialVersionUID = 2398693920546938083L;
141
142
143 private DateTime expirationTime;
144
145
146
147
148
149
150 public ReplayCacheEntry(DateTime expiration) {
151 expirationTime = expiration;
152 }
153
154
155 public DateTime getExpirationTime() {
156 return expirationTime;
157 }
158
159
160 public boolean isExpired() {
161 return expirationTime.isBeforeNow();
162 }
163
164
165 public void onExpire() {
166
167 }
168 }
169 }