Search in sources :

Example 6 with Resource

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;
}
Also used : Resource(com.enonic.xp.resource.Resource) Reader(java.io.Reader) IOException(java.io.IOException) Properties(java.util.Properties) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 7 with Resource

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();
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 8 with Resource

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();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 9 with Resource

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();
}
Also used : ArrayList(java.util.ArrayList) Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 10 with Resource

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);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.enonic.xp.resource.Resource)28 ResourceKey (com.enonic.xp.resource.ResourceKey)21 Test (org.junit.jupiter.api.Test)14 PortalResponse (com.enonic.xp.portal.PortalResponse)7 ContentService (com.enonic.xp.content.ContentService)6 PortalRequest (com.enonic.xp.portal.PortalRequest)6 ResourceService (com.enonic.xp.resource.ResourceService)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ApplicationKey (com.enonic.xp.app.ApplicationKey)5 Branch (com.enonic.xp.branch.Branch)4 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4 MockResource (com.enonic.xp.resource.MockResource)4 RunMode (com.enonic.xp.server.RunMode)4 Site (com.enonic.xp.site.Site)4