Search in sources :

Example 1 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class FilterScriptImpl method doExecute.

private PortalResponse doExecute(final PortalRequest request, final WebResponse response, final WebHandlerChain webHandlerChain) {
    if (!this.scriptExports.hasMethod(FILTER_SCRIPT_METHOD)) {
        throw new WebException(HttpStatus.NOT_IMPLEMENTED, "Missing exported function '" + FILTER_SCRIPT_METHOD + "' in filter script: " + scriptExports.getScript());
    }
    Tracer.withCurrent(this::addTraceInfo);
    final PortalRequestMapper requestMapper = new PortalRequestMapper(request);
    final FilterNextFunctionWrapper nextHandler = new FilterNextFunctionWrapper(webHandlerChain, request, response, scriptExports.getScript(), scriptService);
    final ScriptValue result = this.scriptExports.executeMethod(FILTER_SCRIPT_METHOD, requestMapper, nextHandler);
    return new PortalResponseSerializer(result).serialize();
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) WebException(com.enonic.xp.web.WebException) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper)

Example 2 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class IdProviderControllerScriptImpl method doExecute.

private PortalResponse doExecute(final String functionName, final PortalRequest portalRequest) {
    final boolean exists = this.scriptExports.hasMethod(functionName);
    if (!exists) {
        return new PortalResponseSerializer(null, HttpStatus.NOT_FOUND).serialize();
    }
    final PortalRequestMapper requestMapper = new PortalRequestMapper(portalRequest);
    final ScriptValue result = this.scriptExports.executeMethod(functionName, requestMapper);
    if ((result == null) || !result.isObject()) {
        return null;
    } else {
        return new PortalResponseSerializer(result).serialize();
    }
}
Also used : PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper)

Example 3 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class ResponseProcessorExecutor method executeFilter.

private PortalResponse executeFilter(final ScriptExports filterExports, final PortalRequest request, final PortalResponse response) {
    final PortalRequestMapper requestMapper = new PortalRequestMapper(request);
    final PortalResponseMapper responseMapper = new PortalResponseMapper(response);
    final ScriptValue result = filterExports.executeMethod(RESPONSE_PROCESSOR_METHOD, requestMapper, responseMapper);
    final PortalResponseSerializer portalResponseSerializer = new PortalResponseSerializer(result);
    if (unmodifiedByteSourceBody(response, result)) {
        portalResponseSerializer.body(response.getBody());
    }
    addTraceInfo(Tracer.current(), filterExports);
    return portalResponseSerializer.serialize();
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper)

Example 4 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer 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 PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class MacroProcessorScript method doProcess.

private PortalResponse doProcess(final MacroContext macroContext) {
    final boolean exists = this.scriptExports.hasMethod(SCRIPT_METHOD_NAME);
    if (!exists) {
        return null;
    }
    final MacroContextMapper macroContextMapper = new MacroContextMapper(macroContext);
    final ScriptValue result = this.scriptExports.executeMethod(SCRIPT_METHOD_NAME, macroContextMapper);
    return new PortalResponseSerializer(result).serialize();
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer)

Aggregations

PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)8 ScriptValue (com.enonic.xp.script.ScriptValue)5 PortalRequest (com.enonic.xp.portal.PortalRequest)3 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)3 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)3 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)3 WebSocketEvent (com.enonic.xp.web.websocket.WebSocketEvent)3 Test (org.junit.jupiter.api.Test)3 PartDescriptor (com.enonic.xp.region.PartDescriptor)2 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)2 PortalErrorMapper (com.enonic.xp.portal.impl.mapper.PortalErrorMapper)1 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)1 LayoutDescriptor (com.enonic.xp.region.LayoutDescriptor)1 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)1 WebException (com.enonic.xp.web.WebException)1