Search in sources :

Example 11 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.

the class HttpCacheKeyTest method testEmptyPath.

@Test
public void testEmptyPath() throws Exception {
    HttpCacheKey key = new HttpCacheKey("");
    Assert.assertEquals("", key.getResourcePath());
    Assert.assertEquals(0, key.hashCode());
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) Test(org.junit.Test)

Example 12 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.

the class HttpResourceCacheTest method testGet.

@Test
public void testGet() throws Exception {
    HttpCacheKey key = new HttpCacheKey("/");
    HttpCacheObject obj = rc.get(key);
    Assert.assertNull(obj);
}
Also used : HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) Test(org.junit.Test)

Example 13 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.

the class HttpResourceCacheTest method testPutCachable.

@Test
public void testPutCachable() throws Exception {
    BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).build();
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
    boolean b = rc.put(obj);
    Assert.assertTrue(b);
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) Test(org.junit.Test)

Example 14 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.

the class HttpResourceCacheTest method testPutGet.

@Test
public void testPutGet() throws Exception {
    BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).build();
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
    boolean b = rc.put(obj);
    Assert.assertTrue(b);
    HttpCacheObject obj2 = rc.get(new HttpCacheKey("/"));
    Assert.assertEquals(obj.getCacheKey(), obj2.getCacheKey());
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) Test(org.junit.Test)

Example 15 with HttpCacheKey

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.

the class ResourceServlet method writeStaticResource.

private boolean writeStaticResource(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    String pathInfo = req.getPathInfo();
    if (StringUtility.isNullOrEmpty(pathInfo) || pathInfo.endsWith("/")) {
        pathInfo = "/index.html";
    }
    URL url = null;
    String contentType = null;
    if (m_warPath != null) {
        ServletContext servletContext = getServletContext();
        String resourcePath = m_warPath + pathInfo;
        url = servletContext.getResource(resourcePath);
        contentType = FileUtility.getMimeType(resourcePath);
    }
    // 
    if (url == null) {
        return false;
    }
    long lastModified;
    int contentLength;
    URLConnection connection = url.openConnection();
    lastModified = connection.getLastModified();
    contentLength = connection.getContentLength();
    BinaryResource res = BinaryResources.create().withFilename(pathInfo).withContentType(contentType).withLastModified(lastModified).build();
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(pathInfo), res);
    if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
        return true;
    }
    try (InputStream is = connection.getInputStream()) {
        @SuppressWarnings("resource") OutputStream os = resp.getOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead = is.read(buffer);
        int writtenContentLength = 0;
        while (bytesRead != -1) {
            os.write(buffer, 0, bytesRead);
            writtenContentLength += bytesRead;
            bytesRead = is.read(buffer);
        }
        if (contentLength == -1 || contentLength != writtenContentLength) {
            resp.setContentLength(writtenContentLength);
        }
    }
    return true;
}
Also used : HttpCacheControl(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) ServletContext(javax.servlet.ServletContext) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Aggregations

HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)31 Test (org.junit.Test)24 HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)20 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)15 HttpCacheControl (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl)2 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 ServletContext (javax.servlet.ServletContext)1 NlsLocale (org.eclipse.scout.rt.platform.nls.NlsLocale)1 ImmutablePair (org.eclipse.scout.rt.platform.util.ImmutablePair)1 IHttpResourceCache (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)1 IRemoteFileService (org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)1 RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)1 HtmlFileLoader (org.eclipse.scout.rt.ui.html.res.loader.HtmlFileLoader)1