Search in sources :

Example 1 with ScriptEvaluatingEvent

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;
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Event(org.xwiki.observation.event.Event) LinkedList(java.util.LinkedList)

Example 2 with ScriptEvaluatingEvent

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);
    }
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptMacroParameters(org.xwiki.rendering.macro.script.ScriptMacroParameters) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 3 with ScriptEvaluatingEvent

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())));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) Event(org.xwiki.observation.event.Event) EventListener(org.xwiki.observation.EventListener) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery)

Example 4 with 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());
}
Also used : ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Test(org.junit.Test)

Example 5 with ScriptEvaluatingEvent

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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

ScriptEvaluatingEvent (org.xwiki.script.event.ScriptEvaluatingEvent)9 CancelableEvent (org.xwiki.observation.event.CancelableEvent)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 Test (org.junit.Test)4 ScriptEvaluatedEvent (org.xwiki.script.event.ScriptEvaluatedEvent)3 Event (org.xwiki.observation.event.Event)2 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 LinkedList (java.util.LinkedList)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 ExtendedURLClassLoader (org.xwiki.classloader.ExtendedURLClassLoader)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EventListener (org.xwiki.observation.EventListener)1 Block (org.xwiki.rendering.block.Block)1 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 ScriptMacroParameters (org.xwiki.rendering.macro.script.ScriptMacroParameters)1