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());
}
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;
}
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;
}
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;
}
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);
}
Aggregations