use of com.enonic.xp.resource.Resource in project xp by enonic.
the class IOHandlerBeanTest method getResource.
@Test
public void getResource() throws Exception {
final Resource res1 = this.bean.getResource("/unknown.txt");
assertEquals(false, res1.exists());
final Resource res2 = this.bean.getResource("./sample.txt");
assertEquals(true, res2.exists());
final Resource res3 = this.bean.getResource(ResourceKey.from("myapp:/test/sample.txt"));
assertEquals(true, res3.exists());
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class AssetUrlBuilder method buildUrl.
@Override
protected void buildUrl(final StringBuilder url, final Multimap<String, String> params) {
super.buildUrl(url, params);
final ApplicationKey applicationKey = new ApplicationResolver().portalRequest(this.portalRequest).application(this.params.getApplication()).resolve();
final Resource resource = this.resourceService.getResource(ResourceKey.from(applicationKey, "META-INF/MANIFEST.MF"));
if (!resource.exists()) {
throw new IllegalArgumentException("Could not find application [" + applicationKey + "]");
}
final String fingerprint = RunMode.get() == RunMode.DEV ? String.valueOf(stableTime()) : HexCoder.toHex(resource.getTimestamp());
appendPart(url, applicationKey + ":" + fingerprint);
appendPart(url, this.params.getPath());
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class ScriptExportsCacheTest method expireCacheIfNeeded_one_expired_clears_all.
@Test
void expireCacheIfNeeded_one_expired_clears_all() throws Exception {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "some0.js");
final Resource resource = mock(Resource.class);
when(resource.getTimestamp()).thenReturn(1L);
when(resourceLookup.apply(resourceKey)).thenReturn(resource);
final ResourceKey resourceKeyExtra = ResourceKey.from(ApplicationKey.from("some.app"), "some1.js");
final Resource resourceExtra = mock(Resource.class);
when(resourceExtra.getTimestamp()).thenReturn(2L, 3L);
when(resourceLookup.apply(resourceKeyExtra)).thenReturn(resourceExtra);
final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.DEV, resourceLookup, expiredCallback);
scriptExportsCache.getOrCompute(resourceKey, requireFunction);
scriptExportsCache.getOrCompute(resourceKeyExtra, requireFunction);
scriptExportsCache.expireCacheIfNeeded();
scriptExportsCache.getOrCompute(resourceKey, requireFunction);
verify(resourceExtra, times(2)).getTimestamp();
verify(resourceLookup, times(2)).apply(resourceKey);
verify(requireFunction, times(2)).apply(resource);
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class ScriptExportsCacheTest method expireCacheIfNeeded_has_no_effect_in_prod.
@Test
void expireCacheIfNeeded_has_no_effect_in_prod() throws Exception {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "main.js");
final Resource resource = mock(Resource.class);
when(resource.getTimestamp()).thenReturn(1L, 2L);
final Object value = new Object();
when(requireFunction.apply(resource)).thenReturn(value);
when(resourceLookup.apply(resourceKey)).thenReturn(resource);
final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.PROD, resourceLookup, expiredCallback);
final Object getOrCompute1 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
scriptExportsCache.expireCacheIfNeeded();
final Object getOrCompute2 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
assertAll(() -> assertSame(value, getOrCompute1), () -> assertSame(value, getOrCompute2));
verify(resourceLookup, times(1)).apply(resourceKey);
verifyNoMoreInteractions(resourceLookup);
verify(requireFunction, times(1)).apply(resource);
verifyNoMoreInteractions(requireFunction);
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class WebAppHandler method serveAsset.
private WebResponse serveAsset(final ApplicationKey applicationKey, final String path) {
final ResourceKey key = ResourceKey.from(applicationKey, ROOT_ASSET_PREFIX + path);
final Resource resource = this.resourceService.getResource(key);
if (!resource.exists()) {
return null;
}
final String type = MediaTypes.instance().fromFile(key.getName()).toString();
return PortalResponse.create().body(resource).contentType(MediaType.parse(type)).build();
}
Aggregations