Search in sources :

Example 1 with Resource

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

Example 2 with Resource

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

Example 3 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)

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

Example 5 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)

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