Search in sources :

Example 1 with ControllerScript

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

the class WebAppHandler method executeController.

private PortalResponse executeController(final PortalRequest req) throws Exception {
    final ControllerScript script = getScript(req.getApplicationKey());
    final PortalResponse res = script.execute(req);
    final WebSocketConfig webSocketConfig = res.getWebSocket();
    final WebSocketContext webSocketContext = req.getWebSocketContext();
    if ((webSocketContext != null) && (webSocketConfig != null)) {
        final WebSocketEndpoint webSocketEndpoint = newWebSocketEndpoint(webSocketConfig, script, req.getApplicationKey());
        webSocketContext.apply(webSocketEndpoint);
    }
    return res;
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) WebSocketEndpoint(com.enonic.xp.web.websocket.WebSocketEndpoint) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Example 2 with ControllerScript

use of com.enonic.xp.portal.controller.ControllerScript 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 3 with ControllerScript

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

the class WebAppHandlerTest method handle_executeController.

@Test
public void handle_executeController() throws Exception {
    mockResource("myapp:/assets/a.txt", null);
    this.request.setApplicationKey(ApplicationKey.from("myapp"));
    this.request.setBaseUri("/webapp/myapp");
    this.request.setRawPath("/webapp/myapp/a.txt");
    final ControllerScript script = Mockito.mock(ControllerScript.class);
    Mockito.when(this.controllerScriptFactory.fromScript(ResourceKey.from("myapp:/webapp/webapp.js"))).thenReturn(script);
    final PortalResponse response = PortalResponse.create().build();
    Mockito.when(script.execute(Mockito.any())).thenReturn(response);
    assertSame(response, this.handler.doHandle(this.request, null, this.chain));
    assertEquals("/webapp/myapp", this.request.getContextPath());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) Test(org.junit.jupiter.api.Test)

Example 4 with ControllerScript

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

the class LayoutRendererTest method nullResponseComponentEditMode.

@Test
public void nullResponseComponentEditMode() {
    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 new PortalResponseSerializer(null).serialize();
        }

        @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\"></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) 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)

Example 5 with ControllerScript

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

the class AdminToolHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    // Retrieves the AdminToolDescriptor
    final AdminToolDescriptor adminToolDescriptor = adminToolDescriptorService.getByKey(descriptorKey);
    if (adminToolDescriptor == null) {
        throw WebException.notFound(String.format("Admin application [%s] not found", descriptorKey));
    }
    // Checks if the access to AdminToolDescriptor is allowed
    final PrincipalKeys principals = ContextAccessor.current().getAuthInfo().getPrincipals();
    if (!adminToolDescriptor.isAccessAllowed(principals)) {
        throw WebException.forbidden(String.format("You don't have permission to access [%s]", descriptorKey));
    }
    // Renders the Admin application
    final ResourceKey scriptDir = ResourceKey.from(descriptorKey.getApplicationKey(), "admin/tools/" + 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) AdminToolDescriptor(com.enonic.xp.admin.tool.AdminToolDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey)

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