use of org.xwiki.script.event.ScriptEvaluatingEvent in project xwiki-platform by xwiki.
the class ScriptClassLoaderHandlerListener method getEvents.
@Override
public List<Event> getEvents() {
List<Event> events = new LinkedList<Event>();
events.add(new ScriptEvaluatingEvent());
events.add(new ScriptEvaluatedEvent());
return events;
}
use of org.xwiki.script.event.ScriptEvaluatingEvent in project xwiki-platform by xwiki.
the class ScriptClassLoaderHandlerListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
if (!(data instanceof ScriptMacroParameters)) {
return;
}
if (event instanceof ScriptEvaluatingEvent) {
// Set the context class loader to the script CL to ensure that any script engine using the context
// classloader will work just fine.
// Note: We must absolutely ensure that we always use the same context CL during the whole execution
// request since JSR223 script engines (for example) that create internal class loaders need to
// continue using these class loaders (where classes defined in scripts have been loaded for example).
ScriptMacroParameters parameters = (ScriptMacroParameters) data;
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
this.execution.getContext().setProperty(EXECUTION_CONTEXT_ORIG_CLASSLOADER_KEY, originalClassLoader);
try {
ClassLoader newClassLoader = getClassLoader(parameters.getJars(), originalClassLoader);
Thread.currentThread().setContextClassLoader(newClassLoader);
} catch (Exception exception) {
// abort execution
((CancelableEvent) event).cancel(exception.getMessage());
}
} else if (event instanceof ScriptEvaluatedEvent) {
// Restore original class loader.
ClassLoader originalClassLoader = (ClassLoader) this.execution.getContext().getProperty(EXECUTION_CONTEXT_ORIG_CLASSLOADER_KEY);
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
use of org.xwiki.script.event.ScriptEvaluatingEvent in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager cm) throws Exception {
Mockery mockery = new JUnit4Mockery();
new ScriptMockSetup(mockery, cm);
// fake nested script validator never fails
final EventListener nestedValidator = cm.registerMockComponent(mockery, EventListener.class, "nestedscriptmacrovalidator");
mockery.checking(new Expectations() {
{
atLeast(1).of(nestedValidator).onEvent(with(any(Event.class)), with(any(MacroTransformationContext.class)), with(any(ScriptMacroParameters.class)));
allowing(nestedValidator).getName();
will(returnValue("nestedscriptmacrovalidator"));
allowing(nestedValidator).getEvents();
will(returnValue(Collections.singletonList((Event) new ScriptEvaluatingEvent())));
}
});
}
use of org.xwiki.script.event.ScriptEvaluatingEvent in project xwiki-platform by xwiki.
the class NestedScriptMacroValidatorTest method testNoNestedScriptInHtml.
@Test
public void testNoNestedScriptInHtml() throws Exception {
MacroTransformationContext context = buildContext("script", "html", "script");
CancelableEvent event = new ScriptEvaluatingEvent();
this.validator.onEvent(event, context, null);
Assert.assertTrue(event.isCanceled());
}
use of org.xwiki.script.event.ScriptEvaluatingEvent in project xwiki-platform by xwiki.
the class ProgrammingRightCheckerListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
XWikiContext context = this.xwikiContextProvider.get();
// Should the current document be excluded from the PR check?
XWikiDocument contextDocument = context.getDoc();
DocumentReference currentDocReference = contextDocument.getDocumentReference();
if (this.excludePattern != null && this.excludePattern.matcher(currentDocReference.toString()).matches()) {
this.logger.info("PRChecker: Skipping check for [{}] since it's excluded", currentDocReference);
return;
}
if (event instanceof ScriptEvaluatingEvent) {
// Make it simpler to debug why a test will fail with the PR checker active by logging that we dropped
// permissions.
boolean logPrinted = false;
boolean hasPR = this.contextualAuthorizationManager.hasAccess(Right.PROGRAM, currentDocReference);
if (hasPR) {
// Ideally we would print this in info mode but since right now all pages have PR by default, this is
// just swamping the logs...
this.logger.debug("PRChecker: Dropping permissions for page [{}], which had PR", currentDocReference);
logPrinted = true;
}
// Save the original value
Boolean originalValue = (Boolean) context.get(XWikiConstant.DROPPED_PERMISSIONS);
if (originalValue != null) {
context.put(PRCHECK_KEY, originalValue);
}
if (!logPrinted) {
this.logger.debug("PRChecker: Dropping permissions for page [{}]", currentDocReference);
}
context.dropPermissions();
} else {
// Restore the original value
Boolean originalValue = (Boolean) context.get(PRCHECK_KEY);
this.logger.debug("PRChecker: Restoring permissions for page [{}]", currentDocReference);
if (originalValue != null) {
context.put(XWikiConstant.DROPPED_PERMISSIONS, originalValue);
} else {
context.remove(XWikiConstant.DROPPED_PERMISSIONS);
}
context.remove(PRCHECK_KEY);
}
}
Aggregations