Search in sources :

Example 1 with MimeTypes

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

the class ResourceCacheTest method testUncacheable.

@Test
public void testUncacheable() throws Exception {
    ResourceCollection rc = new ResourceCollection(new String[] { "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/one/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/two/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/three/" });
    Resource[] r = rc.getResources();
    MimeTypes mime = new MimeTypes();
    CachedContentFactory rc3 = new CachedContentFactory(null, r[2], mime, false, false, CompressedContentFormat.NONE);
    CachedContentFactory rc2 = new CachedContentFactory(rc3, r[1], mime, false, false, CompressedContentFormat.NONE) {

        @Override
        public boolean isCacheable(Resource resource) {
            return super.isCacheable(resource) && resource.getName().indexOf("2.txt") < 0;
        }
    };
    CachedContentFactory rc1 = new CachedContentFactory(rc2, r[0], mime, false, false, CompressedContentFormat.NONE);
    assertEquals("1 - one", getContent(rc1, "1.txt"));
    assertEquals("2 - two", getContent(rc1, "2.txt"));
    assertEquals("3 - three", getContent(rc1, "3.txt"));
    assertEquals("1 - two", getContent(rc2, "1.txt"));
    assertEquals("2 - two", getContent(rc2, "2.txt"));
    assertEquals("3 - three", getContent(rc2, "3.txt"));
    assertEquals(null, getContent(rc3, "1.txt"));
    assertEquals("2 - three", getContent(rc3, "2.txt"));
    assertEquals("3 - three", getContent(rc3, "3.txt"));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) MimeTypes(org.eclipse.jetty.http.MimeTypes) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 2 with MimeTypes

use of org.eclipse.jetty.http.MimeTypes 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)

Example 3 with MimeTypes

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

the class TestMinGzipSizeServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    mimeTypes = new MimeTypes();
}
Also used : MimeTypes(org.eclipse.jetty.http.MimeTypes)

Example 4 with MimeTypes

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

the class ContextHandler method doStart.

/* ------------------------------------------------------------ */
/*
     * @see org.eclipse.thread.AbstractLifeCycle#doStart()
     */
@Override
protected void doStart() throws Exception {
    _availability = Availability.STARTING;
    if (_contextPath == null)
        throw new IllegalStateException("Null contextPath");
    if (_logger == null) {
        String log_name = getDisplayName();
        if (log_name == null || log_name.isEmpty()) {
            log_name = getContextPath();
            if (log_name != null || log_name.startsWith("/"))
                log_name = log_name.substring(1);
            if (log_name == null || log_name.isEmpty())
                log_name = Integer.toHexString(hashCode());
        }
        _logger = Log.getLogger("org.eclipse.jetty.ContextHandler." + log_name);
    }
    ClassLoader old_classloader = null;
    Thread current_thread = null;
    Context old_context = null;
    _attributes.setAttribute("org.eclipse.jetty.server.Executor", getServer().getThreadPool());
    if (_mimeTypes == null)
        _mimeTypes = new MimeTypes();
    try {
        // Set the classloader, context and enter scope
        if (_classLoader != null) {
            current_thread = Thread.currentThread();
            old_classloader = current_thread.getContextClassLoader();
            current_thread.setContextClassLoader(_classLoader);
        }
        old_context = __context.get();
        __context.set(_scontext);
        enterScope(null, getState());
        // defers the calling of super.doStart()
        startContext();
        _availability = Availability.AVAILABLE;
        LOG.info("Started {}", this);
    } finally {
        if (_availability == Availability.STARTING)
            _availability = Availability.UNAVAILABLE;
        exitScope(null);
        __context.set(old_context);
        // reset the classloader
        if (_classLoader != null && current_thread != null)
            current_thread.setContextClassLoader(old_classloader);
    }
}
Also used : ServletContext(javax.servlet.ServletContext) URLClassLoader(java.net.URLClassLoader) MimeTypes(org.eclipse.jetty.http.MimeTypes)

Example 5 with MimeTypes

use of org.eclipse.jetty.http.MimeTypes in project dropwizard by dropwizard.

the class ServletEnvironmentTest method addsMimeMapping.

@Test
public void addsMimeMapping() {
    final MimeTypes mimeTypes = mock(MimeTypes.class);
    when(handler.getMimeTypes()).thenReturn(mimeTypes);
    environment.addMimeMapping("example/foo", "foo");
    verify(mimeTypes).addMimeMapping("example/foo", "foo");
}
Also used : MimeTypes(org.eclipse.jetty.http.MimeTypes) Test(org.junit.Test)

Aggregations

MimeTypes (org.eclipse.jetty.http.MimeTypes)13 Test (org.junit.Test)5 Resource (org.eclipse.jetty.util.resource.Resource)4 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)3 Constraint (org.eclipse.jetty.util.security.Constraint)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URLClassLoader (java.net.URLClassLoader)1 EventListener (java.util.EventListener)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 SessionCookieConfig (javax.servlet.SessionCookieConfig)1 SessionTrackingMode (javax.servlet.SessionTrackingMode)1 JspPropertyGroupDescriptor (javax.servlet.descriptor.JspPropertyGroupDescriptor)1 TaglibDescriptor (javax.servlet.descriptor.TaglibDescriptor)1 HttpServlet (javax.servlet.http.HttpServlet)1