Search in sources :

Example 36 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class WidgetHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    if (this.request.getMode() != RenderMode.ADMIN) {
        throw WebException.forbidden("Render mode must be ADMIN.");
    }
    // Retrieves the WidgetDescriptor
    final WidgetDescriptor widgetDescriptor = widgetDescriptorService.getByKey(descriptorKey);
    if (widgetDescriptor == null) {
        throw WebException.notFound(String.format("Widget [%s] not found", descriptorKey));
    }
    // Checks if the access to WidgetDescriptor is allowed
    final PrincipalKeys principals = ContextAccessor.current().getAuthInfo().getPrincipals();
    if (!widgetDescriptor.isAccessAllowed(principals)) {
        throw WebException.forbidden(String.format("You don't have permission to access [%s]", descriptorKey));
    }
    // Renders the widget
    this.request.setApplicationKey(this.descriptorKey.getApplicationKey());
    final ResourceKey scriptDir = ResourceKey.from(descriptorKey.getApplicationKey(), "admin/widgets/" + descriptorKey.getName());
    final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(scriptDir);
    return controllerScript.execute(this.request);
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 37 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class WidgetDescriptorLoaderTest method testLoadMin.

@Test
public void testLoadMin() {
    final DescriptorKey descriptorKey = DescriptorKey.from("myapp1:widget2");
    final ResourceKey resourceKey = this.loader.toResource(descriptorKey);
    final Resource resource = this.resourceService.getResource(resourceKey);
    final WidgetDescriptor descriptor = this.loader.load(descriptorKey, resource);
    assertEquals("MyWidget2", descriptor.getDisplayName());
    assertEquals("MyWidget2 description", descriptor.getDescription());
    assertEquals(1, descriptor.getInterfaces().size());
    assertTrue(descriptor.getInterfaces().contains("com.enonic.xp.my-interface"));
    assertNull(descriptor.getAllowedPrincipals());
}
Also used : Resource(com.enonic.xp.resource.Resource) DescriptorKey(com.enonic.xp.page.DescriptorKey) WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 38 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class ResourceKeyConverterTest method testSameType.

@Test
public void testSameType() {
    final ResourceKey key = ResourceKey.from("myapplication:/some/path");
    assertSame(key, Converters.convert(key, ResourceKey.class));
}
Also used : ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 39 with ResourceKey

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

Example 40 with ResourceKey

use of com.enonic.xp.resource.ResourceKey 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)

Aggregations

ResourceKey (com.enonic.xp.resource.ResourceKey)76 Test (org.junit.jupiter.api.Test)49 Resource (com.enonic.xp.resource.Resource)21 ApplicationKey (com.enonic.xp.app.ApplicationKey)12 ResourceService (com.enonic.xp.resource.ResourceService)12 ScriptExports (com.enonic.xp.script.ScriptExports)11 Application (com.enonic.xp.app.Application)9 UrlResource (com.enonic.xp.resource.UrlResource)9 URL (java.net.URL)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 PortalRequest (com.enonic.xp.portal.PortalRequest)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 ApplicationService (com.enonic.xp.app.ApplicationService)7 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)7 ScriptAsyncService (com.enonic.xp.script.impl.async.ScriptAsyncService)7 ScriptRuntimeFactoryImpl (com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl)7 PortalResponse (com.enonic.xp.portal.PortalResponse)6 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 PropertyTree (com.enonic.xp.data.PropertyTree)5