Search in sources :

Example 11 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript in project xp by enonic.

the class PageRenderer method doRender.

@Override
public PortalResponse doRender(final Content content, final PortalRequest portalRequest) {
    final PageDescriptor pageDescriptor = portalRequest.getPageDescriptor();
    final RenderMode mode = portalRequest.getMode();
    PortalResponse portalResponse;
    if (pageDescriptor != null) {
        final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(pageDescriptor.getComponentPath());
        portalResponse = controllerScript.execute(portalRequest);
    } else if (portalRequest.getControllerScript() != null) {
        portalResponse = portalRequest.getControllerScript().execute(portalRequest);
    } else if ((mode == EDIT || mode == PREVIEW || mode == INLINE) && portalRequest.getContent().getType().isFragment()) {
        portalResponse = renderDefaultFragmentPage(portalRequest, content);
    } else {
        portalResponse = renderForNoPageDescriptor(portalRequest, content);
    }
    return portalResponse;
}
Also used : PageDescriptor(com.enonic.xp.page.PageDescriptor) RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript)

Example 12 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript in project xp by enonic.

the class PartRendererTest method htmlResponseComponentEditModeNoMethodToHandleReq.

@Test
public void htmlResponseComponentEditModeNoMethodToHandleReq() {
    final PartDescriptor partDescriptor = PartDescriptor.create().displayName("My part component").config(Form.create().build()).key(DescriptorKey.from("module:myPartComponent")).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return new PortalResponseSerializer(null, HttpStatus.METHOD_NOT_ALLOWED).serialize();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final PartDescriptorService partDescriptorService = Mockito.mock(PartDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new PartRenderer();
    renderer.setPartDescriptorService(partDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(partDescriptorService.getByKey(any())).thenReturn(partDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    partComponent = PartComponent.create().descriptor("myapp:myPartComponent").descriptor(partDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(partComponent, portalRequest);
    // verify
    String expected = "<div data-portal-component-type=\"part\" data-portal-placeholder=\"true\" data-portal-placeholder-error=\"true\"><span class=\"data-portal-placeholder-error\">No method provided to handle request</span></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) PartDescriptor(com.enonic.xp.region.PartDescriptor) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 13 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript in project xp by enonic.

the class PartRendererTest method nullResponseComponentEditMode.

@Test
public void nullResponseComponentEditMode() {
    final PartDescriptor partDescriptor = PartDescriptor.create().displayName("My part component").config(Form.create().build()).key(DescriptorKey.from("module:myPartComponent")).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return new PortalResponseSerializer(null).serialize();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final PartDescriptorService partDescriptorService = Mockito.mock(PartDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new PartRenderer();
    renderer.setPartDescriptorService(partDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(partDescriptorService.getByKey(any())).thenReturn(partDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    partComponent = PartComponent.create().descriptor("myapp:myPartComponent").descriptor(partDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(partComponent, portalRequest);
    // verify
    String expected = "<div data-portal-component-type=\"part\"></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) PartDescriptor(com.enonic.xp.region.PartDescriptor) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 14 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript in project xp by enonic.

the class PartRendererTest method htmlResponseComponentEditMode.

@Test
public void htmlResponseComponentEditMode() {
    final PartDescriptor partDescriptor = PartDescriptor.create().displayName("My part component").config(Form.create().build()).key(DescriptorKey.from("module:myPartComponent")).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return PortalResponse.create().body("<h1 class=\"important\">My component</h1>").contentType(MediaType.HTML_UTF_8).status(HttpStatus.OK).build();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final PartDescriptorService partDescriptorService = Mockito.mock(PartDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new PartRenderer();
    renderer.setPartDescriptorService(partDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(partDescriptorService.getByKey(any())).thenReturn(partDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    partComponent = PartComponent.create().descriptor("myapp:myPartComponent").descriptor(partDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(partComponent, portalRequest);
    // verify
    String expected = "<h1 data-portal-component-type=\"part\" class=\"important\">My component</h1>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) PartDescriptor(com.enonic.xp.region.PartDescriptor) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 15 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript in project xp by enonic.

the class LayoutRendererTest method htmlResponseComponentEditMode.

@Test
public void htmlResponseComponentEditMode() {
    final LayoutDescriptor layoutDescriptor = LayoutDescriptor.create().displayName("My layout component").config(Form.create().build()).key(DescriptorKey.from("module:myLayoutComponent")).regions(RegionDescriptors.create().add(RegionDescriptor.create().name("left").build()).add(RegionDescriptor.create().name("right").build()).build()).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return PortalResponse.create().body("<div class=\"row\"><div data-portal-region=\"left\" class=\"col-left\"></div><div data-portal-region=\"right\" class=\"col-right\"></div></div>").contentType(MediaType.HTML_UTF_8).status(HttpStatus.OK).build();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final LayoutDescriptorService layoutDescriptorService = Mockito.mock(LayoutDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new LayoutRenderer();
    renderer.setLayoutDescriptorService(layoutDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(layoutDescriptorService.getByKey(any())).thenReturn(layoutDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    layoutComponent = LayoutComponent.create().descriptor("myapp:myLayoutComponent").descriptor(layoutDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(layoutComponent, portalRequest);
    // verify
    String expected = "<div data-portal-component-type=\"layout\" class=\"row\"><div data-portal-region=\"left\" class=\"col-left\"></div><div data-portal-region=\"right\" class=\"col-right\"></div></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) LayoutDescriptor(com.enonic.xp.region.LayoutDescriptor) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ControllerScript (com.enonic.xp.portal.controller.ControllerScript)16 PortalRequest (com.enonic.xp.portal.PortalRequest)7 PortalResponse (com.enonic.xp.portal.PortalResponse)7 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)7 Test (org.junit.jupiter.api.Test)6 WebSocketEvent (com.enonic.xp.web.websocket.WebSocketEvent)5 PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)3 PartDescriptor (com.enonic.xp.region.PartDescriptor)3 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)3 ResourceKey (com.enonic.xp.resource.ResourceKey)3 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)3 WebSocketConfig (com.enonic.xp.web.websocket.WebSocketConfig)3 WebSocketContext (com.enonic.xp.web.websocket.WebSocketContext)3 WebSocketEndpoint (com.enonic.xp.web.websocket.WebSocketEndpoint)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 DescriptorKey (com.enonic.xp.page.DescriptorKey)2 RenderMode (com.enonic.xp.portal.RenderMode)2 LayoutDescriptor (com.enonic.xp.region.LayoutDescriptor)2 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2