use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class DefaultOfficeViewerScriptServiceTest method viewThrowingException.
@Test
public void viewThrowingException() throws Exception {
ExecutionContext executionContext = new ExecutionContext();
executionContext.setProperty("officeView.caughtException", "before");
Execution execution = mocker.getInstance(Execution.class);
when(execution.getContext()).thenReturn(executionContext);
Assert.assertNull(mocker.getComponentUnderTest().view(null));
Exception e = mocker.getComponentUnderTest().getCaughtException();
Assert.assertTrue(e instanceof NullPointerException);
verify(mocker.getMockedLogger()).error("Failed to view office document: null", e);
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class AbstractStatsStoreItem method store.
@Override
public void store(List<XWikiStatsStoreItem> statsList) {
ExecutionContext econtext = Utils.getComponent(Execution.class).getContext();
XWikiContext currentContext = (XWikiContext) econtext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
try {
econtext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.context);
storeInternal(statsList);
} finally {
econtext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, currentContext);
}
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class WikiMacroExecutionEventListener method onWikiMacroExecutionStartsEvent.
/**
* Called when receiving a {@link WikiMacroExecutionStartsEvent} event.
*
* @param wikiMacro the wiki macro sending the event
*/
public void onWikiMacroExecutionStartsEvent(WikiMacro wikiMacro) {
// Modify the context for that following code is executed with the right of wiki macro author
AutoCloseable sucontext = this.suExecutor.before(wikiMacro.getAuthorReference());
// Put it in an hidden context property to restore it later
ExecutionContext econtext = this.execution.getContext();
// Use a stack in case a wiki macro calls another wiki macro
Stack<AutoCloseable> backup = (Stack<AutoCloseable>) econtext.getProperty(SUCONTEXT_KEY);
if (backup == null) {
backup = new Stack<AutoCloseable>();
econtext.setProperty(SUCONTEXT_KEY, backup);
}
backup.push(sucontext);
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class WikiMacroExecutionEventListener method onWikiMacroExecutionFinishedEvent.
/**
* Called when receiving a {@link WikiMacroExecutionFinishedEvent} event.
*/
public void onWikiMacroExecutionFinishedEvent() {
// Get the su context to restore
ExecutionContext econtext = this.execution.getContext();
// Use a stack in case a wiki macro calls another wiki macro
Stack<AutoCloseable> backup = (Stack<AutoCloseable>) econtext.getProperty(SUCONTEXT_KEY);
if (backup != null && !backup.isEmpty()) {
// Restore the context execution rights
this.suExecutor.after(backup.pop());
} else {
this.logger.error("Can't find any backed up execution right information in the execution context");
}
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class DefaultWikiMacro method execute.
@Override
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException {
validate(parameters, macroContent);
// Parse the wiki macro content.
XDOM xdom = prepareWikiMacroContent(context);
// Prepare macro context.
Map<String, Object> macroBinding = new HashMap<>();
macroBinding.put(MACRO_PARAMS_KEY, parameters);
macroBinding.put(MACRO_CONTENT_KEY, macroContent);
macroBinding.put(MACRO_DESCRIPTOR_KEY, getDescriptor());
macroBinding.put(MACRO_CONTEXT_KEY, context);
macroBinding.put(MACRO_RESULT_KEY, null);
// Extension point to add more wiki macro bindings
try {
List<WikiMacroBindingInitializer> bindingInitializers = this.componentManager.getInstanceList(WikiMacroBindingInitializer.class);
for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) {
bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, macroBinding);
}
} catch (ComponentLookupException e) {
// TODO: we should probably log something but that should never happen
}
// Execute the macro
ObservationManager observation = null;
try {
observation = this.componentManager.getInstance(ObservationManager.class);
} catch (ComponentLookupException e) {
// TODO: maybe log something
}
// Get XWiki context
Map<String, Object> xwikiContext = null;
try {
Execution execution = this.componentManager.getInstance(Execution.class);
ExecutionContext econtext = execution.getContext();
if (econtext != null) {
xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext");
}
} catch (ComponentLookupException e) {
// TODO: maybe log something
}
try {
Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT);
if (xwikiContext != null) {
// Place macro context inside xwiki context ($xcontext.macro).
xwikiContext.put(MACRO_KEY, macroBinding);
}
MacroBlock wikiMacroBlock = context.getCurrentMacroBlock();
MacroMarkerBlock wikiMacroMarker = new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline());
// Make sure to use provided metadatas
MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), xdom.getMetaData());
// Make sure the context XDOM contains the wiki macro content
wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock);
// "Emulate" the fact that wiki macro block is still part of the XDOM (what is in the XDOM is a
// MacroMarkerBlock and MacroTransformationContext current macro block only support MacroBlock so we can't
// switch it without breaking some APIs)
wikiMacroBlock.setParent(metaDataBlock.getParent());
wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling());
wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling());
try {
if (observation != null) {
observation.notify(STARTEXECUTION_EVENT, this, macroBinding);
}
// Perform internal macro transformations.
TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax);
txContext.setId(context.getId());
RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class);
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, wikiMacroMarker);
} finally {
// Restore context XDOM to its previous state
metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock);
}
return extractResult(wikiMacroMarker.getChildren(), macroBinding, context);
} catch (Exception ex) {
throw new MacroExecutionException("Error while performing internal macro transformations", ex);
} finally {
if (xwikiContext != null) {
xwikiContext.remove(MACRO_KEY);
}
if (observation != null) {
observation.notify(ENDEXECUTION_EVENT, this);
}
}
}
Aggregations