Search in sources :

Example 66 with Resource

use of org.eclipse.jetty.util.resource.Resource 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 67 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class HttpOutputTest method testAsyncWriteMed.

@Test
public void testAsyncWriteMed() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/big.txt");
    _handler._writeLengthIfKnown = false;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[4000];
    _handler._async = true;
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, Matchers.not(containsString("Content-Length")));
    assertThat(response, endsWith(toUTF8String(big)));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 68 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class HttpOutputTest method testWriteLargeKnown.

@Test
public void testWriteLargeKnown() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/big.txt");
    _handler._writeLengthIfKnown = true;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[8192];
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, containsString("Content-Length"));
    assertThat(response, endsWith(toUTF8String(big)));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 69 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class HttpOutputTest method testWriteByteKnown.

@Test
public void testWriteByteKnown() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/big.txt");
    _handler._writeLengthIfKnown = true;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[1];
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, containsString("Content-Length"));
    assertThat(response, endsWith(toUTF8String(big)));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 70 with Resource

use of org.eclipse.jetty.util.resource.Resource in project jetty.project by eclipse.

the class HttpOutputTest method testAsyncWriteSimpleKnown.

@Test
public void testAsyncWriteSimpleKnown() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/simple.txt");
    _handler._async = true;
    _handler._writeLengthIfKnown = true;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[4000];
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, containsString("Content-Length: 11"));
    assertThat(response, containsString("simple text"));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6