-
Notifications
You must be signed in to change notification settings - Fork 974
/
CachedResource.java
369 lines (324 loc) · 12.9 KB
/
CachedResource.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers.resource;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import io.undertow.UndertowLogger;
import io.undertow.io.IoCallback;
import io.undertow.io.Sender;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.cache.DirectBufferCache;
import io.undertow.server.handlers.cache.LimitedBufferSlicePool;
import io.undertow.server.handlers.cache.ResponseCachingSender;
import io.undertow.util.DateUtils;
import io.undertow.util.ETag;
import io.undertow.util.MimeMappings;
/**
* @author Stuart Douglas
*/
public class CachedResource implements Resource, RangeAwareResource {
private final CacheKey cacheKey;
private final CachingResourceManager cachingResourceManager;
private final Resource underlyingResource;
private final boolean directory;
private final Date lastModifiedDate;
private final String lastModifiedDateString;
private final ETag eTag;
private final String name;
private volatile long nextMaxAgeCheck;
public CachedResource(final CachingResourceManager cachingResourceManager, final Resource underlyingResource, final String path) {
this.cachingResourceManager = cachingResourceManager;
this.underlyingResource = underlyingResource;
this.directory = underlyingResource.isDirectory();
this.lastModifiedDate = underlyingResource.getLastModified();
if (lastModifiedDate != null) {
this.lastModifiedDateString = DateUtils.toDateString(lastModifiedDate);
} else {
this.lastModifiedDateString = null;
}
this.eTag = underlyingResource.getETag();
this.name = underlyingResource.getName();
this.cacheKey = new CacheKey(cachingResourceManager, underlyingResource.getCacheKey());
if (cachingResourceManager.getMaxAge() > 0) {
nextMaxAgeCheck = System.currentTimeMillis() + cachingResourceManager.getMaxAge();
} else {
nextMaxAgeCheck = -1;
}
}
@Override
public String getPath() {
return underlyingResource.getPath();
}
@Override
public Date getLastModified() {
return lastModifiedDate;
}
@Override
public String getLastModifiedString() {
return lastModifiedDateString;
}
@Override
public ETag getETag() {
return eTag;
}
@Override
public String getName() {
return name;
}
@Override
public boolean isDirectory() {
return directory;
}
@Override
public List<Resource> list() {
return underlyingResource.list();
}
@Override
public String getContentType(final MimeMappings mimeMappings) {
return underlyingResource.getContentType(mimeMappings);
}
public void invalidate() {
final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
if(dataCache != null) {
dataCache.remove(cacheKey);
}
}
public boolean checkStillValid() {
if (nextMaxAgeCheck > 0) {
long time = System.currentTimeMillis();
if (time > nextMaxAgeCheck) {
nextMaxAgeCheck = time + cachingResourceManager.getMaxAge();
if (!underlyingResource.getLastModified().equals(lastModifiedDate)) {
return false;
}
}
}
return true;
}
@Override
public void serve(final Sender sender, final HttpServerExchange exchange, final IoCallback completionCallback) {
final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
if(dataCache == null) {
underlyingResource.serve(sender, exchange, completionCallback);
return;
}
final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
final Long length = getContentLength();
//if it is not eligible to be served from the cache
if (length == null || length > cachingResourceManager.getMaxFileSize()) {
underlyingResource.serve(sender, exchange, completionCallback);
return;
}
//it is not cached yet, install a wrapper to grab the data
if (existing == null || !existing.enabled() || !existing.reference()) {
Sender newSender = sender;
final DirectBufferCache.CacheEntry entry;
if (existing == null) {
entry = dataCache.add(cacheKey, length.intValue(), cachingResourceManager.getMaxAge());
} else {
entry = existing;
}
if (entry != null && entry.buffers().length != 0 && entry.claimEnable()) {
if (entry.reference()) {
newSender = new ResponseCachingSender(sender, entry, length);
} else {
entry.disable();
}
}
underlyingResource.serve(newSender, exchange, completionCallback);
} else {
UndertowLogger.REQUEST_LOGGER.tracef("Serving resource %s from the buffer cache to %s", name, exchange);
//serve straight from the cache
ByteBuffer[] buffers;
boolean ok = false;
try {
LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers();
buffers = new ByteBuffer[pooled.length];
for (int i = 0; i < buffers.length; i++) {
// Keep position from mutating
buffers[i] = pooled[i].getBuffer().duplicate();
}
ok = true;
} finally {
if (!ok) {
existing.dereference();
}
}
sender.send(buffers, new DereferenceCallback(existing, completionCallback));
}
}
@Override
public Long getContentLength() {
//we always use the underlying size unless the data is cached in the buffer cache
//to prevent a mis-match between size on disk and cached size
final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
if(dataCache == null) {
return underlyingResource.getContentLength();
}
final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
if(existing == null || !existing.enabled()) {
return underlyingResource.getContentLength();
}
//we only return the
return (long)existing.size();
}
@Override
public String getCacheKey() {
return cacheKey.cacheKey;
}
@Override
public File getFile() {
return underlyingResource.getFile();
}
@Override
public Path getFilePath() {
return underlyingResource.getFilePath();
}
@Override
public File getResourceManagerRoot() {
return underlyingResource.getResourceManagerRoot();
}
@Override
public Path getResourceManagerRootPath() {
return underlyingResource.getResourceManagerRootPath();
}
@Override
public URL getUrl() {
return underlyingResource.getUrl();
}
@Override
public void serveRange(Sender sender, HttpServerExchange exchange, long start, long end, IoCallback completionCallback) {
final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
if(dataCache == null) {
((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback);
return;
}
final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
final Long length = getContentLength();
//if it is not eligible to be served from the cache
if (length == null || length > cachingResourceManager.getMaxFileSize()) {
((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback);
return;
}
//it is not cached yet, just serve it directly
if (existing == null || !existing.enabled() || !existing.reference()) {
//it is not cached yet, we can't use a range request to establish the cached item
//so we just serve it
((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback);
} else {
//serve straight from the cache
ByteBuffer[] buffers;
boolean ok = false;
try {
LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers();
buffers = new ByteBuffer[pooled.length];
for (int i = 0; i < buffers.length; i++) {
// Keep position from mutating
buffers[i] = pooled[i].getBuffer().duplicate();
}
ok = true;
} finally {
if (!ok) {
existing.dereference();
}
}
long endTarget = end + 1; //as it is inclusive
long startDec = start;
long endCount = 0;
//handle the start of the range
for (ByteBuffer b : buffers) {
if (endCount == endTarget) {
b.limit(b.position());
continue;
} else if (endCount + b.remaining() < endTarget) {
endCount += b.remaining();
} else {
b.limit((int) (b.position() + (endTarget - endCount)));
endCount = endTarget;
}
if (b.remaining() >= startDec) {
b.position((int) (b.position() + startDec));
startDec = 0;
} else {
startDec -= b.remaining();
b.position(b.limit());
}
}
sender.send(buffers, new DereferenceCallback(existing, completionCallback));
}
}
@Override
public boolean isRangeSupported() {
//we can only handle range requests if the underlying resource supports it
//even if we have the resource in the cache it may disappear before we try and serve it
return underlyingResource instanceof RangeAwareResource && ((RangeAwareResource) underlyingResource).isRangeSupported();
}
private static class DereferenceCallback implements IoCallback {
private final DirectBufferCache.CacheEntry entry;
private final IoCallback callback;
DereferenceCallback(DirectBufferCache.CacheEntry entry, final IoCallback callback) {
this.entry = entry;
this.callback = callback;
}
@Override
public void onComplete(final HttpServerExchange exchange, final Sender sender) {
try {
entry.dereference();
} finally {
callback.onComplete(exchange, sender);
}
}
@Override
public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
try {
entry.dereference();
} finally {
callback.onException(exchange, sender, exception);
}
}
}
static final class CacheKey {
final CachingResourceManager manager;
final String cacheKey;
CacheKey(CachingResourceManager manager, String cacheKey) {
this.manager = manager;
this.cacheKey = cacheKey;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey1 = (CacheKey) o;
if (cacheKey != null ? !cacheKey.equals(cacheKey1.cacheKey) : cacheKey1.cacheKey != null) return false;
if (manager != null ? !manager.equals(cacheKey1.manager) : cacheKey1.manager != null) return false;
return true;
}
@Override
public int hashCode() {
int result = manager != null ? manager.hashCode() : 0;
result = 31 * result + (cacheKey != null ? cacheKey.hashCode() : 0);
return result;
}
}
}