Search in sources :

Example 1 with HttpContent

use of org.eclipse.jetty.http.HttpContent 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);
}
Also used : HashMap(java.util.HashMap) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) Resource(org.eclipse.jetty.util.resource.Resource) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 2 with HttpContent

use of org.eclipse.jetty.http.HttpContent in project jetty.project by eclipse.

the class ResourceContentFactory method getContent.

/* ------------------------------------------------------------ */
@Override
public HttpContent getContent(String pathInContext, int maxBufferSize) throws IOException {
    // try loading the content from our factory.
    Resource resource = _factory.getResource(pathInContext);
    HttpContent loaded = load(pathInContext, resource, maxBufferSize);
    return loaded;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 3 with HttpContent

use of org.eclipse.jetty.http.HttpContent 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;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) PrecompressedHttpContent(org.eclipse.jetty.http.PrecompressedHttpContent) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 4 with HttpContent

use of org.eclipse.jetty.http.HttpContent 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);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) Resource(org.eclipse.jetty.util.resource.Resource) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat) PrecompressedHttpContent(org.eclipse.jetty.http.PrecompressedHttpContent) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 5 with HttpContent

use of org.eclipse.jetty.http.HttpContent 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();
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.eclipse.jetty.util.resource.Resource) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) MimeTypes(org.eclipse.jetty.http.MimeTypes) File(java.io.File) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) Test(org.junit.Test)

Aggregations

HttpContent (org.eclipse.jetty.http.HttpContent)7 ResourceHttpContent (org.eclipse.jetty.http.ResourceHttpContent)5 Resource (org.eclipse.jetty.util.resource.Resource)5 CompressedContentFormat (org.eclipse.jetty.http.CompressedContentFormat)3 File (java.io.File)2 HashMap (java.util.HashMap)2 PrecompressedHttpContent (org.eclipse.jetty.http.PrecompressedHttpContent)2 Test (org.junit.Test)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MimeTypes (org.eclipse.jetty.http.MimeTypes)1 ResourceContentFactory (org.eclipse.jetty.server.ResourceContentFactory)1