Search in sources :

Example 11 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject 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 12 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject 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 13 with HttpCacheObject

use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject 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)

Example 14 with HttpCacheObject

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

the class RemoteFileServlet method writeResource.

private boolean writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String resourcePath) throws IOException {
    IRemoteFileService rfs = BEANS.get(getConfiguredRemoteFileServiceClass());
    RemoteFile spec = new RemoteFile((resourcePath == null) ? null : StringUtility.join("", m_folder, resourcePath), -1);
    RemoteFile remoteFile = rfs.getRemoteFile(spec);
    if (!remoteFile.exists()) {
        return false;
    }
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(resourcePath), remoteFile.toBinaryResource());
    if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
        return true;
    }
    rfs.streamRemoteFile(remoteFile, resp.getOutputStream());
    return true;
}
Also used : HttpCacheControl(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IRemoteFileService(org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)

Example 15 with HttpCacheObject

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

the class DynamicResourceLoader method loadResource.

@Override
public HttpCacheObject loadResource(HttpCacheKey cacheKey) {
    DynamicResourceInfo info = createDynamicResourceInfo(cacheKey);
    if (info == null) {
        LOG.warn("invalid dynamic-resource request received.", new Exception("origin"));
        return null;
    }
    IBinaryResourceProvider provider = getBinaryResourceProvider(info.getUiSession(), info.getJsonAdapterId());
    if (provider == null) {
        return null;
    }
    BinaryResourceHolder localResourceHolder = provider.provideBinaryResource(info.getFileName());
    if (localResourceHolder == null || localResourceHolder.get() == null) {
        return null;
    }
    BinaryResource localResource = localResourceHolder.get();
    BinaryResource httpResource = localResource.createAlias(cacheKey.getResourcePath());
    HttpCacheObject httpCacheObject = new HttpCacheObject(cacheKey, httpResource);
    for (IHttpResponseInterceptor interceptor : localResourceHolder.getHttpResponseInterceptors()) {
        httpCacheObject.addHttpResponseInterceptor(interceptor);
    }
    return httpCacheObject;
}
Also used : IBinaryResourceProvider(org.eclipse.scout.rt.ui.html.res.IBinaryResourceProvider) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) IHttpResponseInterceptor(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResponseInterceptor) IOException(java.io.IOException) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)

Aggregations

HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)25 HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)20 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)19 Test (org.junit.Test)15 IOException (java.io.IOException)2 HttpCacheControl (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl)2 IHttpResourceCache (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResourceCache)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 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ImmutablePair (org.eclipse.scout.rt.platform.util.ImmutablePair)1 HttpClientInfo (org.eclipse.scout.rt.server.commons.servlet.HttpClientInfo)1 HttpResponseHeaderContributor (org.eclipse.scout.rt.server.commons.servlet.cache.HttpResponseHeaderContributor)1 IHttpResponseInterceptor (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResponseInterceptor)1 IRemoteFileService (org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)1