Search in sources :

Example 36 with DescriptorKey

use of com.enonic.xp.page.DescriptorKey in project xp by enonic.

the class ServiceDescriptorServiceImplTest method testGetByKey.

@Test
public void testGetByKey() throws Exception {
    final DescriptorKey key = DescriptorKey.from("myapp1:myservice1");
    final ServiceDescriptor descriptor = this.service.getByKey(key);
    assertNotNull(descriptor);
    assertEquals("[role:system.admin]", descriptor.getAllowedPrincipals().toString());
}
Also used : ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) Test(org.junit.jupiter.api.Test) AbstractDescriptorServiceTest(com.enonic.xp.core.impl.content.page.AbstractDescriptorServiceTest)

Example 37 with DescriptorKey

use of com.enonic.xp.page.DescriptorKey in project xp by enonic.

the class PageResolver method resolve.

public PageResolverResult resolve(final RenderMode mode, final Content content, final Site site) {
    final Page page = content.getPage();
    final PageTemplate pageTemplate;
    final DescriptorKey controller;
    final Page effectivePage;
    if (content instanceof PageTemplate) {
        pageTemplate = (PageTemplate) content;
        controller = getControllerFromTemplate(pageTemplate, mode);
        effectivePage = pageTemplate.getPage();
    } else if (page != null) {
        if (page.getFragment() != null) {
            controller = null;
            effectivePage = page;
        } else if (page.getDescriptor() != null) {
            controller = page.getDescriptor();
            effectivePage = page;
        } else if (page.getTemplate() != null) {
            pageTemplate = getPageTemplateOrFindDefault(page.getTemplate(), content.getType(), site.getPath());
            if (pageTemplate != null) {
                controller = getControllerFromTemplate(pageTemplate, mode);
                effectivePage = mergePageFromPageTemplate(pageTemplate, page);
            } else {
                controller = errorOrNull(null, mode, String.format("Template [%s] is missing and no default template found for content", page.getTemplate()));
                effectivePage = page;
            }
        } else {
            controller = errorOrNull(null, mode, "Content page has neither template nor descriptor");
            effectivePage = page;
        }
    } else {
        pageTemplate = this.pageTemplateService.getDefault(GetDefaultPageTemplateParams.create().sitePath(site.getPath()).contentType(content.getType()).build());
        if (pageTemplate != null) {
            controller = getControllerFromTemplate(pageTemplate, mode);
            effectivePage = mergePageFromPageTemplate(pageTemplate, null);
        } else {
            controller = errorOrNull(null, mode, "No default template found for content");
            effectivePage = null;
        }
    }
    return new PageResolverResult(effectivePage, controller);
}
Also used : PageTemplate(com.enonic.xp.page.PageTemplate) DescriptorKey(com.enonic.xp.page.DescriptorKey) Page(com.enonic.xp.page.Page)

Example 38 with DescriptorKey

use of com.enonic.xp.page.DescriptorKey in project xp by enonic.

the class ServiceHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    // Retrieves the ServiceDescriptor
    final DescriptorKey descriptorKey = DescriptorKey.from(applicationKey, name);
    final ServiceDescriptor serviceDescriptor = serviceDescriptorService.getByKey(descriptorKey);
    if (serviceDescriptor == null) {
        throw WebException.notFound(String.format("Service [%s] not found", descriptorKey));
    }
    // Checks if the access to ServiceDescriptor is allowed
    final PrincipalKeys principals = ContextAccessor.current().getAuthInfo().getPrincipals();
    if (!serviceDescriptor.isAccessAllowed(principals)) {
        throw WebException.forbidden(String.format("You don't have permission to access [%s]", descriptorKey));
    }
    final ContentResolverResult resolvedContent = contentResolver.resolve(request);
    final Site site = resolvedContent.getNearestSite();
    // Checks if the application is set on the current site
    if (site != null) {
        final PropertyTree siteConfig = site.getSiteConfig(applicationKey);
        if (siteConfig == null) {
            throw WebException.forbidden(String.format("Service [%s] forbidden for this site", descriptorKey));
        }
    }
    // Checks if the application is set on the current application
    final ApplicationKey baseApplicationKey = getBaseApplicationKey();
    if (baseApplicationKey != null && !baseApplicationKey.equals(applicationKey)) {
        throw WebException.forbidden(String.format("Service [%s] forbidden for this application", descriptorKey));
    }
    // Prepares the request
    this.request.setApplicationKey(applicationKey);
    this.request.setContent(resolvedContent.getContent());
    this.request.setSite(site);
    // Executes the service
    final ControllerScript controllerScript = getScript();
    final PortalResponse portalResponse = controllerScript.execute(this.request);
    final WebSocketConfig webSocketConfig = portalResponse.getWebSocket();
    final WebSocketContext webSocketContext = this.request.getWebSocketContext();
    if ((webSocketContext != null) && (webSocketConfig != null)) {
        final WebSocketEndpoint webSocketEndpoint = newWebSocketEndpoint(webSocketConfig);
        webSocketContext.apply(webSocketEndpoint);
    }
    return portalResponse;
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) PortalResponse(com.enonic.xp.portal.PortalResponse) WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) WebSocketEndpoint(com.enonic.xp.web.websocket.WebSocketEndpoint) DescriptorKey(com.enonic.xp.page.DescriptorKey) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Example 39 with DescriptorKey

