Search in sources :

Example 1 with RenderException

use of com.enonic.xp.portal.impl.rendering.RenderException in project xp by enonic.

the class MacroInstruction method evaluate.

@Override
public PortalResponse evaluate(final PortalRequest portalRequest, final String instruction) {
    if (!Instruction.isInstruction(instruction, "MACRO")) {
        return null;
    }
    // parse instruction
    final Instruction macroInstruction;
    try {
        macroInstruction = new InstructionParser().parse(instruction);
    } catch (RenderException e) {
        return null;
    }
    final String macroName = macroInstruction.attribute(MACRO_NAME);
    if (macroName == null) {
        return null;
    }
    // resolve macro processor
    final Site site = portalRequest.getSite();
    if (site == null) {
        throw new RenderException("Macro controller script could not be resolved, context site could not be found.");
    }
    final MacroDescriptor macroDescriptor = resolveMacroDescriptor(site.getSiteConfigs(), macroName);
    if (macroDescriptor == null) {
        final String editModeMacro = toMacroInstruction(macroInstruction);
        return PortalResponse.create().body(editModeMacro).build();
    }
    final MacroProcessor macroProcessor = resolveMacroProcessor(macroDescriptor);
    if (macroProcessor == null) {
        throw new RenderException("Macro controller not found: " + macroName);
    }
    // execute macro
    final MacroContext context = createContext(macroInstruction, macroDescriptor, portalRequest);
    final ApplicationKey previousAppKey = portalRequest.getApplicationKey();
    try {
        portalRequest.setApplicationKey(macroDescriptor.getKey().getApplicationKey());
        return macroProcessor.process(context);
    } finally {
        portalRequest.setApplicationKey(previousAppKey);
    }
}
Also used : Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) MacroContext(com.enonic.xp.portal.macro.MacroContext) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction)

Example 2 with RenderException

use of com.enonic.xp.portal.impl.rendering.RenderException in project xp by enonic.

the class HtmlBlockParser method parse.

public HtmlBlocks parse(final String html) {
    this.currentBlock = new StringBuilder();
    this.htmlBlocks = HtmlBlocks.builder();
    try {
        final MarkupAttoParser parser = new MarkupAttoParser();
        parser.parse(html, new HtmlBlockParseAttoHandler(this));
        addStaticHtml();
        return this.htmlBlocks.build();
    } catch (final AttoParseException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw new RenderException("Failed to post process document", cause != null ? cause : e);
    }
}
Also used : RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MarkupAttoParser(org.attoparser.markup.MarkupAttoParser) AttoParseException(org.attoparser.AttoParseException)

Example 3 with RenderException

use of com.enonic.xp.portal.impl.rendering.RenderException in project xp by enonic.

the class ComponentInstruction method resolveComponent.

private Component resolveComponent(final PortalRequest portalRequest, final ComponentPath path) {
    final Content content = portalRequest.getContent();
    if (content == null) {
        return null;
    }
    final Page page = content.getPage();
    if (content.getType().isFragment()) {
        return resolveComponentInFragment(page, path);
    }
    final PageRegions pageRegions = page.getRegions();
    Component component = pageRegions.getComponent(path);
    if (component == null) {
        throw new RenderException("Component not found: [{0}]", path);
    }
    return component;
}
Also used : PageRegions(com.enonic.xp.page.PageRegions) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Content(com.enonic.xp.content.Content) Page(com.enonic.xp.page.Page) LayoutComponent(com.enonic.xp.region.LayoutComponent) Component(com.enonic.xp.region.Component)

Example 4 with RenderException

use of com.enonic.xp.portal.impl.rendering.RenderException in project xp by enonic.

the class ResponseProcessorExecutor method execute.

public PortalResponse execute(final ResponseProcessorDescriptor filter, final PortalRequest request, final PortalResponse response) {
    final String filterName = filter.getName();
    final String filterJsPath = "/site/processors/" + filterName + ".js";
    final ResourceKey script = ResourceKey.from(filter.getApplication(), filterJsPath);
    final ScriptExports filterExports;
    try {
        filterExports = this.scriptService.execute(script);
    } catch (ResourceNotFoundException e) {
        LOG.warn("Filter execution failed: {}", e.getMessage());
        throw e;
    }
    final boolean exists = filterExports.hasMethod(RESPONSE_PROCESSOR_METHOD);
    if (!exists) {
        throw new RenderException("Missing exported function [{0}] in response filter [{1}]", RESPONSE_PROCESSOR_METHOD, filterJsPath);
    }
    final ApplicationKey previousApp = request.getApplicationKey();
    // set application of the filter in the current context PortalRequest
    request.setApplicationKey(filter.getApplication());
    PortalRequestAccessor.set(request);
    try {
        return Tracer.trace("controllerScript", () -> executeFilter(filterExports, request, response));
    } finally {
        PortalRequestAccessor.remove();
        request.setApplicationKey(previousApp);
    }
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceNotFoundException(com.enonic.xp.resource.ResourceNotFoundException) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 5 with RenderException

use of com.enonic.xp.portal.impl.rendering.RenderException in project xp by enonic.

the class MacroInstructionTest method testMacroInstructionMissingController.

@Test
public void testMacroInstructionMissingController() throws Exception {
    MacroKey key = MacroKey.from("myapp:mymacro");
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    try {
        macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"");
        fail("Expected exception");
    } catch (RenderException e) {
        assertEquals("Macro controller not found: mymacro", e.getMessage());
    }
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

RenderException (com.enonic.xp.portal.impl.rendering.RenderException)7 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 MacroDescriptor (com.enonic.xp.macro.MacroDescriptor)2 Component (com.enonic.xp.region.Component)2 LayoutComponent (com.enonic.xp.region.LayoutComponent)2 ResourceKey (com.enonic.xp.resource.ResourceKey)2 ScriptExports (com.enonic.xp.script.ScriptExports)2 Test (org.junit.jupiter.api.Test)2 Content (com.enonic.xp.content.Content)1 MacroKey (com.enonic.xp.macro.MacroKey)1 Page (com.enonic.xp.page.Page)1 PageRegions (com.enonic.xp.page.PageRegions)1 PortalRequest (com.enonic.xp.portal.PortalRequest)1 PortalResponse (com.enonic.xp.portal.PortalResponse)1 MacroContext (com.enonic.xp.portal.macro.MacroContext)1 MacroProcessor (com.enonic.xp.portal.macro.MacroProcessor)1 PostProcessInstruction (com.enonic.xp.portal.postprocess.PostProcessInstruction)1 PortalScriptService (com.enonic.xp.portal.script.PortalScriptService)1 LayoutRegions (com.enonic.xp.region.LayoutRegions)1 ResourceNotFoundException (com.enonic.xp.resource.ResourceNotFoundException)1