use of com.enonic.xp.resource.Resource in project xp by enonic.
the class LocaleServiceImpl method loadBundle.
private Properties loadBundle(final ApplicationKey applicationKey, final String bundleName) {
final ResourceKey resourceKey = ResourceKey.from(applicationKey, bundleName + ".properties");
final Resource resource = resourceService.getResource(resourceKey);
final Properties properties = new Properties();
if (resource.exists()) {
try (Reader in = resource.openReader()) {
properties.load(in);
} catch (final IOException e) {
throw new LocalizationException("Not able to load resource for: " + applicationKey.toString(), e);
}
}
return properties;
}
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();
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class AssetHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
if (request.getMethod() == HttpMethod.OPTIONS) {
// it will be handled by default OPTIONS handler in BaseWebHandler
return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
}
final ResourceKey assetsKey = ResourceKey.assets(applicationKey);
final String assetPath = assetsKey.getPath() + path;
final ResourceKey resourceKey = ResourceKey.from(applicationKey, assetPath);
final Resource resource = resolveResource(resourceKey);
final String type = MediaTypes.instance().fromFile(resource.getKey().getName()).toString();
final PortalResponse.Builder portalResponse = PortalResponse.create().body(resource).contentType(MediaType.parse(type));
if (!nullToEmpty(this.fingerprint).isBlank() && !nullToEmpty(cacheControlHeaderConfig).isBlank() && RunMode.get() != RunMode.DEV && resourceKey.getPath().equals(assetPath) && fingerprintMatches(fingerprint)) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
return portalResponse.build();
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class ErrorPageRichBuilder method findAllSourceLines.
private List<String> findAllSourceLines(final ResourceProblemException cause) {
final ResourceKey resourceKey = cause.getResource();
if (resourceKey == null || this.resourceService == null) {
return new ArrayList<>();
}
final Resource resource = resourceService.getResource(resourceKey);
return resource.readLines();
}
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);
}
Aggregations