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);
}
}
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);
}
}
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;
}
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);
}
}
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());
}
}
Aggregations