Search in sources :

Example 1 with BinaryResourceHolder

use of org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder in project scout.rt by eclipse.

the class DownloadHandlerStorageTest method testGet.

@Test
public void testGet() {
    final CountDownLatch secondGet = new CountDownLatch(1);
    final CountDownLatch elementRemoved = new CountDownLatch(1);
    DownloadHandlerStorage storage = new DownloadHandlerStorage() {

        @Override
        protected long getTTLForResource(BinaryResource res) {
            return 50L;
        }

        @Override
        protected void removeOnTimeout(String key) {
            waitFor(secondGet);
            super.removeOnTimeout(key);
            elementRemoved.countDown();
        }
    };
    BinaryResource res = new BinaryResource("bar.txt", null);
    BinaryResourceHolder holder = new BinaryResourceHolder(res);
    storage.put(KEY, holder, OpenUriAction.NEW_WINDOW);
    assertEquals(1, storage.futureMap().size());
    BinaryResourceHolderWithAction holderWithAction = storage.get(KEY);
    assertEquals(OpenUriAction.NEW_WINDOW, holderWithAction.getOpenUriAction());
    assertEquals(res, holderWithAction.getHolder().get());
    assertFalse("Must not contain download interceptor.", containsDownloadInterceptor(holderWithAction.getHolder()));
    holderWithAction = storage.get(KEY);
    assertEquals(res, holderWithAction.getHolder().get());
    assertTrue("Must contain download interceptor.", containsDownloadInterceptor(holderWithAction.getHolder()));
    secondGet.countDown();
    waitFor(elementRemoved);
    assertNull(storage.get(KEY));
    assertEquals("futureMap must be cleared after timeout", 0, storage.futureMap().size());
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) CountDownLatch(java.util.concurrent.CountDownLatch) BinaryResourceHolderWithAction(org.eclipse.scout.rt.ui.html.json.desktop.DownloadHandlerStorage.BinaryResourceHolderWithAction) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder) Test(org.junit.Test)

Example 2 with BinaryResourceHolder

use of org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder in project scout.rt by eclipse.

the class DownloadHandlerStorage method get.

/**
 * Retrieve a downloadable item from the storage.
 * <p>
 * Items with {@link OpenUriAction#DOWNLOAD} are automatically removed after a certain TTL
 * ({@link #getRemovalTimeoutAfterFirstRequest()}).
 */
public BinaryResourceHolderWithAction get(String key) {
    BinaryResourceHolderWithAction resHolderWithAction;
    synchronized (m_valueMap) {
        resHolderWithAction = m_valueMap.get(key);
    }
    if (resHolderWithAction != null) {
        if (resHolderWithAction.getOpenUriAction() == OpenUriAction.DOWNLOAD) {
            scheduleRemoval(key, getRemovalTimeoutAfterFirstRequest());
        } else {
            if (!containsDownloadInterceptor(resHolderWithAction.getHolder())) {
                BinaryResourceHolder holder = new BinaryResourceHolder(resHolderWithAction.getHolder().get());
                holder.addHttpResponseInterceptor(new DownloadHttpResponseInterceptor(holder.get().getFilename()));
                put(key, holder, resHolderWithAction.getOpenUriAction());
            }
        }
    }
    return resHolderWithAction;
}
Also used : DownloadHttpResponseInterceptor(org.eclipse.scout.rt.server.commons.servlet.cache.DownloadHttpResponseInterceptor) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)

Example 3 with BinaryResourceHolder

use of org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder 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)

Example 4 with BinaryResourceHolder

use of org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder in project scout.rt by eclipse.

the class JsonBrowserField method provideBinaryResource.

@Override
public BinaryResourceHolder provideBinaryResource(String filenameWithFingerprint) {
    Pair<String, Long> filenameAndFingerprint = BinaryResourceUrlUtility.extractFilenameWithFingerprint(filenameWithFingerprint);
    String filename = filenameAndFingerprint.getLeft();
    BinaryResource binaryResource = getModel().getUIFacade().requestBinaryResourceFromUI(filename);
    BinaryResourceHolder holder = new BinaryResourceHolder(binaryResource);
    holder.addHttpResponseInterceptor(new BrowserFieldContentHttpResponseInterceptor(getUiSession()));
    return holder;
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)

Example 5 with BinaryResourceHolder

use of org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder in project scout.rt by eclipse.

the class JsonDesktop method handleModelOpenUri.

protected void handleModelOpenUri(BinaryResource res, IOpenUriAction openUriAction) {
    String filename = ObjectUtility.nvl(res.getFilename(), "binaryData");
    String filenameEncoded = IOUtility.urlEncode(filename);
    // add another path segment to filename to distinguish between different resources
    // with the same filename (also makes hash collisions irrelevant).
    long counter = RESOURCE_COUNTER.getAndIncrement();
    filenameEncoded = counter + "/" + filenameEncoded;
    BinaryResourceHolder holder = new BinaryResourceHolder(res);
    if (openUriAction == OpenUriAction.DOWNLOAD) {
        holder.addHttpResponseInterceptor(new DownloadHttpResponseInterceptor(res.getFilename()));
    }
    m_downloads.put(filenameEncoded, holder, openUriAction);
    String downloadUrl = BinaryResourceUrlUtility.createDynamicAdapterResourceUrl(this, filenameEncoded);
    handleModelOpenUri(downloadUrl, openUriAction);
}
Also used : DownloadHttpResponseInterceptor(org.eclipse.scout.rt.server.commons.servlet.cache.DownloadHttpResponseInterceptor) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)

Aggregations

BinaryResourceHolder (org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)7 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)5 Test (org.junit.Test)3 DownloadHttpResponseInterceptor (org.eclipse.scout.rt.server.commons.servlet.cache.DownloadHttpResponseInterceptor)2 BinaryResourceHolderWithAction (org.eclipse.scout.rt.ui.html.json.desktop.DownloadHandlerStorage.BinaryResourceHolderWithAction)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)1 IHttpResponseInterceptor (org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResponseInterceptor)1 IBinaryResourceProvider (org.eclipse.scout.rt.ui.html.res.IBinaryResourceProvider)1