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 PartDescriptorLoader method loadIcon.
private Icon loadIcon(final DescriptorKey key, final String mimeType, final String ext) {
final ResourceKey resourceKey = PartDescriptor.toResourceKey(key, ext);
final Resource resource = this.resourceService.getResource(resourceKey);
if (!resource.exists()) {
return null;
}
final Instant modifiedTime = Instant.ofEpochMilli(resource.getTimestamp());
return Icon.from(resource.readBytes(), mimeType, modifiedTime);
}
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 IconLoader method loadIcon.
private static Icon loadIcon(final MacroKey macroKey, final String mimeType, final String ext, final ResourceService resourceService, final String path) {
final ResourceKey resourceKey = toResourceKey(macroKey, ext, path);
final Resource resource = resourceService.getResource(resourceKey);
return doLoadIcon(resource, mimeType);
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class ExceptionRendererImplTest method render_default_error_page_when_error_in_custom_handler.
@Test
void render_default_error_page_when_error_in_custom_handler() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> {
throw new RuntimeException("Something went wrong in the handler script");
};
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
final String body = res.getBody().toString();
assertTrue(body.contains("400 Bad Request"));
assertTrue(body.contains("Custom message"));
}
Aggregations