use of com.enonic.xp.page.DescriptorKey in project xp by enonic.

the class RenderBaseHandlerTest method createDescriptor.

private PageDescriptor createDescriptor() {
    final ApplicationKey applicationKey = ApplicationKey.from("mainapplication");
    final String name = "mypage";
    final DescriptorKey key = DescriptorKey.from(applicationKey, name);
    final String xml = "<?xml version=\"1.0\"?>\n" + "<page>\n" + "  <display-name>Landing page</display-name>\n" + "  <config/>\n" + "</page>";
    final PageDescriptor.Builder builder = PageDescriptor.create();
    parseXml(applicationKey, builder, xml);
    return builder.key(key).displayName("Landing page").build();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) PageDescriptor(com.enonic.xp.page.PageDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 40 with DescriptorKey

use of com.enonic.xp.page.DescriptorKey in project xp by enonic.

the class ComponentHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final ContentResolverResult resolvedContent = contentResolver.resolve(this.request);
    final Content content = resolvedContent.getContentOrElseThrow();
    final Site site = resolvedContent.getNearestSiteOrElseThrow();
    Page page = content.getPage();
    final PageResolverResult resolvedPage = pageResolver.resolve(request.getMode(), content, site);
    Component component = null;
    if (content.getType().isFragment()) {
        // fragment content, try resolving component path in Layout fragment
        final Component fragmentComponent = page.getFragment();
        if (this.componentPath.isEmpty()) {
            component = fragmentComponent;
        } else if (fragmentComponent instanceof LayoutComponent) {
            component = ((LayoutComponent) fragmentComponent).getComponent(this.componentPath);
        }
    }
    final Page effectivePage;
    if (component == null) {
        effectivePage = inlineFragments(resolvedPage.getEffectivePage(), this.componentPath);
        component = effectivePage.getRegions().getComponent(this.componentPath);
    } else {
        effectivePage = resolvedPage.getEffectivePage();
    }
    if (component == null) {
        throw WebException.notFound(String.format("Page component for [%s] not found", this.componentPath));
    }
    final Content effectiveContent = Content.create(content).page(effectivePage).build();
    final DescriptorKey controller = resolvedPage.getController();
    this.request.setSite(site);
    this.request.setContent(effectiveContent);
    this.request.setComponent(component);
    this.request.setApplicationKey(controller != null ? controller.getApplicationKey() : null);
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("componentPath", component.getPath());
        trace.put("type", component.getType().toString());
    }
    final PortalResponse response = rendererDelegate.render(component, this.request);
    return this.postProcessor.processResponseInstructions(this.request, response);
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) DescriptorKey(com.enonic.xp.page.DescriptorKey) Page(com.enonic.xp.page.Page) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component) LayoutComponent(com.enonic.xp.region.LayoutComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent)

Aggregations

DescriptorKey (com.enonic.xp.page.DescriptorKey)73 Test (org.junit.jupiter.api.Test)45 PropertyTree (com.enonic.xp.data.PropertyTree)25 PrincipalKey (com.enonic.xp.security.PrincipalKey)11 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)10 TaskId (com.enonic.xp.task.TaskId)10 Page (com.enonic.xp.page.Page)7 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)7 TaskDescriptor (com.enonic.xp.task.TaskDescriptor)7 Instant (java.time.Instant)7 PropertySet (com.enonic.xp.data.PropertySet)6 ServiceDescriptor (com.enonic.xp.service.ServiceDescriptor)6 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)5 CronCalendar (com.enonic.xp.scheduler.CronCalendar)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)4 AbstractDescriptorServiceTest (com.enonic.xp.core.impl.content.page.AbstractDescriptorServiceTest)4 DescribedTask (com.enonic.xp.impl.task.distributed.DescribedTask)4 PortalRequest (com.enonic.xp.portal.PortalRequest)4 PortalResponse (com.enonic.xp.portal.PortalResponse)4