Search in sources :

Example 71 with DescriptorKey

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

the class PageHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final ContentResolverResult resolvedContent = contentResolver.resolve(this.request);
    final Content content = resolvedContent.getContentOrElseThrow();
    if (content.getType().isShortcut()) {
        return renderShortcut(content);
    }
    final Site site = resolvedContent.getNearestSiteOrElseThrow();
    final PageResolverResult resolvedPage = pageResolver.resolve(request.getMode(), content, site);
    final Content effectiveContent = Content.create(content).page(resolvedPage.getEffectivePage()).build();
    final DescriptorKey pageDescriptorKey = resolvedPage.getController();
    this.request.setSite(site);
    this.request.setContent(effectiveContent);
    this.request.setApplicationKey(pageDescriptorKey != null ? pageDescriptorKey.getApplicationKey() : null);
    this.request.setPageDescriptor(pageDescriptorKey != null ? this.pageDescriptorService.getByKey(pageDescriptorKey) : null);
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("contentPath", effectiveContent.getPath().toString());
        trace.put("type", "page");
    }
    return rendererDelegate.render(effectiveContent, this.request);
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) Trace(com.enonic.xp.trace.Trace) Content(com.enonic.xp.content.Content) DescriptorKey(com.enonic.xp.page.DescriptorKey)

Example 72 with DescriptorKey

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

the class ServiceHandlerTest method setup.

@BeforeEach
public final void setup() throws Exception {
    this.request = new PortalRequest();
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    this.controllerScript = Mockito.mock(ControllerScript.class);
    Mockito.when(controllerScriptFactory.fromDir(Mockito.any())).thenReturn(this.controllerScript);
    final PortalResponse portalResponse = PortalResponse.create().build();
    Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
    this.resourceService = Mockito.mock(ResourceService.class);
    final Resource resourceNotFound = Mockito.mock(Resource.class);
    Mockito.when(resourceNotFound.exists()).thenReturn(false);
    final Resource resource = Mockito.mock(Resource.class);
    Mockito.when(resource.exists()).thenReturn(true);
    Mockito.when(this.resourceService.getResource(ResourceKey.from("demo:/services/test"))).thenReturn(resourceNotFound);
    this.serviceDescriptorService = Mockito.mock(ServiceDescriptorService.class);
    final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
    final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).build();
    Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
    this.contentService = Mockito.mock(ContentService.class);
    this.handler = new ServiceHandler();
    this.handler.setControllerScriptFactory(controllerScriptFactory);
    this.handler.setContentService(this.contentService);
    this.handler.setResourceService(this.resourceService);
    this.handler.setServiceDescriptorService(this.serviceDescriptorService);
    this.request.setMethod(HttpMethod.GET);
    this.request.setContentPath(ContentPath.from("/site/somepath/content"));
    this.request.setEndpointPath("/_/service/demo/myservice");
    this.request.setRawPath("/site/draft/site/somepath/content/_/service/demo/myservice");
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) ResourceService(com.enonic.xp.resource.ResourceService) Resource(com.enonic.xp.resource.Resource) DescriptorKey(com.enonic.xp.page.DescriptorKey) ContentService(com.enonic.xp.content.ContentService) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) ServiceDescriptorService(com.enonic.xp.service.ServiceDescriptorService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 73 with DescriptorKey

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

the class ServiceHandlerTest method testForbiddenService.

@Test
public void testForbiddenService() throws Exception {
    final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
    final Set<PrincipalKey> allowedPrincipals = Collections.singleton(PrincipalKey.from("role:system.admin"));
    final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).setAllowedPrincipals(allowedPrincipals).build();
    Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
    this.request.setEndpointPath("/_/service/demo/test");
    boolean forbiddenErrorThrown = false;
    try {
        this.handler.handle(this.request, PortalResponse.create().build(), null);
    } catch (WebException e) {
        if (HttpStatus.UNAUTHORIZED == e.getStatus()) {
            forbiddenErrorThrown = true;
        }
    }
    assertTrue(forbiddenErrorThrown);
}
Also used : WebException(com.enonic.xp.web.WebException) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

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