use of org.eclipse.jetty.http.ResourceHttpContent in project jetty.project by eclipse.
the class ResourceCacheTest method testResourceCache.
@Test
public void testResourceCache() throws Exception {
final Resource directory;
File[] files = new File[10];
String[] names = new String[files.length];
CachedContentFactory cache;
for (int i = 0; i < files.length; i++) {
files[i] = File.createTempFile("R-" + i + "-", ".txt");
files[i].deleteOnExit();
names[i] = files[i].getName();
try (OutputStream out = new FileOutputStream(files[i])) {
for (int j = 0; j < (i * 10 - 1); j++) out.write(' ');
out.write('\n');
}
}
directory = Resource.newResource(files[0].getParentFile().getAbsolutePath());
cache = new CachedContentFactory(null, directory, new MimeTypes(), false, false, CompressedContentFormat.NONE);
cache.setMaxCacheSize(95);
cache.setMaxCachedFileSize(85);
cache.setMaxCachedFiles(4);
assertTrue(cache.getContent("does not exist", 4096) == null);
assertTrue(cache.getContent(names[9], 4096) instanceof ResourceHttpContent);
assertTrue(cache.getContent(names[9], 4096).getIndirectBuffer() != null);
HttpContent content;
content = cache.getContent(names[8], 4096);
assertTrue(content != null);
assertEquals(80, content.getContentLengthValue());
assertEquals(0, cache.getCachedSize());
if (OS.IS_LINUX) {
// Initially not using memory mapped files
content.getDirectBuffer();
assertEquals(80, cache.getCachedSize());
// with both types of buffer loaded, this is too large for cache
content.getIndirectBuffer();
assertEquals(0, cache.getCachedSize());
assertEquals(0, cache.getCachedFiles());
cache = new CachedContentFactory(null, directory, new MimeTypes(), true, false, CompressedContentFormat.NONE);
cache.setMaxCacheSize(95);
cache.setMaxCachedFileSize(85);
cache.setMaxCachedFiles(4);
content = cache.getContent(names[8], 4096);
content.getDirectBuffer();
assertEquals(cache.isUseFileMappedBuffer() ? 0 : 80, cache.getCachedSize());
// with both types of buffer loaded, this is not too large for cache because
// mapped buffers don't count, so we can continue
}
content.getIndirectBuffer();
assertEquals(80, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[1], 4096);
assertEquals(80, cache.getCachedSize());
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[2], 4096);
content.getIndirectBuffer();
assertEquals(30, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[3], 4096);
content.getIndirectBuffer();
assertEquals(60, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[4], 4096);
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[5], 4096);
content.getIndirectBuffer();
assertEquals(90, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[6], 4096);
content.getIndirectBuffer();
assertEquals(60, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
try (OutputStream out = new FileOutputStream(files[6])) {
out.write(' ');
}
content = cache.getContent(names[7], 4096);
content.getIndirectBuffer();
assertEquals(70, cache.getCachedSize());
assertEquals(1, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[6], 4096);
content.getIndirectBuffer();
assertEquals(71, cache.getCachedSize());
assertEquals(2, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[0], 4096);
content.getIndirectBuffer();
assertEquals(72, cache.getCachedSize());
assertEquals(3, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[1], 4096);
content.getIndirectBuffer();
assertEquals(82, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[2], 4096);
content.getIndirectBuffer();
assertEquals(32, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
content = cache.getContent(names[3], 4096);
content.getIndirectBuffer();
assertEquals(61, cache.getCachedSize());
assertEquals(4, cache.getCachedFiles());
Thread.sleep(200);
cache.flushCache();
assertEquals(0, cache.getCachedSize());
assertEquals(0, cache.getCachedFiles());
cache.flushCache();
}
use of org.eclipse.jetty.http.ResourceHttpContent in project jetty.project by eclipse.
the class CachedContentFactory method getContent.
/* ------------------------------------------------------------ */
/** Get a Entry from the cache.
* Get either a valid entry object or create a new one if possible.
*
* @param pathInContext The key into the cache
* @param maxBufferSize The maximum buffer to allocated for this request. For cached content, a larger buffer may have
* previously been allocated and returned by the {@link HttpContent#getDirectBuffer()} or {@link HttpContent#getIndirectBuffer()} calls.
* @return The entry matching <code>pathInContext</code>, or a new entry
* if no matching entry was found. If the content exists but is not cachable,
* then a {@link ResourceHttpContent} instance is return. If
* the resource does not exist, then null is returned.
* @throws IOException Problem loading the resource
*/
@Override
public HttpContent getContent(String pathInContext, int maxBufferSize) throws IOException {
// Is the content in this cache?
CachedHttpContent content = _cache.get(pathInContext);
if (content != null && (content).isValid())
return content;
// try loading the content from our factory.
Resource resource = _factory.getResource(pathInContext);
HttpContent loaded = load(pathInContext, resource, maxBufferSize);
if (loaded != null)
return loaded;
// Is the content in the parent cache?
if (_parent != null) {
HttpContent httpContent = _parent.getContent(pathInContext, maxBufferSize);
if (httpContent != null)
return httpContent;
}
return null;
}
use of org.eclipse.jetty.http.ResourceHttpContent in project jetty.project by eclipse.
the class CachedContentFactory method load.
/* ------------------------------------------------------------ */
private HttpContent load(String pathInContext, Resource resource, int maxBufferSize) throws IOException {
if (resource == null || !resource.exists())
return null;
if (resource.isDirectory())
return new ResourceHttpContent(resource, _mimeTypes.getMimeByExtension(resource.toString()), getMaxCachedFileSize());
// Will it fit in the cache?
if (isCacheable(resource)) {
CachedHttpContent content = null;
// Look for precompressed resources
if (_precompressedFormats.length > 0) {
Map<CompressedContentFormat, CachedHttpContent> precompresssedContents = new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format : _precompressedFormats) {
String compressedPathInContext = pathInContext + format._extension;
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent == null || compressedContent.isValid()) {
compressedContent = null;
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified() && compressedResource.length() < resource.length()) {
compressedContent = new CachedHttpContent(compressedPathInContext, compressedResource, null);
CachedHttpContent added = _cache.putIfAbsent(compressedPathInContext, compressedContent);
if (added != null) {
compressedContent.invalidate();
compressedContent = added;
}
}
}
if (compressedContent != null)
precompresssedContents.put(format, compressedContent);
}
content = new CachedHttpContent(pathInContext, resource, precompresssedContents);
} else
content = new CachedHttpContent(pathInContext, resource, null);
// Add it to the cache.
CachedHttpContent added = _cache.putIfAbsent(pathInContext, content);
if (added != null) {
content.invalidate();
content = added;
}
return content;
}
// Look for non Cacheable precompressed resource or content
String mt = _mimeTypes.getMimeByExtension(pathInContext);
if (_precompressedFormats.length > 0) {
// Is the precompressed content cached?
Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>();
for (CompressedContentFormat format : _precompressedFormats) {
String compressedPathInContext = pathInContext + format._extension;
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent != null && compressedContent.isValid() && compressedContent.getResource().lastModified() >= resource.lastModified())
compressedContents.put(format, compressedContent);
// Is there a precompressed resource?
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified() && compressedResource.length() < resource.length())
compressedContents.put(format, new ResourceHttpContent(compressedResource, _mimeTypes.getMimeByExtension(compressedPathInContext), maxBufferSize));
}
if (!compressedContents.isEmpty())
return new ResourceHttpContent(resource, mt, maxBufferSize, compressedContents);
}
return new ResourceHttpContent(resource, mt, maxBufferSize);
}
use of org.eclipse.jetty.http.ResourceHttpContent in project jetty.project by eclipse.
the class ResourceContentFactory method load.
/* ------------------------------------------------------------ */
private HttpContent load(String pathInContext, Resource resource, int maxBufferSize) throws IOException {
if (resource == null || !resource.exists())
return null;
if (resource.isDirectory())
return new ResourceHttpContent(resource, _mimeTypes.getMimeByExtension(resource.toString()), maxBufferSize);
// Look for a precompressed resource or content
String mt = _mimeTypes.getMimeByExtension(pathInContext);
if (_precompressedFormats.length > 0) {
// Is there a compressed resource?
Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format : _precompressedFormats) {
String compressedPathInContext = pathInContext + format._extension;
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified() && compressedResource.length() < resource.length())
compressedContents.put(format, new ResourceHttpContent(compressedResource, _mimeTypes.getMimeByExtension(compressedPathInContext), maxBufferSize));
}
if (!compressedContents.isEmpty())
return new ResourceHttpContent(resource, mt, maxBufferSize, compressedContents);
}
return new ResourceHttpContent(resource, mt, maxBufferSize);
}
Aggregations