[MAGNOLIA-8658] Programmatic rendering Created: 28/Nov/22  Updated: 05/Dec/22  Resolved: 28/Nov/22

Status: Closed
Project: Magnolia
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Neutral
Reporter: Roman Kovařík Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
causality
caused by MAGNOLIA-6452 Add contentType field to renderers Closed
caused by MAGNOLIA-4165 rendering: implement new rendering Closed
caused by MAGNOLIA-7543 Make page editors attributes configur... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)

 Description   

Steps to reproduce

  1. Duplicate https://demo.magnolia-cms.com/.magnolia/admincentral#app:groovy:browser;/samples/commands/GroovyMailCommand:null:
  2. https://demopublic.magnolia-cms.com/.magnolia/admincentral#app:groovy:detail;/samples/commands/GroovyMailCommand0:edit Edit the duplicated script and add this content:
  3. package samples.commands;
    
    import info.magnolia.rendering.engine.*;
    import info.magnolia.rendering.context.*;
    import info.magnolia.objectfactory.Components;
    import info.magnolia.commands.CommandsManager;
    import info.magnolia.commands.impl.*;
    import org.apache.commons.lang3.text.StrBuilder;
    
    public class ProgrammaticRenderCommand extends BaseRepositoryCommand {
    
        @Override
        public boolean execute(Context context) throws Exception {
    
            def node = context.getJCRSession("website").getNode("/travel/about");
    
            final StrBuilder out = new StrBuilder();
            AppendableOnlyOutputProvider outputProvider = new AppendableOnlyOutputProvider(out);
    
            RenderingEngine renderingEngine = Components.getComponent(RenderingEngine.class);
            renderingEngine.render(node, outputProvider);
            
            def resources = context.getJCRSession("resources");
            def rendered = NodeUtil.createPath(resources.getRootNode(), "rendered", "mgnl:content");
            rendered.setProperty("text", out.toString());
            resources.save();
            return true;
        }
    }
    
  1. Save changes.
  2. Set https://demopublic.magnolia-cms.com/.magnolia/admincentral#app:configuration:browser;/modules/publishing-core/commands/default/publish@class:treeview: to samples.commands.ProgrammaticRenderCommand (we misuse the publication command for simplicity but we are on public instance so the command shouldn't be used)
  3. Publish something (e.g. https://demopublic.magnolia-cms.com/.magnolia/admincentral#app:configuration:browser;/modules/publishing-core/commands/default/publish:treeview:)
  4. The page is rendered and saved to the resources workspace. This is working fine as we reuse the request created when clicking the action. See https://demopublic.magnolia-cms.com/.magnolia/admincentral#app:resources:view;/rendered:edit.
  5. Try to do the step 3. but this time Publish recursively. (this action  is configured to run asynchronously)
  6. This fails with NullPointerException as rendering misses a request.

Expected results

I can render content without a web request (asynchronously).

Actual results

The process fails on missing web context.

Workaround

Programmatically create http request with all that information against Magnolia and capture rendered content.

Development notes



 Comments   
Comment by Roman Kovařík [ 28/Nov/22 ]

Closing as won't do as 

  • Magnolia can't guarantee that no template script/ model attempts to access web context or request on its own.
  • The only reliable way to render asynchronously is described in the workaround section.
Generated at Mon Feb 12 04:34:38 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.